blob: 87b0e856b6d04f88834ade99fe6766a78cc002e8 [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>
David Sterbadff51cd2011-06-14 12:52:17 +020026#include <linux/ratelimit.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050028#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050029#include "ctree.h"
30#include "disk-io.h"
31#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040032#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050034#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040036#include "free-space-cache.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060037#include "math.h"
Chris Masonfec577f2007-02-26 10:40:21 -050038
Arne Jansen709c0482011-09-12 12:22:57 +020039#undef SCRAMBLE_DELAYED_REFS
40
Miao Xie9e622d62012-01-26 15:01:12 -050041/*
42 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040043 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
44 * if we really need one.
45 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040046 * CHUNK_ALLOC_LIMITED means to only try and allocate one
47 * if we have very few chunks already allocated. This is
48 * used as part of the clustering code to help make sure
49 * we have a good pool of storage to cluster in, without
50 * filling the FS with empty chunks
51 *
Miao Xie9e622d62012-01-26 15:01:12 -050052 * CHUNK_ALLOC_FORCE means it must try to allocate one
53 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040054 */
55enum {
56 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050057 CHUNK_ALLOC_LIMITED = 1,
58 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040059};
60
Josef Bacikfb25e912011-07-26 17:00:46 -040061/*
62 * Control how reservations are dealt with.
63 *
64 * RESERVE_FREE - freeing a reservation.
65 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
66 * ENOSPC accounting
67 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
68 * bytes_may_use as the ENOSPC accounting is done elsewhere
69 */
70enum {
71 RESERVE_FREE = 0,
72 RESERVE_ALLOC = 1,
73 RESERVE_ALLOC_NO_ACCOUNT = 2,
74};
75
Josef Bacikf3465ca2008-11-12 14:19:50 -050076static int update_block_group(struct btrfs_trans_handle *trans,
77 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040078 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040079static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 u64 bytenr, u64 num_bytes, u64 parent,
82 u64 root_objectid, u64 owner_objectid,
83 u64 owner_offset, int refs_to_drop,
84 struct btrfs_delayed_extent_op *extra_op);
85static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
86 struct extent_buffer *leaf,
87 struct btrfs_extent_item *ei);
88static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
89 struct btrfs_root *root,
90 u64 parent, u64 root_objectid,
91 u64 flags, u64 owner, u64 offset,
92 struct btrfs_key *ins, int ref_mod);
93static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
94 struct btrfs_root *root,
95 u64 parent, u64 root_objectid,
96 u64 flags, struct btrfs_disk_key *key,
97 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050098static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -040099 struct btrfs_root *extent_root, u64 flags,
100 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400101static int find_next_key(struct btrfs_path *path, int level,
102 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400103static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
104 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400105static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
106 u64 num_bytes, int reserve);
Josef Bacik6a632092009-02-20 11:00:09 -0500107
Josef Bacik817d52f2009-07-13 21:29:25 -0400108static noinline int
109block_group_cache_done(struct btrfs_block_group_cache *cache)
110{
111 smp_mb();
112 return cache->cached == BTRFS_CACHE_FINISHED;
113}
114
Josef Bacik0f9dd462008-09-23 13:14:11 -0400115static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
116{
117 return (cache->flags & bits) == bits;
118}
119
David Sterba62a45b62011-04-20 15:52:26 +0200120static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000121{
122 atomic_inc(&cache->count);
123}
124
125void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
126{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400127 if (atomic_dec_and_test(&cache->count)) {
128 WARN_ON(cache->pinned > 0);
129 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800130 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000131 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400132 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000133}
134
Josef Bacik0f9dd462008-09-23 13:14:11 -0400135/*
136 * this adds the block group to the fs_info rb tree for the block group
137 * cache
138 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500139static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400140 struct btrfs_block_group_cache *block_group)
141{
142 struct rb_node **p;
143 struct rb_node *parent = NULL;
144 struct btrfs_block_group_cache *cache;
145
146 spin_lock(&info->block_group_cache_lock);
147 p = &info->block_group_cache_tree.rb_node;
148
149 while (*p) {
150 parent = *p;
151 cache = rb_entry(parent, struct btrfs_block_group_cache,
152 cache_node);
153 if (block_group->key.objectid < cache->key.objectid) {
154 p = &(*p)->rb_left;
155 } else if (block_group->key.objectid > cache->key.objectid) {
156 p = &(*p)->rb_right;
157 } else {
158 spin_unlock(&info->block_group_cache_lock);
159 return -EEXIST;
160 }
161 }
162
163 rb_link_node(&block_group->cache_node, parent, p);
164 rb_insert_color(&block_group->cache_node,
165 &info->block_group_cache_tree);
166 spin_unlock(&info->block_group_cache_lock);
167
168 return 0;
169}
170
171/*
172 * This will return the block group at or after bytenr if contains is 0, else
173 * it will return the block group that contains the bytenr
174 */
175static struct btrfs_block_group_cache *
176block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
177 int contains)
178{
179 struct btrfs_block_group_cache *cache, *ret = NULL;
180 struct rb_node *n;
181 u64 end, start;
182
183 spin_lock(&info->block_group_cache_lock);
184 n = info->block_group_cache_tree.rb_node;
185
186 while (n) {
187 cache = rb_entry(n, struct btrfs_block_group_cache,
188 cache_node);
189 end = cache->key.objectid + cache->key.offset - 1;
190 start = cache->key.objectid;
191
192 if (bytenr < start) {
193 if (!contains && (!ret || start < ret->key.objectid))
194 ret = cache;
195 n = n->rb_left;
196 } else if (bytenr > start) {
197 if (contains && bytenr <= end) {
198 ret = cache;
199 break;
200 }
201 n = n->rb_right;
202 } else {
203 ret = cache;
204 break;
205 }
206 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500207 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000208 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400209 spin_unlock(&info->block_group_cache_lock);
210
211 return ret;
212}
213
Yan Zheng11833d62009-09-11 16:11:19 -0400214static int add_excluded_extent(struct btrfs_root *root,
215 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400216{
Yan Zheng11833d62009-09-11 16:11:19 -0400217 u64 end = start + num_bytes - 1;
218 set_extent_bits(&root->fs_info->freed_extents[0],
219 start, end, EXTENT_UPTODATE, GFP_NOFS);
220 set_extent_bits(&root->fs_info->freed_extents[1],
221 start, end, EXTENT_UPTODATE, GFP_NOFS);
222 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400223}
224
Yan Zheng11833d62009-09-11 16:11:19 -0400225static void free_excluded_extents(struct btrfs_root *root,
226 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400227{
Yan Zheng11833d62009-09-11 16:11:19 -0400228 u64 start, end;
229
230 start = cache->key.objectid;
231 end = start + cache->key.offset - 1;
232
233 clear_extent_bits(&root->fs_info->freed_extents[0],
234 start, end, EXTENT_UPTODATE, GFP_NOFS);
235 clear_extent_bits(&root->fs_info->freed_extents[1],
236 start, end, EXTENT_UPTODATE, GFP_NOFS);
237}
238
239static int exclude_super_stripes(struct btrfs_root *root,
240 struct btrfs_block_group_cache *cache)
241{
Josef Bacik817d52f2009-07-13 21:29:25 -0400242 u64 bytenr;
243 u64 *logical;
244 int stripe_len;
245 int i, nr, ret;
246
Yan, Zheng06b23312009-11-26 09:31:11 +0000247 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
248 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
249 cache->bytes_super += stripe_len;
250 ret = add_excluded_extent(root, cache->key.objectid,
251 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100252 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng06b23312009-11-26 09:31:11 +0000253 }
254
Josef Bacik817d52f2009-07-13 21:29:25 -0400255 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
256 bytenr = btrfs_sb_offset(i);
257 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
258 cache->key.objectid, bytenr,
259 0, &logical, &nr, &stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100260 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -0400261
Josef Bacik817d52f2009-07-13 21:29:25 -0400262 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400263 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400264 ret = add_excluded_extent(root, logical[nr],
265 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 BUG_ON(ret); /* -ENOMEM */
Josef Bacik817d52f2009-07-13 21:29:25 -0400267 }
Yan Zheng11833d62009-09-11 16:11:19 -0400268
Josef Bacik817d52f2009-07-13 21:29:25 -0400269 kfree(logical);
270 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400271 return 0;
272}
273
Yan Zheng11833d62009-09-11 16:11:19 -0400274static struct btrfs_caching_control *
275get_caching_control(struct btrfs_block_group_cache *cache)
276{
277 struct btrfs_caching_control *ctl;
278
279 spin_lock(&cache->lock);
280 if (cache->cached != BTRFS_CACHE_STARTED) {
281 spin_unlock(&cache->lock);
282 return NULL;
283 }
284
Josef Bacikdde5abe2010-09-16 16:17:03 -0400285 /* We're loading it the fast way, so we don't have a caching_ctl. */
286 if (!cache->caching_ctl) {
287 spin_unlock(&cache->lock);
288 return NULL;
289 }
290
Yan Zheng11833d62009-09-11 16:11:19 -0400291 ctl = cache->caching_ctl;
292 atomic_inc(&ctl->count);
293 spin_unlock(&cache->lock);
294 return ctl;
295}
296
297static void put_caching_control(struct btrfs_caching_control *ctl)
298{
299 if (atomic_dec_and_test(&ctl->count))
300 kfree(ctl);
301}
302
Josef Bacik0f9dd462008-09-23 13:14:11 -0400303/*
304 * this is only called by cache_block_group, since we could have freed extents
305 * we need to check the pinned_extents for any extents that can't be used yet
306 * since their free space will be released as soon as the transaction commits.
307 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400308static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400309 struct btrfs_fs_info *info, u64 start, u64 end)
310{
Josef Bacik817d52f2009-07-13 21:29:25 -0400311 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 int ret;
313
314 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400315 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400317 EXTENT_DIRTY | EXTENT_UPTODATE,
318 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400319 if (ret)
320 break;
321
Yan, Zheng06b23312009-11-26 09:31:11 +0000322 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400323 start = extent_end + 1;
324 } else if (extent_start > start && extent_start < end) {
325 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400326 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500327 ret = btrfs_add_free_space(block_group, start,
328 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100329 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400330 start = extent_end + 1;
331 } else {
332 break;
333 }
334 }
335
336 if (start < end) {
337 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400338 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500339 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400341 }
342
Josef Bacik817d52f2009-07-13 21:29:25 -0400343 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400344}
345
Josef Bacikbab39bf2011-06-30 14:42:28 -0400346static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400347{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400348 struct btrfs_block_group_cache *block_group;
349 struct btrfs_fs_info *fs_info;
350 struct btrfs_caching_control *caching_ctl;
351 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400352 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400353 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400354 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400355 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400356 u64 last = 0;
357 u32 nritems;
358 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400359
Josef Bacikbab39bf2011-06-30 14:42:28 -0400360 caching_ctl = container_of(work, struct btrfs_caching_control, work);
361 block_group = caching_ctl->block_group;
362 fs_info = block_group->fs_info;
363 extent_root = fs_info->extent_root;
364
Chris Masone37c9e62007-05-09 20:13:14 -0400365 path = btrfs_alloc_path();
366 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400367 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400368
Josef Bacik817d52f2009-07-13 21:29:25 -0400369 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400370
Chris Mason5cd57b22008-06-25 16:01:30 -0400371 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400372 * We don't want to deadlock with somebody trying to allocate a new
373 * extent for the extent root while also trying to search the extent
374 * root to add free space. So we skip locking and search the commit
375 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400376 */
377 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400378 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400379 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400380
Yan Zhenge4404d62008-12-12 10:03:26 -0500381 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400382 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400383 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400384again:
Yan Zheng11833d62009-09-11 16:11:19 -0400385 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400386 /* need to make sure the commit_root doesn't disappear */
387 down_read(&fs_info->extent_commit_sem);
388
Yan Zheng11833d62009-09-11 16:11:19 -0400389 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400390 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400391 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500392
Yan Zheng11833d62009-09-11 16:11:19 -0400393 leaf = path->nodes[0];
394 nritems = btrfs_header_nritems(leaf);
395
Chris Masond3977122009-01-05 21:25:51 -0500396 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200397 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400398 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400399 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400400 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400401
Yan Zheng11833d62009-09-11 16:11:19 -0400402 if (path->slots[0] < nritems) {
403 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
404 } else {
405 ret = find_next_key(path, 0, &key);
406 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400407 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400408
Josef Bacik589d8ad2011-05-11 17:30:53 -0400409 if (need_resched() ||
410 btrfs_next_leaf(extent_root, path)) {
411 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400412 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400413 up_read(&fs_info->extent_commit_sem);
414 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400415 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400416 goto again;
417 }
418 leaf = path->nodes[0];
419 nritems = btrfs_header_nritems(leaf);
420 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400421 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400422
Yan Zheng11833d62009-09-11 16:11:19 -0400423 if (key.objectid < block_group->key.objectid) {
424 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400425 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400426 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400427
Chris Masone37c9e62007-05-09 20:13:14 -0400428 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400429 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400430 break;
Yan7d7d6062007-09-14 16:15:28 -0400431
Yan Zheng11833d62009-09-11 16:11:19 -0400432 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400433 total_found += add_new_free_space(block_group,
434 fs_info, last,
435 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400436 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400437
Yan Zheng11833d62009-09-11 16:11:19 -0400438 if (total_found > (1024 * 1024 * 2)) {
439 total_found = 0;
440 wake_up(&caching_ctl->wait);
441 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400442 }
Chris Masone37c9e62007-05-09 20:13:14 -0400443 path->slots[0]++;
444 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400445 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400446
447 total_found += add_new_free_space(block_group, fs_info, last,
448 block_group->key.objectid +
449 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400450 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400451
452 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400453 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400454 block_group->cached = BTRFS_CACHE_FINISHED;
455 spin_unlock(&block_group->lock);
456
Chris Mason54aa1f42007-06-22 14:16:25 -0400457err:
Chris Masone37c9e62007-05-09 20:13:14 -0400458 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400459 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400460
Yan Zheng11833d62009-09-11 16:11:19 -0400461 free_excluded_extents(extent_root, block_group);
462
463 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400464out:
Yan Zheng11833d62009-09-11 16:11:19 -0400465 wake_up(&caching_ctl->wait);
466
467 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000468 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400469}
470
Josef Bacik9d66e232010-08-25 16:54:15 -0400471static int cache_block_group(struct btrfs_block_group_cache *cache,
472 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500473 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400474 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400475{
Josef Bacik291c7d22011-11-14 13:52:14 -0500476 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400477 struct btrfs_fs_info *fs_info = cache->fs_info;
478 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400479 int ret = 0;
480
Josef Bacik291c7d22011-11-14 13:52:14 -0500481 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100482 if (!caching_ctl)
483 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500484
485 INIT_LIST_HEAD(&caching_ctl->list);
486 mutex_init(&caching_ctl->mutex);
487 init_waitqueue_head(&caching_ctl->wait);
488 caching_ctl->block_group = cache;
489 caching_ctl->progress = cache->key.objectid;
490 atomic_set(&caching_ctl->count, 1);
491 caching_ctl->work.func = caching_thread;
492
493 spin_lock(&cache->lock);
494 /*
495 * This should be a rare occasion, but this could happen I think in the
496 * case where one thread starts to load the space cache info, and then
497 * some other thread starts a transaction commit which tries to do an
498 * allocation while the other thread is still loading the space cache
499 * info. The previous loop should have kept us from choosing this block
500 * group, but if we've moved to the state where we will wait on caching
501 * block groups we need to first check if we're doing a fast load here,
502 * so we can wait for it to finish, otherwise we could end up allocating
503 * from a block group who's cache gets evicted for one reason or
504 * another.
505 */
506 while (cache->cached == BTRFS_CACHE_FAST) {
507 struct btrfs_caching_control *ctl;
508
509 ctl = cache->caching_ctl;
510 atomic_inc(&ctl->count);
511 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
512 spin_unlock(&cache->lock);
513
514 schedule();
515
516 finish_wait(&ctl->wait, &wait);
517 put_caching_control(ctl);
518 spin_lock(&cache->lock);
519 }
520
521 if (cache->cached != BTRFS_CACHE_NO) {
522 spin_unlock(&cache->lock);
523 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400524 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500525 }
526 WARN_ON(cache->caching_ctl);
527 cache->caching_ctl = caching_ctl;
528 cache->cached = BTRFS_CACHE_FAST;
529 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400530
Josef Bacik9d66e232010-08-25 16:54:15 -0400531 /*
532 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500533 * to have the normal tree locking. Also if we are currently trying to
534 * allocate blocks for the tree root we can't do the fast caching since
535 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400536 */
Josef Bacikd53ba472012-04-12 16:03:57 -0400537 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400538 ret = load_free_space_cache(fs_info, cache);
539
540 spin_lock(&cache->lock);
541 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500542 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400543 cache->cached = BTRFS_CACHE_FINISHED;
544 cache->last_byte_to_unpin = (u64)-1;
545 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500546 if (load_cache_only) {
547 cache->caching_ctl = NULL;
548 cache->cached = BTRFS_CACHE_NO;
549 } else {
550 cache->cached = BTRFS_CACHE_STARTED;
551 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400552 }
553 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500554 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000555 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500556 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000557 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400558 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000559 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500560 } else {
561 /*
562 * We are not going to do the fast caching, set cached to the
563 * appropriate value and wakeup any waiters.
564 */
565 spin_lock(&cache->lock);
566 if (load_cache_only) {
567 cache->caching_ctl = NULL;
568 cache->cached = BTRFS_CACHE_NO;
569 } else {
570 cache->cached = BTRFS_CACHE_STARTED;
571 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400572 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500573 wake_up(&caching_ctl->wait);
574 }
575
576 if (load_cache_only) {
577 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400578 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400579 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400580
Yan Zheng11833d62009-09-11 16:11:19 -0400581 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500582 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400583 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
584 up_write(&fs_info->extent_commit_sem);
585
Josef Bacik11dfe352009-11-13 20:12:59 +0000586 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400587
Josef Bacikbab39bf2011-06-30 14:42:28 -0400588 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400589
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400590 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400591}
592
Josef Bacik0f9dd462008-09-23 13:14:11 -0400593/*
594 * return the block group that starts at or after bytenr
595 */
Chris Masond3977122009-01-05 21:25:51 -0500596static struct btrfs_block_group_cache *
597btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400598{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400599 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400600
Josef Bacik0f9dd462008-09-23 13:14:11 -0400601 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400602
Josef Bacik0f9dd462008-09-23 13:14:11 -0400603 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400604}
605
Josef Bacik0f9dd462008-09-23 13:14:11 -0400606/*
Sankar P9f556842009-05-14 13:52:22 -0400607 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400608 */
Chris Masond3977122009-01-05 21:25:51 -0500609struct btrfs_block_group_cache *btrfs_lookup_block_group(
610 struct btrfs_fs_info *info,
611 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400612{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400613 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400614
Josef Bacik0f9dd462008-09-23 13:14:11 -0400615 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400616
Josef Bacik0f9dd462008-09-23 13:14:11 -0400617 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400618}
Chris Mason0b86a832008-03-24 15:01:56 -0400619
Josef Bacik0f9dd462008-09-23 13:14:11 -0400620static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
621 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400622{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400623 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400624 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400625
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200626 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400627
Chris Mason4184ea72009-03-10 12:39:20 -0400628 rcu_read_lock();
629 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400630 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400631 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400632 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400633 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400634 }
Chris Mason4184ea72009-03-10 12:39:20 -0400635 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400636 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400637}
638
Chris Mason4184ea72009-03-10 12:39:20 -0400639/*
640 * after adding space to the filesystem, we need to clear the full flags
641 * on all the space infos.
642 */
643void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
644{
645 struct list_head *head = &info->space_info;
646 struct btrfs_space_info *found;
647
648 rcu_read_lock();
649 list_for_each_entry_rcu(found, head, list)
650 found->full = 0;
651 rcu_read_unlock();
652}
653
Yan Zhengd2fb3432008-12-11 16:30:39 -0500654u64 btrfs_find_block_group(struct btrfs_root *root,
655 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400656{
Chris Mason96b51792007-10-15 16:15:19 -0400657 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400658 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500659 u64 last = max(search_hint, search_start);
660 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400661 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500662 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400663 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400664again:
Zheng Yane8569812008-09-26 10:05:48 -0400665 while (1) {
666 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400667 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400668 break;
Chris Mason96b51792007-10-15 16:15:19 -0400669
Chris Masonc286ac42008-07-22 23:06:41 -0400670 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400671 last = cache->key.objectid + cache->key.offset;
672 used = btrfs_block_group_used(&cache->item);
673
Yan Zhengd2fb3432008-12-11 16:30:39 -0500674 if ((full_search || !cache->ro) &&
675 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400676 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500677 div_factor(cache->key.offset, factor)) {
678 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400679 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400680 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400681 goto found;
682 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400683 }
Chris Masonc286ac42008-07-22 23:06:41 -0400684 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400685 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400686 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400687 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400688 if (!wrapped) {
689 last = search_start;
690 wrapped = 1;
691 goto again;
692 }
693 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400694 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400695 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400696 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400697 goto again;
698 }
Chris Masonbe744172007-05-06 10:15:01 -0400699found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500700 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400701}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400702
Chris Masone02119d2008-09-05 16:13:11 -0400703/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400704int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400705{
706 int ret;
707 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400708 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400709
Zheng Yan31840ae2008-09-23 13:14:14 -0400710 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700711 if (!path)
712 return -ENOMEM;
713
Chris Masone02119d2008-09-05 16:13:11 -0400714 key.objectid = start;
715 key.offset = len;
716 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
717 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
718 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400719 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500720 return ret;
721}
722
Chris Masond8d5f3e2007-12-11 12:42:00 -0500723/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400724 * helper function to lookup reference count and flags of extent.
725 *
726 * the head node for delayed ref is used to store the sum of all the
727 * reference count modifications queued up in the rbtree. the head
728 * node may also store the extent flags to set. This way you can check
729 * to see what the reference count and extent flags would be if all of
730 * the delayed refs are not processed.
731 */
732int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
733 struct btrfs_root *root, u64 bytenr,
734 u64 num_bytes, u64 *refs, u64 *flags)
735{
736 struct btrfs_delayed_ref_head *head;
737 struct btrfs_delayed_ref_root *delayed_refs;
738 struct btrfs_path *path;
739 struct btrfs_extent_item *ei;
740 struct extent_buffer *leaf;
741 struct btrfs_key key;
742 u32 item_size;
743 u64 num_refs;
744 u64 extent_flags;
745 int ret;
746
747 path = btrfs_alloc_path();
748 if (!path)
749 return -ENOMEM;
750
751 key.objectid = bytenr;
752 key.type = BTRFS_EXTENT_ITEM_KEY;
753 key.offset = num_bytes;
754 if (!trans) {
755 path->skip_locking = 1;
756 path->search_commit_root = 1;
757 }
758again:
759 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
760 &key, path, 0, 0);
761 if (ret < 0)
762 goto out_free;
763
764 if (ret == 0) {
765 leaf = path->nodes[0];
766 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
767 if (item_size >= sizeof(*ei)) {
768 ei = btrfs_item_ptr(leaf, path->slots[0],
769 struct btrfs_extent_item);
770 num_refs = btrfs_extent_refs(leaf, ei);
771 extent_flags = btrfs_extent_flags(leaf, ei);
772 } else {
773#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
774 struct btrfs_extent_item_v0 *ei0;
775 BUG_ON(item_size != sizeof(*ei0));
776 ei0 = btrfs_item_ptr(leaf, path->slots[0],
777 struct btrfs_extent_item_v0);
778 num_refs = btrfs_extent_refs_v0(leaf, ei0);
779 /* FIXME: this isn't correct for data */
780 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
781#else
782 BUG();
783#endif
784 }
785 BUG_ON(num_refs == 0);
786 } else {
787 num_refs = 0;
788 extent_flags = 0;
789 ret = 0;
790 }
791
792 if (!trans)
793 goto out;
794
795 delayed_refs = &trans->transaction->delayed_refs;
796 spin_lock(&delayed_refs->lock);
797 head = btrfs_find_delayed_ref_head(trans, bytenr);
798 if (head) {
799 if (!mutex_trylock(&head->mutex)) {
800 atomic_inc(&head->node.refs);
801 spin_unlock(&delayed_refs->lock);
802
David Sterbab3b4aa72011-04-21 01:20:15 +0200803 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400804
David Sterba8cc33e52011-05-02 15:29:25 +0200805 /*
806 * Mutex was contended, block until it's released and try
807 * again
808 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400809 mutex_lock(&head->mutex);
810 mutex_unlock(&head->mutex);
811 btrfs_put_delayed_ref(&head->node);
812 goto again;
813 }
814 if (head->extent_op && head->extent_op->update_flags)
815 extent_flags |= head->extent_op->flags_to_set;
816 else
817 BUG_ON(num_refs == 0);
818
819 num_refs += head->node.ref_mod;
820 mutex_unlock(&head->mutex);
821 }
822 spin_unlock(&delayed_refs->lock);
823out:
824 WARN_ON(num_refs == 0);
825 if (refs)
826 *refs = num_refs;
827 if (flags)
828 *flags = extent_flags;
829out_free:
830 btrfs_free_path(path);
831 return ret;
832}
833
834/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500835 * Back reference rules. Back refs have three main goals:
836 *
837 * 1) differentiate between all holders of references to an extent so that
838 * when a reference is dropped we can make sure it was a valid reference
839 * before freeing the extent.
840 *
841 * 2) Provide enough information to quickly find the holders of an extent
842 * if we notice a given block is corrupted or bad.
843 *
844 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
845 * maintenance. This is actually the same as #2, but with a slightly
846 * different use case.
847 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400848 * There are two kinds of back refs. The implicit back refs is optimized
849 * for pointers in non-shared tree blocks. For a given pointer in a block,
850 * back refs of this kind provide information about the block's owner tree
851 * and the pointer's key. These information allow us to find the block by
852 * b-tree searching. The full back refs is for pointers in tree blocks not
853 * referenced by their owner trees. The location of tree block is recorded
854 * in the back refs. Actually the full back refs is generic, and can be
855 * used in all cases the implicit back refs is used. The major shortcoming
856 * of the full back refs is its overhead. Every time a tree block gets
857 * COWed, we have to update back refs entry for all pointers in it.
858 *
859 * For a newly allocated tree block, we use implicit back refs for
860 * pointers in it. This means most tree related operations only involve
861 * implicit back refs. For a tree block created in old transaction, the
862 * only way to drop a reference to it is COW it. So we can detect the
863 * event that tree block loses its owner tree's reference and do the
864 * back refs conversion.
865 *
866 * When a tree block is COW'd through a tree, there are four cases:
867 *
868 * The reference count of the block is one and the tree is the block's
869 * owner tree. Nothing to do in this case.
870 *
871 * The reference count of the block is one and the tree is not the
872 * block's owner tree. In this case, full back refs is used for pointers
873 * in the block. Remove these full back refs, add implicit back refs for
874 * every pointers in the new block.
875 *
876 * The reference count of the block is greater than one and the tree is
877 * the block's owner tree. In this case, implicit back refs is used for
878 * pointers in the block. Add full back refs for every pointers in the
879 * block, increase lower level extents' reference counts. The original
880 * implicit back refs are entailed to the new block.
881 *
882 * The reference count of the block is greater than one and the tree is
883 * not the block's owner tree. Add implicit back refs for every pointer in
884 * the new block, increase lower level extents' reference count.
885 *
886 * Back Reference Key composing:
887 *
888 * The key objectid corresponds to the first byte in the extent,
889 * The key type is used to differentiate between types of back refs.
890 * There are different meanings of the key offset for different types
891 * of back refs.
892 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500893 * File extents can be referenced by:
894 *
895 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400896 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500897 * - different offsets inside a file (bookend extents in file.c)
898 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500900 *
901 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500902 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400903 * - original offset in the file
904 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400905 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 * The key offset for the implicit back refs is hash of the first
907 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500908 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400909 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500910 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500912 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 * The key offset for the implicit back refs is the first byte of
914 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500915 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 * When a file extent is allocated, The implicit back refs is used.
917 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500918 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919 * (root_key.objectid, inode objectid, offset in file, 1)
920 *
921 * When a file extent is removed file truncation, we find the
922 * corresponding implicit back refs and check the following fields:
923 *
924 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500925 *
926 * Btree extents can be referenced by:
927 *
928 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500929 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400930 * Both the implicit back refs and the full back refs for tree blocks
931 * only consist of key. The key offset for the implicit back refs is
932 * objectid of block's owner tree. The key offset for the full back refs
933 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500934 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400935 * When implicit back refs is used, information about the lowest key and
936 * level of the tree block are required. These information are stored in
937 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500938 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400939
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400940#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
941static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
942 struct btrfs_root *root,
943 struct btrfs_path *path,
944 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500945{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 struct btrfs_extent_item *item;
947 struct btrfs_extent_item_v0 *ei0;
948 struct btrfs_extent_ref_v0 *ref0;
949 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400950 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400951 struct btrfs_key key;
952 struct btrfs_key found_key;
953 u32 new_size = sizeof(*item);
954 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500955 int ret;
956
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957 leaf = path->nodes[0];
958 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500959
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400960 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
961 ei0 = btrfs_item_ptr(leaf, path->slots[0],
962 struct btrfs_extent_item_v0);
963 refs = btrfs_extent_refs_v0(leaf, ei0);
964
965 if (owner == (u64)-1) {
966 while (1) {
967 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
968 ret = btrfs_next_leaf(root, path);
969 if (ret < 0)
970 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100971 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400972 leaf = path->nodes[0];
973 }
974 btrfs_item_key_to_cpu(leaf, &found_key,
975 path->slots[0]);
976 BUG_ON(key.objectid != found_key.objectid);
977 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
978 path->slots[0]++;
979 continue;
980 }
981 ref0 = btrfs_item_ptr(leaf, path->slots[0],
982 struct btrfs_extent_ref_v0);
983 owner = btrfs_ref_objectid_v0(leaf, ref0);
984 break;
985 }
986 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200987 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988
989 if (owner < BTRFS_FIRST_FREE_OBJECTID)
990 new_size += sizeof(*bi);
991
992 new_size -= sizeof(*ei0);
993 ret = btrfs_search_slot(trans, root, &key, path,
994 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400995 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400996 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100997 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400998
Jeff Mahoney143bede2012-03-01 14:56:26 +0100999 btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001000
1001 leaf = path->nodes[0];
1002 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1003 btrfs_set_extent_refs(leaf, item, refs);
1004 /* FIXME: get real generation */
1005 btrfs_set_extent_generation(leaf, item, 0);
1006 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1007 btrfs_set_extent_flags(leaf, item,
1008 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1009 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1010 bi = (struct btrfs_tree_block_info *)(item + 1);
1011 /* FIXME: get first key of the block */
1012 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1013 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1014 } else {
1015 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1016 }
1017 btrfs_mark_buffer_dirty(leaf);
1018 return 0;
1019}
1020#endif
1021
1022static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1023{
1024 u32 high_crc = ~(u32)0;
1025 u32 low_crc = ~(u32)0;
1026 __le64 lenum;
1027
1028 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001029 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001031 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001032 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001033 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001034
1035 return ((u64)high_crc << 31) ^ (u64)low_crc;
1036}
1037
1038static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1039 struct btrfs_extent_data_ref *ref)
1040{
1041 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1042 btrfs_extent_data_ref_objectid(leaf, ref),
1043 btrfs_extent_data_ref_offset(leaf, ref));
1044}
1045
1046static int match_extent_data_ref(struct extent_buffer *leaf,
1047 struct btrfs_extent_data_ref *ref,
1048 u64 root_objectid, u64 owner, u64 offset)
1049{
1050 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1051 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1052 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1053 return 0;
1054 return 1;
1055}
1056
1057static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1058 struct btrfs_root *root,
1059 struct btrfs_path *path,
1060 u64 bytenr, u64 parent,
1061 u64 root_objectid,
1062 u64 owner, u64 offset)
1063{
1064 struct btrfs_key key;
1065 struct btrfs_extent_data_ref *ref;
1066 struct extent_buffer *leaf;
1067 u32 nritems;
1068 int ret;
1069 int recow;
1070 int err = -ENOENT;
1071
1072 key.objectid = bytenr;
1073 if (parent) {
1074 key.type = BTRFS_SHARED_DATA_REF_KEY;
1075 key.offset = parent;
1076 } else {
1077 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1078 key.offset = hash_extent_data_ref(root_objectid,
1079 owner, offset);
1080 }
1081again:
1082 recow = 0;
1083 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1084 if (ret < 0) {
1085 err = ret;
1086 goto fail;
1087 }
1088
1089 if (parent) {
1090 if (!ret)
1091 return 0;
1092#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1093 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001094 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1096 if (ret < 0) {
1097 err = ret;
1098 goto fail;
1099 }
1100 if (!ret)
1101 return 0;
1102#endif
1103 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001104 }
1105
1106 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001107 nritems = btrfs_header_nritems(leaf);
1108 while (1) {
1109 if (path->slots[0] >= nritems) {
1110 ret = btrfs_next_leaf(root, path);
1111 if (ret < 0)
1112 err = ret;
1113 if (ret)
1114 goto fail;
1115
1116 leaf = path->nodes[0];
1117 nritems = btrfs_header_nritems(leaf);
1118 recow = 1;
1119 }
1120
1121 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1122 if (key.objectid != bytenr ||
1123 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1124 goto fail;
1125
1126 ref = btrfs_item_ptr(leaf, path->slots[0],
1127 struct btrfs_extent_data_ref);
1128
1129 if (match_extent_data_ref(leaf, ref, root_objectid,
1130 owner, offset)) {
1131 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001132 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001133 goto again;
1134 }
1135 err = 0;
1136 break;
1137 }
1138 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001139 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001140fail:
1141 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001142}
1143
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001144static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1145 struct btrfs_root *root,
1146 struct btrfs_path *path,
1147 u64 bytenr, u64 parent,
1148 u64 root_objectid, u64 owner,
1149 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001150{
1151 struct btrfs_key key;
1152 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001153 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001154 u32 num_refs;
1155 int ret;
1156
1157 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158 if (parent) {
1159 key.type = BTRFS_SHARED_DATA_REF_KEY;
1160 key.offset = parent;
1161 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001162 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1164 key.offset = hash_extent_data_ref(root_objectid,
1165 owner, offset);
1166 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001167 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001168
1169 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1170 if (ret && ret != -EEXIST)
1171 goto fail;
1172
1173 leaf = path->nodes[0];
1174 if (parent) {
1175 struct btrfs_shared_data_ref *ref;
1176 ref = btrfs_item_ptr(leaf, path->slots[0],
1177 struct btrfs_shared_data_ref);
1178 if (ret == 0) {
1179 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1180 } else {
1181 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1182 num_refs += refs_to_add;
1183 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1184 }
1185 } else {
1186 struct btrfs_extent_data_ref *ref;
1187 while (ret == -EEXIST) {
1188 ref = btrfs_item_ptr(leaf, path->slots[0],
1189 struct btrfs_extent_data_ref);
1190 if (match_extent_data_ref(leaf, ref, root_objectid,
1191 owner, offset))
1192 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001193 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194 key.offset++;
1195 ret = btrfs_insert_empty_item(trans, root, path, &key,
1196 size);
1197 if (ret && ret != -EEXIST)
1198 goto fail;
1199
1200 leaf = path->nodes[0];
1201 }
1202 ref = btrfs_item_ptr(leaf, path->slots[0],
1203 struct btrfs_extent_data_ref);
1204 if (ret == 0) {
1205 btrfs_set_extent_data_ref_root(leaf, ref,
1206 root_objectid);
1207 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1208 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1209 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1210 } else {
1211 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1212 num_refs += refs_to_add;
1213 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1214 }
1215 }
1216 btrfs_mark_buffer_dirty(leaf);
1217 ret = 0;
1218fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001219 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001220 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001221}
1222
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001223static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1224 struct btrfs_root *root,
1225 struct btrfs_path *path,
1226 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001227{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 struct btrfs_key key;
1229 struct btrfs_extent_data_ref *ref1 = NULL;
1230 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001231 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001232 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001233 int ret = 0;
1234
1235 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001236 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1237
1238 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1239 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_extent_data_ref);
1241 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1242 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1243 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1244 struct btrfs_shared_data_ref);
1245 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1246#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1247 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1248 struct btrfs_extent_ref_v0 *ref0;
1249 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1250 struct btrfs_extent_ref_v0);
1251 num_refs = btrfs_ref_count_v0(leaf, ref0);
1252#endif
1253 } else {
1254 BUG();
1255 }
1256
Chris Mason56bec292009-03-13 10:10:06 -04001257 BUG_ON(num_refs < refs_to_drop);
1258 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001259
Zheng Yan31840ae2008-09-23 13:14:14 -04001260 if (num_refs == 0) {
1261 ret = btrfs_del_item(trans, root, path);
1262 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001263 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1264 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1265 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1266 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1267#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1268 else {
1269 struct btrfs_extent_ref_v0 *ref0;
1270 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1271 struct btrfs_extent_ref_v0);
1272 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1273 }
1274#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001275 btrfs_mark_buffer_dirty(leaf);
1276 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001277 return ret;
1278}
1279
1280static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1281 struct btrfs_path *path,
1282 struct btrfs_extent_inline_ref *iref)
1283{
1284 struct btrfs_key key;
1285 struct extent_buffer *leaf;
1286 struct btrfs_extent_data_ref *ref1;
1287 struct btrfs_shared_data_ref *ref2;
1288 u32 num_refs = 0;
1289
1290 leaf = path->nodes[0];
1291 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1292 if (iref) {
1293 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1294 BTRFS_EXTENT_DATA_REF_KEY) {
1295 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1296 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1297 } else {
1298 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1299 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1300 }
1301 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1302 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1303 struct btrfs_extent_data_ref);
1304 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1305 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1306 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1307 struct btrfs_shared_data_ref);
1308 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1309#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1310 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1311 struct btrfs_extent_ref_v0 *ref0;
1312 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1313 struct btrfs_extent_ref_v0);
1314 num_refs = btrfs_ref_count_v0(leaf, ref0);
1315#endif
1316 } else {
1317 WARN_ON(1);
1318 }
1319 return num_refs;
1320}
1321
1322static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1323 struct btrfs_root *root,
1324 struct btrfs_path *path,
1325 u64 bytenr, u64 parent,
1326 u64 root_objectid)
1327{
1328 struct btrfs_key key;
1329 int ret;
1330
1331 key.objectid = bytenr;
1332 if (parent) {
1333 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1334 key.offset = parent;
1335 } else {
1336 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1337 key.offset = root_objectid;
1338 }
1339
1340 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1341 if (ret > 0)
1342 ret = -ENOENT;
1343#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1344 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001345 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346 key.type = BTRFS_EXTENT_REF_V0_KEY;
1347 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1348 if (ret > 0)
1349 ret = -ENOENT;
1350 }
1351#endif
1352 return ret;
1353}
1354
1355static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1356 struct btrfs_root *root,
1357 struct btrfs_path *path,
1358 u64 bytenr, u64 parent,
1359 u64 root_objectid)
1360{
1361 struct btrfs_key key;
1362 int ret;
1363
1364 key.objectid = bytenr;
1365 if (parent) {
1366 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1367 key.offset = parent;
1368 } else {
1369 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1370 key.offset = root_objectid;
1371 }
1372
1373 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001374 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001375 return ret;
1376}
1377
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378static inline int extent_ref_type(u64 parent, u64 owner)
1379{
1380 int type;
1381 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1382 if (parent > 0)
1383 type = BTRFS_SHARED_BLOCK_REF_KEY;
1384 else
1385 type = BTRFS_TREE_BLOCK_REF_KEY;
1386 } else {
1387 if (parent > 0)
1388 type = BTRFS_SHARED_DATA_REF_KEY;
1389 else
1390 type = BTRFS_EXTENT_DATA_REF_KEY;
1391 }
1392 return type;
1393}
1394
Yan Zheng2c47e6052009-06-27 21:07:35 -04001395static int find_next_key(struct btrfs_path *path, int level,
1396 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397
1398{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001399 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001400 if (!path->nodes[level])
1401 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001402 if (path->slots[level] + 1 >=
1403 btrfs_header_nritems(path->nodes[level]))
1404 continue;
1405 if (level == 0)
1406 btrfs_item_key_to_cpu(path->nodes[level], key,
1407 path->slots[level] + 1);
1408 else
1409 btrfs_node_key_to_cpu(path->nodes[level], key,
1410 path->slots[level] + 1);
1411 return 0;
1412 }
1413 return 1;
1414}
1415
1416/*
1417 * look for inline back ref. if back ref is found, *ref_ret is set
1418 * to the address of inline back ref, and 0 is returned.
1419 *
1420 * if back ref isn't found, *ref_ret is set to the address where it
1421 * should be inserted, and -ENOENT is returned.
1422 *
1423 * if insert is true and there are too many inline back refs, the path
1424 * points to the extent item, and -EAGAIN is returned.
1425 *
1426 * NOTE: inline back refs are ordered in the same way that back ref
1427 * items in the tree are ordered.
1428 */
1429static noinline_for_stack
1430int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1431 struct btrfs_root *root,
1432 struct btrfs_path *path,
1433 struct btrfs_extent_inline_ref **ref_ret,
1434 u64 bytenr, u64 num_bytes,
1435 u64 parent, u64 root_objectid,
1436 u64 owner, u64 offset, int insert)
1437{
1438 struct btrfs_key key;
1439 struct extent_buffer *leaf;
1440 struct btrfs_extent_item *ei;
1441 struct btrfs_extent_inline_ref *iref;
1442 u64 flags;
1443 u64 item_size;
1444 unsigned long ptr;
1445 unsigned long end;
1446 int extra_size;
1447 int type;
1448 int want;
1449 int ret;
1450 int err = 0;
1451
1452 key.objectid = bytenr;
1453 key.type = BTRFS_EXTENT_ITEM_KEY;
1454 key.offset = num_bytes;
1455
1456 want = extent_ref_type(parent, owner);
1457 if (insert) {
1458 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001459 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001460 } else
1461 extra_size = -1;
1462 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1463 if (ret < 0) {
1464 err = ret;
1465 goto out;
1466 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001467 if (ret && !insert) {
1468 err = -ENOENT;
1469 goto out;
1470 }
1471 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001472
1473 leaf = path->nodes[0];
1474 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1475#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1476 if (item_size < sizeof(*ei)) {
1477 if (!insert) {
1478 err = -ENOENT;
1479 goto out;
1480 }
1481 ret = convert_extent_item_v0(trans, root, path, owner,
1482 extra_size);
1483 if (ret < 0) {
1484 err = ret;
1485 goto out;
1486 }
1487 leaf = path->nodes[0];
1488 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1489 }
1490#endif
1491 BUG_ON(item_size < sizeof(*ei));
1492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1494 flags = btrfs_extent_flags(leaf, ei);
1495
1496 ptr = (unsigned long)(ei + 1);
1497 end = (unsigned long)ei + item_size;
1498
1499 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1500 ptr += sizeof(struct btrfs_tree_block_info);
1501 BUG_ON(ptr > end);
1502 } else {
1503 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1504 }
1505
1506 err = -ENOENT;
1507 while (1) {
1508 if (ptr >= end) {
1509 WARN_ON(ptr > end);
1510 break;
1511 }
1512 iref = (struct btrfs_extent_inline_ref *)ptr;
1513 type = btrfs_extent_inline_ref_type(leaf, iref);
1514 if (want < type)
1515 break;
1516 if (want > type) {
1517 ptr += btrfs_extent_inline_ref_size(type);
1518 continue;
1519 }
1520
1521 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1522 struct btrfs_extent_data_ref *dref;
1523 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1524 if (match_extent_data_ref(leaf, dref, root_objectid,
1525 owner, offset)) {
1526 err = 0;
1527 break;
1528 }
1529 if (hash_extent_data_ref_item(leaf, dref) <
1530 hash_extent_data_ref(root_objectid, owner, offset))
1531 break;
1532 } else {
1533 u64 ref_offset;
1534 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1535 if (parent > 0) {
1536 if (parent == ref_offset) {
1537 err = 0;
1538 break;
1539 }
1540 if (ref_offset < parent)
1541 break;
1542 } else {
1543 if (root_objectid == ref_offset) {
1544 err = 0;
1545 break;
1546 }
1547 if (ref_offset < root_objectid)
1548 break;
1549 }
1550 }
1551 ptr += btrfs_extent_inline_ref_size(type);
1552 }
1553 if (err == -ENOENT && insert) {
1554 if (item_size + extra_size >=
1555 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1556 err = -EAGAIN;
1557 goto out;
1558 }
1559 /*
1560 * To add new inline back ref, we have to make sure
1561 * there is no corresponding back ref item.
1562 * For simplicity, we just do not add new inline back
1563 * ref if there is any kind of item for this block
1564 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001565 if (find_next_key(path, 0, &key) == 0 &&
1566 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001567 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001568 err = -EAGAIN;
1569 goto out;
1570 }
1571 }
1572 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1573out:
Yan Zheng85d41982009-06-11 08:51:10 -04001574 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001575 path->keep_locks = 0;
1576 btrfs_unlock_up_safe(path, 1);
1577 }
1578 return err;
1579}
1580
1581/*
1582 * helper to add new inline back ref
1583 */
1584static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001585void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1586 struct btrfs_root *root,
1587 struct btrfs_path *path,
1588 struct btrfs_extent_inline_ref *iref,
1589 u64 parent, u64 root_objectid,
1590 u64 owner, u64 offset, int refs_to_add,
1591 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001592{
1593 struct extent_buffer *leaf;
1594 struct btrfs_extent_item *ei;
1595 unsigned long ptr;
1596 unsigned long end;
1597 unsigned long item_offset;
1598 u64 refs;
1599 int size;
1600 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001601
1602 leaf = path->nodes[0];
1603 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1604 item_offset = (unsigned long)iref - (unsigned long)ei;
1605
1606 type = extent_ref_type(parent, owner);
1607 size = btrfs_extent_inline_ref_size(type);
1608
Jeff Mahoney143bede2012-03-01 14:56:26 +01001609 btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001610
1611 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1612 refs = btrfs_extent_refs(leaf, ei);
1613 refs += refs_to_add;
1614 btrfs_set_extent_refs(leaf, ei, refs);
1615 if (extent_op)
1616 __run_delayed_extent_op(extent_op, leaf, ei);
1617
1618 ptr = (unsigned long)ei + item_offset;
1619 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1620 if (ptr < end - size)
1621 memmove_extent_buffer(leaf, ptr + size, ptr,
1622 end - size - ptr);
1623
1624 iref = (struct btrfs_extent_inline_ref *)ptr;
1625 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1626 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1627 struct btrfs_extent_data_ref *dref;
1628 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1629 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1630 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1631 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1632 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1633 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1634 struct btrfs_shared_data_ref *sref;
1635 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1636 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1637 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1638 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1639 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1640 } else {
1641 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1642 }
1643 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644}
1645
1646static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1647 struct btrfs_root *root,
1648 struct btrfs_path *path,
1649 struct btrfs_extent_inline_ref **ref_ret,
1650 u64 bytenr, u64 num_bytes, u64 parent,
1651 u64 root_objectid, u64 owner, u64 offset)
1652{
1653 int ret;
1654
1655 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1656 bytenr, num_bytes, parent,
1657 root_objectid, owner, offset, 0);
1658 if (ret != -ENOENT)
1659 return ret;
1660
David Sterbab3b4aa72011-04-21 01:20:15 +02001661 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662 *ref_ret = NULL;
1663
1664 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1665 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1666 root_objectid);
1667 } else {
1668 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1669 root_objectid, owner, offset);
1670 }
1671 return ret;
1672}
1673
1674/*
1675 * helper to update/remove inline back ref
1676 */
1677static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001678void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1679 struct btrfs_root *root,
1680 struct btrfs_path *path,
1681 struct btrfs_extent_inline_ref *iref,
1682 int refs_to_mod,
1683 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001684{
1685 struct extent_buffer *leaf;
1686 struct btrfs_extent_item *ei;
1687 struct btrfs_extent_data_ref *dref = NULL;
1688 struct btrfs_shared_data_ref *sref = NULL;
1689 unsigned long ptr;
1690 unsigned long end;
1691 u32 item_size;
1692 int size;
1693 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 u64 refs;
1695
1696 leaf = path->nodes[0];
1697 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1698 refs = btrfs_extent_refs(leaf, ei);
1699 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1700 refs += refs_to_mod;
1701 btrfs_set_extent_refs(leaf, ei, refs);
1702 if (extent_op)
1703 __run_delayed_extent_op(extent_op, leaf, ei);
1704
1705 type = btrfs_extent_inline_ref_type(leaf, iref);
1706
1707 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1708 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1709 refs = btrfs_extent_data_ref_count(leaf, dref);
1710 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1711 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1712 refs = btrfs_shared_data_ref_count(leaf, sref);
1713 } else {
1714 refs = 1;
1715 BUG_ON(refs_to_mod != -1);
1716 }
1717
1718 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1719 refs += refs_to_mod;
1720
1721 if (refs > 0) {
1722 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1723 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1724 else
1725 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1726 } else {
1727 size = btrfs_extent_inline_ref_size(type);
1728 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1729 ptr = (unsigned long)iref;
1730 end = (unsigned long)ei + item_size;
1731 if (ptr + size < end)
1732 memmove_extent_buffer(leaf, ptr, ptr + size,
1733 end - ptr - size);
1734 item_size -= size;
Jeff Mahoney143bede2012-03-01 14:56:26 +01001735 btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001736 }
1737 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001738}
1739
1740static noinline_for_stack
1741int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1742 struct btrfs_root *root,
1743 struct btrfs_path *path,
1744 u64 bytenr, u64 num_bytes, u64 parent,
1745 u64 root_objectid, u64 owner,
1746 u64 offset, int refs_to_add,
1747 struct btrfs_delayed_extent_op *extent_op)
1748{
1749 struct btrfs_extent_inline_ref *iref;
1750 int ret;
1751
1752 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1753 bytenr, num_bytes, parent,
1754 root_objectid, owner, offset, 1);
1755 if (ret == 0) {
1756 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001757 update_inline_extent_backref(trans, root, path, iref,
1758 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001759 } else if (ret == -ENOENT) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001760 setup_inline_extent_backref(trans, root, path, iref, parent,
1761 root_objectid, owner, offset,
1762 refs_to_add, extent_op);
1763 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001764 }
1765 return ret;
1766}
1767
1768static int insert_extent_backref(struct btrfs_trans_handle *trans,
1769 struct btrfs_root *root,
1770 struct btrfs_path *path,
1771 u64 bytenr, u64 parent, u64 root_objectid,
1772 u64 owner, u64 offset, int refs_to_add)
1773{
1774 int ret;
1775 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1776 BUG_ON(refs_to_add != 1);
1777 ret = insert_tree_block_ref(trans, root, path, bytenr,
1778 parent, root_objectid);
1779 } else {
1780 ret = insert_extent_data_ref(trans, root, path, bytenr,
1781 parent, root_objectid,
1782 owner, offset, refs_to_add);
1783 }
1784 return ret;
1785}
1786
1787static int remove_extent_backref(struct btrfs_trans_handle *trans,
1788 struct btrfs_root *root,
1789 struct btrfs_path *path,
1790 struct btrfs_extent_inline_ref *iref,
1791 int refs_to_drop, int is_data)
1792{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001793 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001794
1795 BUG_ON(!is_data && refs_to_drop != 1);
1796 if (iref) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001797 update_inline_extent_backref(trans, root, path, iref,
1798 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001799 } else if (is_data) {
1800 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1801 } else {
1802 ret = btrfs_del_item(trans, root, path);
1803 }
1804 return ret;
1805}
1806
Li Dongyang5378e602011-03-24 10:24:27 +00001807static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001808 u64 start, u64 len)
1809{
Li Dongyang5378e602011-03-24 10:24:27 +00001810 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001811}
Chris Mason15916de2008-11-19 21:17:22 -05001812
Liu Hui1f3c79a2009-01-05 15:57:51 -05001813static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001814 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001815{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001816 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001817 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001818 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001819
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001820
Liu Hui1f3c79a2009-01-05 15:57:51 -05001821 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001822 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001823 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001824 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001825 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001826 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001827 int i;
1828
Liu Hui1f3c79a2009-01-05 15:57:51 -05001829
Jan Schmidta1d3c472011-08-04 17:15:33 +02001830 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001831 if (!stripe->dev->can_discard)
1832 continue;
1833
Li Dongyang5378e602011-03-24 10:24:27 +00001834 ret = btrfs_issue_discard(stripe->dev->bdev,
1835 stripe->physical,
1836 stripe->length);
1837 if (!ret)
1838 discarded_bytes += stripe->length;
1839 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001840 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001841
1842 /*
1843 * Just in case we get back EOPNOTSUPP for some reason,
1844 * just ignore the return value so we don't screw up
1845 * people calling discard_extent.
1846 */
1847 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001848 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001849 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001850 }
Li Dongyang5378e602011-03-24 10:24:27 +00001851
1852 if (actual_bytes)
1853 *actual_bytes = discarded_bytes;
1854
Liu Hui1f3c79a2009-01-05 15:57:51 -05001855
David Woodhouse53b381b2013-01-29 18:40:14 -05001856 if (ret == -EOPNOTSUPP)
1857 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001858 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001859}
1860
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001861/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001862int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1863 struct btrfs_root *root,
1864 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001865 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001866{
1867 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001868 struct btrfs_fs_info *fs_info = root->fs_info;
1869
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1871 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001872
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001874 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1875 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001877 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001879 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1880 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001882 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001884 return ret;
1885}
1886
Chris Mason925baed2008-06-25 16:01:30 -04001887static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001888 struct btrfs_root *root,
1889 u64 bytenr, u64 num_bytes,
1890 u64 parent, u64 root_objectid,
1891 u64 owner, u64 offset, int refs_to_add,
1892 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001893{
Chris Mason5caf2a02007-04-02 11:20:42 -04001894 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001895 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001896 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001897 u64 refs;
1898 int ret;
1899 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001900
Chris Mason5caf2a02007-04-02 11:20:42 -04001901 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001902 if (!path)
1903 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001904
Chris Mason3c12ac72008-04-21 12:01:38 -04001905 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001906 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001907 /* this will setup the path even if it fails to insert the back ref */
1908 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1909 path, bytenr, num_bytes, parent,
1910 root_objectid, owner, offset,
1911 refs_to_add, extent_op);
1912 if (ret == 0)
1913 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001914
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001915 if (ret != -EAGAIN) {
1916 err = ret;
1917 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001918 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001919
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001920 leaf = path->nodes[0];
1921 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1922 refs = btrfs_extent_refs(leaf, item);
1923 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1924 if (extent_op)
1925 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001926
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001927 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001928 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001929
Chris Mason3c12ac72008-04-21 12:01:38 -04001930 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001931 path->leave_spinning = 1;
1932
Chris Mason56bec292009-03-13 10:10:06 -04001933 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001934 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001935 path, bytenr, parent, root_objectid,
1936 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001937 if (ret)
1938 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001939out:
Chris Mason74493f72007-12-11 09:25:06 -05001940 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001941 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001942}
1943
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001944static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1945 struct btrfs_root *root,
1946 struct btrfs_delayed_ref_node *node,
1947 struct btrfs_delayed_extent_op *extent_op,
1948 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001949{
Chris Mason56bec292009-03-13 10:10:06 -04001950 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001951 struct btrfs_delayed_data_ref *ref;
1952 struct btrfs_key ins;
1953 u64 parent = 0;
1954 u64 ref_root = 0;
1955 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001956
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001957 ins.objectid = node->bytenr;
1958 ins.offset = node->num_bytes;
1959 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001960
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001961 ref = btrfs_delayed_node_to_data_ref(node);
1962 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1963 parent = ref->parent;
1964 else
1965 ref_root = ref->root;
1966
1967 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1968 if (extent_op) {
1969 BUG_ON(extent_op->update_key);
1970 flags |= extent_op->flags_to_set;
1971 }
1972 ret = alloc_reserved_file_extent(trans, root,
1973 parent, ref_root, flags,
1974 ref->objectid, ref->offset,
1975 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1977 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1978 node->num_bytes, parent,
1979 ref_root, ref->objectid,
1980 ref->offset, node->ref_mod,
1981 extent_op);
1982 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1983 ret = __btrfs_free_extent(trans, root, node->bytenr,
1984 node->num_bytes, parent,
1985 ref_root, ref->objectid,
1986 ref->offset, node->ref_mod,
1987 extent_op);
1988 } else {
1989 BUG();
1990 }
Chris Mason56bec292009-03-13 10:10:06 -04001991 return ret;
1992}
1993
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001994static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1995 struct extent_buffer *leaf,
1996 struct btrfs_extent_item *ei)
1997{
1998 u64 flags = btrfs_extent_flags(leaf, ei);
1999 if (extent_op->update_flags) {
2000 flags |= extent_op->flags_to_set;
2001 btrfs_set_extent_flags(leaf, ei, flags);
2002 }
2003
2004 if (extent_op->update_key) {
2005 struct btrfs_tree_block_info *bi;
2006 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2007 bi = (struct btrfs_tree_block_info *)(ei + 1);
2008 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2009 }
2010}
2011
2012static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2013 struct btrfs_root *root,
2014 struct btrfs_delayed_ref_node *node,
2015 struct btrfs_delayed_extent_op *extent_op)
2016{
2017 struct btrfs_key key;
2018 struct btrfs_path *path;
2019 struct btrfs_extent_item *ei;
2020 struct extent_buffer *leaf;
2021 u32 item_size;
2022 int ret;
2023 int err = 0;
2024
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002025 if (trans->aborted)
2026 return 0;
2027
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002028 path = btrfs_alloc_path();
2029 if (!path)
2030 return -ENOMEM;
2031
2032 key.objectid = node->bytenr;
2033 key.type = BTRFS_EXTENT_ITEM_KEY;
2034 key.offset = node->num_bytes;
2035
2036 path->reada = 1;
2037 path->leave_spinning = 1;
2038 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2039 path, 0, 1);
2040 if (ret < 0) {
2041 err = ret;
2042 goto out;
2043 }
2044 if (ret > 0) {
2045 err = -EIO;
2046 goto out;
2047 }
2048
2049 leaf = path->nodes[0];
2050 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2051#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2052 if (item_size < sizeof(*ei)) {
2053 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2054 path, (u64)-1, 0);
2055 if (ret < 0) {
2056 err = ret;
2057 goto out;
2058 }
2059 leaf = path->nodes[0];
2060 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2061 }
2062#endif
2063 BUG_ON(item_size < sizeof(*ei));
2064 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2065 __run_delayed_extent_op(extent_op, leaf, ei);
2066
2067 btrfs_mark_buffer_dirty(leaf);
2068out:
2069 btrfs_free_path(path);
2070 return err;
2071}
2072
2073static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2074 struct btrfs_root *root,
2075 struct btrfs_delayed_ref_node *node,
2076 struct btrfs_delayed_extent_op *extent_op,
2077 int insert_reserved)
2078{
2079 int ret = 0;
2080 struct btrfs_delayed_tree_ref *ref;
2081 struct btrfs_key ins;
2082 u64 parent = 0;
2083 u64 ref_root = 0;
2084
2085 ins.objectid = node->bytenr;
2086 ins.offset = node->num_bytes;
2087 ins.type = BTRFS_EXTENT_ITEM_KEY;
2088
2089 ref = btrfs_delayed_node_to_tree_ref(node);
2090 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2091 parent = ref->parent;
2092 else
2093 ref_root = ref->root;
2094
2095 BUG_ON(node->ref_mod != 1);
2096 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2097 BUG_ON(!extent_op || !extent_op->update_flags ||
2098 !extent_op->update_key);
2099 ret = alloc_reserved_tree_block(trans, root,
2100 parent, ref_root,
2101 extent_op->flags_to_set,
2102 &extent_op->key,
2103 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002104 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2105 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2106 node->num_bytes, parent, ref_root,
2107 ref->level, 0, 1, extent_op);
2108 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2109 ret = __btrfs_free_extent(trans, root, node->bytenr,
2110 node->num_bytes, parent, ref_root,
2111 ref->level, 0, 1, extent_op);
2112 } else {
2113 BUG();
2114 }
2115 return ret;
2116}
2117
Chris Mason56bec292009-03-13 10:10:06 -04002118/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2120 struct btrfs_root *root,
2121 struct btrfs_delayed_ref_node *node,
2122 struct btrfs_delayed_extent_op *extent_op,
2123 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002124{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002125 int ret = 0;
2126
2127 if (trans->aborted)
2128 return 0;
2129
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002130 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002131 struct btrfs_delayed_ref_head *head;
2132 /*
2133 * we've hit the end of the chain and we were supposed
2134 * to insert this extent into the tree. But, it got
2135 * deleted before we ever needed to insert it, so all
2136 * we have to do is clean up the accounting
2137 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002138 BUG_ON(extent_op);
2139 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002140 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002141 btrfs_pin_extent(root, node->bytenr,
2142 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002143 if (head->is_data) {
2144 ret = btrfs_del_csums(trans, root,
2145 node->bytenr,
2146 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 }
Chris Mason56bec292009-03-13 10:10:06 -04002148 }
Chris Mason56bec292009-03-13 10:10:06 -04002149 mutex_unlock(&head->mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002150 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002151 }
Josef Bacikeb099672009-02-12 09:27:38 -05002152
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002153 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2154 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2155 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2156 insert_reserved);
2157 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2158 node->type == BTRFS_SHARED_DATA_REF_KEY)
2159 ret = run_delayed_data_ref(trans, root, node, extent_op,
2160 insert_reserved);
2161 else
2162 BUG();
2163 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002164}
2165
Chris Mason56bec292009-03-13 10:10:06 -04002166static noinline struct btrfs_delayed_ref_node *
2167select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002168{
Chris Mason56bec292009-03-13 10:10:06 -04002169 struct rb_node *node;
2170 struct btrfs_delayed_ref_node *ref;
2171 int action = BTRFS_ADD_DELAYED_REF;
2172again:
2173 /*
2174 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2175 * this prevents ref count from going down to zero when
2176 * there still are pending delayed ref.
2177 */
2178 node = rb_prev(&head->node.rb_node);
2179 while (1) {
2180 if (!node)
2181 break;
2182 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2183 rb_node);
2184 if (ref->bytenr != head->node.bytenr)
2185 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002186 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002187 return ref;
2188 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002189 }
Chris Mason56bec292009-03-13 10:10:06 -04002190 if (action == BTRFS_ADD_DELAYED_REF) {
2191 action = BTRFS_DROP_DELAYED_REF;
2192 goto again;
2193 }
2194 return NULL;
2195}
2196
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002197/*
2198 * Returns 0 on success or if called with an already aborted transaction.
2199 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2200 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002201static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2202 struct btrfs_root *root,
2203 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002204{
Chris Mason56bec292009-03-13 10:10:06 -04002205 struct btrfs_delayed_ref_root *delayed_refs;
2206 struct btrfs_delayed_ref_node *ref;
2207 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002208 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002209 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002210 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002211 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002212 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002213
2214 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002215 while (1) {
2216 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002217 /* pick a new head ref from the cluster list */
2218 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002219 break;
Chris Mason56bec292009-03-13 10:10:06 -04002220
Chris Masonc3e69d52009-03-13 10:17:05 -04002221 locked_ref = list_entry(cluster->next,
2222 struct btrfs_delayed_ref_head, cluster);
2223
2224 /* grab the lock that says we are going to process
2225 * all the refs for this head */
2226 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2227
2228 /*
2229 * we may have dropped the spin lock to get the head
2230 * mutex lock, and that might have given someone else
2231 * time to free the head. If that's true, it has been
2232 * removed from our list and we can move on.
2233 */
2234 if (ret == -EAGAIN) {
2235 locked_ref = NULL;
2236 count++;
2237 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002238 }
2239 }
2240
2241 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002242 * We need to try and merge add/drops of the same ref since we
2243 * can run into issues with relocate dropping the implicit ref
2244 * and then it being added back again before the drop can
2245 * finish. If we merged anything we need to re-loop so we can
2246 * get a good ref.
2247 */
2248 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2249 locked_ref);
2250
2251 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002252 * locked_ref is the head node, so we have to go one
2253 * node back for any delayed ref updates
2254 */
2255 ref = select_delayed_ref(locked_ref);
2256
2257 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002258 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002259 /*
2260 * there are still refs with lower seq numbers in the
2261 * process of being added. Don't run this ref yet.
2262 */
2263 list_del_init(&locked_ref->cluster);
2264 mutex_unlock(&locked_ref->mutex);
2265 locked_ref = NULL;
2266 delayed_refs->num_heads_ready++;
2267 spin_unlock(&delayed_refs->lock);
2268 cond_resched();
2269 spin_lock(&delayed_refs->lock);
2270 continue;
2271 }
2272
2273 /*
Chris Mason56bec292009-03-13 10:10:06 -04002274 * record the must insert reserved flag before we
2275 * drop the spin lock.
2276 */
2277 must_insert_reserved = locked_ref->must_insert_reserved;
2278 locked_ref->must_insert_reserved = 0;
2279
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002280 extent_op = locked_ref->extent_op;
2281 locked_ref->extent_op = NULL;
2282
Chris Mason56bec292009-03-13 10:10:06 -04002283 if (!ref) {
2284 /* All delayed refs have been processed, Go ahead
2285 * and send the head node to run_one_delayed_ref,
2286 * so that any accounting fixes can happen
2287 */
2288 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002289
2290 if (extent_op && must_insert_reserved) {
2291 kfree(extent_op);
2292 extent_op = NULL;
2293 }
2294
2295 if (extent_op) {
2296 spin_unlock(&delayed_refs->lock);
2297
2298 ret = run_delayed_extent_op(trans, root,
2299 ref, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002300 kfree(extent_op);
2301
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002302 if (ret) {
Liu Bo37c41462012-11-05 12:42:08 +00002303 list_del_init(&locked_ref->cluster);
2304 mutex_unlock(&locked_ref->mutex);
2305
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002306 printk(KERN_DEBUG "btrfs: run_delayed_extent_op returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002307 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002308 return ret;
2309 }
2310
Chris Mason203bf282012-01-06 15:23:57 -05002311 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002312 }
2313
Chris Masonc3e69d52009-03-13 10:17:05 -04002314 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002315 locked_ref = NULL;
2316 }
2317
2318 ref->in_tree = 0;
2319 rb_erase(&ref->rb_node, &delayed_refs->root);
2320 delayed_refs->num_entries--;
Arne Jansen22cd2e72012-08-09 00:16:53 -06002321 if (locked_ref) {
2322 /*
2323 * when we play the delayed ref, also correct the
2324 * ref_mod on head
2325 */
2326 switch (ref->action) {
2327 case BTRFS_ADD_DELAYED_REF:
2328 case BTRFS_ADD_DELAYED_EXTENT:
2329 locked_ref->node.ref_mod -= ref->ref_mod;
2330 break;
2331 case BTRFS_DROP_DELAYED_REF:
2332 locked_ref->node.ref_mod += ref->ref_mod;
2333 break;
2334 default:
2335 WARN_ON(1);
2336 }
2337 }
Chris Mason56bec292009-03-13 10:10:06 -04002338 spin_unlock(&delayed_refs->lock);
2339
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002340 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002341 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002342
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002343 btrfs_put_delayed_ref(ref);
2344 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002345 count++;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002346
2347 if (ret) {
Liu Bo37c41462012-11-05 12:42:08 +00002348 if (locked_ref) {
2349 list_del_init(&locked_ref->cluster);
2350 mutex_unlock(&locked_ref->mutex);
2351 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002352 printk(KERN_DEBUG "btrfs: run_one_delayed_ref returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002353 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002354 return ret;
2355 }
2356
Chris Mason203bf282012-01-06 15:23:57 -05002357next:
Chris Mason1887be62009-03-13 10:11:24 -04002358 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002359 spin_lock(&delayed_refs->lock);
2360 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002361 return count;
2362}
2363
Arne Jansen709c0482011-09-12 12:22:57 +02002364#ifdef SCRAMBLE_DELAYED_REFS
2365/*
2366 * Normally delayed refs get processed in ascending bytenr order. This
2367 * correlates in most cases to the order added. To expose dependencies on this
2368 * order, we start to process the tree in the middle instead of the beginning
2369 */
2370static u64 find_middle(struct rb_root *root)
2371{
2372 struct rb_node *n = root->rb_node;
2373 struct btrfs_delayed_ref_node *entry;
2374 int alt = 1;
2375 u64 middle;
2376 u64 first = 0, last = 0;
2377
2378 n = rb_first(root);
2379 if (n) {
2380 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2381 first = entry->bytenr;
2382 }
2383 n = rb_last(root);
2384 if (n) {
2385 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2386 last = entry->bytenr;
2387 }
2388 n = root->rb_node;
2389
2390 while (n) {
2391 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2392 WARN_ON(!entry->in_tree);
2393
2394 middle = entry->bytenr;
2395
2396 if (alt)
2397 n = n->rb_left;
2398 else
2399 n = n->rb_right;
2400
2401 alt = 1 - alt;
2402 }
2403 return middle;
2404}
2405#endif
2406
Arne Jansenbed92ea2012-06-28 18:03:02 +02002407int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2408 struct btrfs_fs_info *fs_info)
2409{
2410 struct qgroup_update *qgroup_update;
2411 int ret = 0;
2412
2413 if (list_empty(&trans->qgroup_ref_list) !=
2414 !trans->delayed_ref_elem.seq) {
2415 /* list without seq or seq without list */
2416 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2417 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2418 trans->delayed_ref_elem.seq);
2419 BUG();
2420 }
2421
2422 if (!trans->delayed_ref_elem.seq)
2423 return 0;
2424
2425 while (!list_empty(&trans->qgroup_ref_list)) {
2426 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2427 struct qgroup_update, list);
2428 list_del(&qgroup_update->list);
2429 if (!ret)
2430 ret = btrfs_qgroup_account_ref(
2431 trans, fs_info, qgroup_update->node,
2432 qgroup_update->extent_op);
2433 kfree(qgroup_update);
2434 }
2435
2436 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2437
2438 return ret;
2439}
2440
Chris Masonbb721702013-01-29 18:44:12 -05002441static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2442 int count)
2443{
2444 int val = atomic_read(&delayed_refs->ref_seq);
2445
2446 if (val < seq || val >= seq + count)
2447 return 1;
2448 return 0;
2449}
2450
Chris Masonc3e69d52009-03-13 10:17:05 -04002451/*
2452 * this starts processing the delayed reference count updates and
2453 * extent insertions we have queued up so far. count can be
2454 * 0, which means to process everything in the tree at the start
2455 * of the run (but not newly added entries), or it can be some target
2456 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002457 *
2458 * Returns 0 on success or if called with an aborted transaction
2459 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002460 */
2461int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2462 struct btrfs_root *root, unsigned long count)
2463{
2464 struct rb_node *node;
2465 struct btrfs_delayed_ref_root *delayed_refs;
2466 struct btrfs_delayed_ref_node *ref;
2467 struct list_head cluster;
2468 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002469 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002470 int run_all = count == (unsigned long)-1;
2471 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002472 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002473
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002474 /* We'll clean this up in btrfs_cleanup_transaction */
2475 if (trans->aborted)
2476 return 0;
2477
Chris Masonc3e69d52009-03-13 10:17:05 -04002478 if (root == root->fs_info->extent_root)
2479 root = root->fs_info->tree_root;
2480
Jan Schmidtedf39272012-06-28 18:04:55 +02002481 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2482
Chris Masonc3e69d52009-03-13 10:17:05 -04002483 delayed_refs = &trans->transaction->delayed_refs;
2484 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002485 if (count == 0) {
2486 count = delayed_refs->num_entries * 2;
2487 run_most = 1;
2488 }
2489
2490 if (!run_all && !run_most) {
2491 int old;
2492 int seq = atomic_read(&delayed_refs->ref_seq);
2493
2494progress:
2495 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2496 if (old) {
2497 DEFINE_WAIT(__wait);
2498 if (delayed_refs->num_entries < 16348)
2499 return 0;
2500
2501 prepare_to_wait(&delayed_refs->wait, &__wait,
2502 TASK_UNINTERRUPTIBLE);
2503
2504 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2505 if (old) {
2506 schedule();
2507 finish_wait(&delayed_refs->wait, &__wait);
2508
2509 if (!refs_newer(delayed_refs, seq, 256))
2510 goto progress;
2511 else
2512 return 0;
2513 } else {
2514 finish_wait(&delayed_refs->wait, &__wait);
2515 goto again;
2516 }
2517 }
2518
2519 } else {
2520 atomic_inc(&delayed_refs->procs_running_refs);
2521 }
2522
Chris Masonc3e69d52009-03-13 10:17:05 -04002523again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002524 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002525 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002526
Arne Jansen709c0482011-09-12 12:22:57 +02002527#ifdef SCRAMBLE_DELAYED_REFS
2528 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2529#endif
2530
Chris Masonc3e69d52009-03-13 10:17:05 -04002531 while (1) {
2532 if (!(run_all || run_most) &&
2533 delayed_refs->num_heads_ready < 64)
2534 break;
2535
2536 /*
2537 * go find something we can process in the rbtree. We start at
2538 * the beginning of the tree, and then build a cluster
2539 * of refs to process starting at the first one we are able to
2540 * lock
2541 */
Jan Schmidta1686502011-12-12 16:10:07 +01002542 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002543 ret = btrfs_find_ref_cluster(trans, &cluster,
2544 delayed_refs->run_delayed_start);
2545 if (ret)
2546 break;
2547
2548 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002549 if (ret < 0) {
2550 spin_unlock(&delayed_refs->lock);
2551 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002552 atomic_dec(&delayed_refs->procs_running_refs);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002553 return ret;
2554 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002555
Chris Masonbb721702013-01-29 18:44:12 -05002556 atomic_add(ret, &delayed_refs->ref_seq);
2557
Chris Masonc3e69d52009-03-13 10:17:05 -04002558 count -= min_t(unsigned long, ret, count);
2559
2560 if (count == 0)
2561 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002562
Arne Jansen1fa11e22012-08-06 14:18:51 -06002563 if (delayed_start >= delayed_refs->run_delayed_start) {
2564 if (loops == 0) {
2565 /*
2566 * btrfs_find_ref_cluster looped. let's do one
2567 * more cycle. if we don't run any delayed ref
2568 * during that cycle (because we can't because
2569 * all of them are blocked), bail out.
2570 */
2571 loops = 1;
2572 } else {
2573 /*
2574 * no runnable refs left, stop trying
2575 */
2576 BUG_ON(run_all);
2577 break;
2578 }
2579 }
2580 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002581 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002582 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002583 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002584 }
2585
Chris Mason56bec292009-03-13 10:10:06 -04002586 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002587 if (!list_empty(&trans->new_bgs)) {
2588 spin_unlock(&delayed_refs->lock);
2589 btrfs_create_pending_block_groups(trans, root);
2590 spin_lock(&delayed_refs->lock);
2591 }
2592
Chris Mason56bec292009-03-13 10:10:06 -04002593 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002594 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002595 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002596 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002597
2598 while (node) {
2599 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2600 rb_node);
2601 if (btrfs_delayed_ref_is_head(ref)) {
2602 struct btrfs_delayed_ref_head *head;
2603
2604 head = btrfs_delayed_node_to_head(ref);
2605 atomic_inc(&ref->refs);
2606
2607 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002608 /*
2609 * Mutex was contended, block until it's
2610 * released and try again
2611 */
Chris Mason56bec292009-03-13 10:10:06 -04002612 mutex_lock(&head->mutex);
2613 mutex_unlock(&head->mutex);
2614
2615 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002616 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002617 goto again;
2618 }
2619 node = rb_next(node);
2620 }
2621 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002622 schedule_timeout(1);
2623 goto again;
2624 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002625out:
Chris Masonbb721702013-01-29 18:44:12 -05002626 atomic_dec(&delayed_refs->procs_running_refs);
2627 smp_mb();
2628 if (waitqueue_active(&delayed_refs->wait))
2629 wake_up(&delayed_refs->wait);
2630
Chris Masonc3e69d52009-03-13 10:17:05 -04002631 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002632 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002633 return 0;
2634}
2635
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002636int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2637 struct btrfs_root *root,
2638 u64 bytenr, u64 num_bytes, u64 flags,
2639 int is_data)
2640{
2641 struct btrfs_delayed_extent_op *extent_op;
2642 int ret;
2643
2644 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2645 if (!extent_op)
2646 return -ENOMEM;
2647
2648 extent_op->flags_to_set = flags;
2649 extent_op->update_flags = 1;
2650 extent_op->update_key = 0;
2651 extent_op->is_data = is_data ? 1 : 0;
2652
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002653 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2654 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002655 if (ret)
2656 kfree(extent_op);
2657 return ret;
2658}
2659
2660static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2661 struct btrfs_root *root,
2662 struct btrfs_path *path,
2663 u64 objectid, u64 offset, u64 bytenr)
2664{
2665 struct btrfs_delayed_ref_head *head;
2666 struct btrfs_delayed_ref_node *ref;
2667 struct btrfs_delayed_data_ref *data_ref;
2668 struct btrfs_delayed_ref_root *delayed_refs;
2669 struct rb_node *node;
2670 int ret = 0;
2671
2672 ret = -ENOENT;
2673 delayed_refs = &trans->transaction->delayed_refs;
2674 spin_lock(&delayed_refs->lock);
2675 head = btrfs_find_delayed_ref_head(trans, bytenr);
2676 if (!head)
2677 goto out;
2678
2679 if (!mutex_trylock(&head->mutex)) {
2680 atomic_inc(&head->node.refs);
2681 spin_unlock(&delayed_refs->lock);
2682
David Sterbab3b4aa72011-04-21 01:20:15 +02002683 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002684
David Sterba8cc33e52011-05-02 15:29:25 +02002685 /*
2686 * Mutex was contended, block until it's released and let
2687 * caller try again
2688 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002689 mutex_lock(&head->mutex);
2690 mutex_unlock(&head->mutex);
2691 btrfs_put_delayed_ref(&head->node);
2692 return -EAGAIN;
2693 }
2694
2695 node = rb_prev(&head->node.rb_node);
2696 if (!node)
2697 goto out_unlock;
2698
2699 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2700
2701 if (ref->bytenr != bytenr)
2702 goto out_unlock;
2703
2704 ret = 1;
2705 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2706 goto out_unlock;
2707
2708 data_ref = btrfs_delayed_node_to_data_ref(ref);
2709
2710 node = rb_prev(node);
2711 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002712 int seq = ref->seq;
2713
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002714 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002715 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002716 goto out_unlock;
2717 }
2718
2719 if (data_ref->root != root->root_key.objectid ||
2720 data_ref->objectid != objectid || data_ref->offset != offset)
2721 goto out_unlock;
2722
2723 ret = 0;
2724out_unlock:
2725 mutex_unlock(&head->mutex);
2726out:
2727 spin_unlock(&delayed_refs->lock);
2728 return ret;
2729}
2730
2731static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2732 struct btrfs_root *root,
2733 struct btrfs_path *path,
2734 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002735{
2736 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002737 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002738 struct btrfs_extent_data_ref *ref;
2739 struct btrfs_extent_inline_ref *iref;
2740 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002741 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002742 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002743 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002744
Chris Masonbe20aa92007-12-17 20:14:01 -05002745 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002746 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002747 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002748
Chris Masonbe20aa92007-12-17 20:14:01 -05002749 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2750 if (ret < 0)
2751 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002752 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002753
2754 ret = -ENOENT;
2755 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002756 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002757
Zheng Yan31840ae2008-09-23 13:14:14 -04002758 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002759 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002760 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002761
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002762 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002763 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002764
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002765 ret = 1;
2766 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2767#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2768 if (item_size < sizeof(*ei)) {
2769 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2770 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002771 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002772#endif
2773 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2774
2775 if (item_size != sizeof(*ei) +
2776 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2777 goto out;
2778
2779 if (btrfs_extent_generation(leaf, ei) <=
2780 btrfs_root_last_snapshot(&root->root_item))
2781 goto out;
2782
2783 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2784 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2785 BTRFS_EXTENT_DATA_REF_KEY)
2786 goto out;
2787
2788 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2789 if (btrfs_extent_refs(leaf, ei) !=
2790 btrfs_extent_data_ref_count(leaf, ref) ||
2791 btrfs_extent_data_ref_root(leaf, ref) !=
2792 root->root_key.objectid ||
2793 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2794 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2795 goto out;
2796
Yan Zhengf321e492008-07-30 09:26:11 -04002797 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002798out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002799 return ret;
2800}
2801
2802int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2803 struct btrfs_root *root,
2804 u64 objectid, u64 offset, u64 bytenr)
2805{
2806 struct btrfs_path *path;
2807 int ret;
2808 int ret2;
2809
2810 path = btrfs_alloc_path();
2811 if (!path)
2812 return -ENOENT;
2813
2814 do {
2815 ret = check_committed_ref(trans, root, path, objectid,
2816 offset, bytenr);
2817 if (ret && ret != -ENOENT)
2818 goto out;
2819
2820 ret2 = check_delayed_ref(trans, root, path, objectid,
2821 offset, bytenr);
2822 } while (ret2 == -EAGAIN);
2823
2824 if (ret2 && ret2 != -ENOENT) {
2825 ret = ret2;
2826 goto out;
2827 }
2828
2829 if (ret != -ENOENT || ret2 != -ENOENT)
2830 ret = 0;
2831out:
Yan Zhengf321e492008-07-30 09:26:11 -04002832 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002833 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2834 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002835 return ret;
2836}
2837
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002838static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002839 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002840 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002841 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002842{
2843 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002844 u64 num_bytes;
2845 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002846 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002847 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002848 struct btrfs_key key;
2849 struct btrfs_file_extent_item *fi;
2850 int i;
2851 int level;
2852 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002853 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002854 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002855
2856 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002857 nritems = btrfs_header_nritems(buf);
2858 level = btrfs_header_level(buf);
2859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002860 if (!root->ref_cows && level == 0)
2861 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002862
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002863 if (inc)
2864 process_func = btrfs_inc_extent_ref;
2865 else
2866 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002868 if (full_backref)
2869 parent = buf->start;
2870 else
2871 parent = 0;
2872
Zheng Yan31840ae2008-09-23 13:14:14 -04002873 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002874 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002875 btrfs_item_key_to_cpu(buf, &key, i);
2876 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002877 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002878 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002879 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002880 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002881 BTRFS_FILE_EXTENT_INLINE)
2882 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002883 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2884 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002885 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002886
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002887 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2888 key.offset -= btrfs_file_extent_offset(buf, fi);
2889 ret = process_func(trans, root, bytenr, num_bytes,
2890 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002891 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002892 if (ret)
2893 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002894 } else {
2895 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002896 num_bytes = btrfs_level_size(root, level - 1);
2897 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002898 parent, ref_root, level - 1, 0,
2899 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002900 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002901 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002902 }
2903 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002904 return 0;
2905fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002906 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002907}
2908
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002909int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002910 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002911{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002912 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002913}
Zheng Yan31840ae2008-09-23 13:14:14 -04002914
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002915int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002916 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002917{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002918 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002919}
2920
Chris Mason9078a3e2007-04-26 16:46:15 -04002921static int write_one_cache_group(struct btrfs_trans_handle *trans,
2922 struct btrfs_root *root,
2923 struct btrfs_path *path,
2924 struct btrfs_block_group_cache *cache)
2925{
2926 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002927 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002928 unsigned long bi;
2929 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002930
Chris Mason9078a3e2007-04-26 16:46:15 -04002931 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002932 if (ret < 0)
2933 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002934 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04002935
2936 leaf = path->nodes[0];
2937 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2938 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2939 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002940 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002941fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002942 if (ret) {
2943 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04002944 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002945 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002946 return 0;
2947
2948}
2949
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002950static struct btrfs_block_group_cache *
2951next_block_group(struct btrfs_root *root,
2952 struct btrfs_block_group_cache *cache)
2953{
2954 struct rb_node *node;
2955 spin_lock(&root->fs_info->block_group_cache_lock);
2956 node = rb_next(&cache->cache_node);
2957 btrfs_put_block_group(cache);
2958 if (node) {
2959 cache = rb_entry(node, struct btrfs_block_group_cache,
2960 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002961 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002962 } else
2963 cache = NULL;
2964 spin_unlock(&root->fs_info->block_group_cache_lock);
2965 return cache;
2966}
2967
Josef Bacik0af3d002010-06-21 14:48:16 -04002968static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2969 struct btrfs_trans_handle *trans,
2970 struct btrfs_path *path)
2971{
2972 struct btrfs_root *root = block_group->fs_info->tree_root;
2973 struct inode *inode = NULL;
2974 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002975 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002976 int num_pages = 0;
2977 int retries = 0;
2978 int ret = 0;
2979
2980 /*
2981 * If this block group is smaller than 100 megs don't bother caching the
2982 * block group.
2983 */
2984 if (block_group->key.offset < (100 * 1024 * 1024)) {
2985 spin_lock(&block_group->lock);
2986 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2987 spin_unlock(&block_group->lock);
2988 return 0;
2989 }
2990
2991again:
2992 inode = lookup_free_space_inode(root, block_group, path);
2993 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2994 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002995 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002996 goto out;
2997 }
2998
2999 if (IS_ERR(inode)) {
3000 BUG_ON(retries);
3001 retries++;
3002
3003 if (block_group->ro)
3004 goto out_free;
3005
3006 ret = create_free_space_inode(root, trans, block_group, path);
3007 if (ret)
3008 goto out_free;
3009 goto again;
3010 }
3011
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003012 /* We've already setup this transaction, go ahead and exit */
3013 if (block_group->cache_generation == trans->transid &&
3014 i_size_read(inode)) {
3015 dcs = BTRFS_DC_SETUP;
3016 goto out_put;
3017 }
3018
Josef Bacik0af3d002010-06-21 14:48:16 -04003019 /*
3020 * We want to set the generation to 0, that way if anything goes wrong
3021 * from here on out we know not to trust this cache when we load up next
3022 * time.
3023 */
3024 BTRFS_I(inode)->generation = 0;
3025 ret = btrfs_update_inode(trans, root, inode);
3026 WARN_ON(ret);
3027
3028 if (i_size_read(inode) > 0) {
3029 ret = btrfs_truncate_free_space_cache(root, trans, path,
3030 inode);
3031 if (ret)
3032 goto out_put;
3033 }
3034
3035 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003036 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3037 !btrfs_test_opt(root, SPACE_CACHE)) {
3038 /*
3039 * don't bother trying to write stuff out _if_
3040 * a) we're not cached,
3041 * b) we're with nospace_cache mount option.
3042 */
Josef Bacik2b209822010-12-03 13:17:53 -05003043 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003044 spin_unlock(&block_group->lock);
3045 goto out_put;
3046 }
3047 spin_unlock(&block_group->lock);
3048
Josef Bacik6fc823b2012-08-06 13:46:38 -06003049 /*
3050 * Try to preallocate enough space based on how big the block group is.
3051 * Keep in mind this has to include any pinned space which could end up
3052 * taking up quite a bit since it's not folded into the other space
3053 * cache.
3054 */
3055 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003056 if (!num_pages)
3057 num_pages = 1;
3058
Josef Bacik0af3d002010-06-21 14:48:16 -04003059 num_pages *= 16;
3060 num_pages *= PAGE_CACHE_SIZE;
3061
3062 ret = btrfs_check_data_free_space(inode, num_pages);
3063 if (ret)
3064 goto out_put;
3065
3066 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3067 num_pages, num_pages,
3068 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003069 if (!ret)
3070 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003071 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003072
Josef Bacik0af3d002010-06-21 14:48:16 -04003073out_put:
3074 iput(inode);
3075out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003076 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003077out:
3078 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003079 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003080 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003081 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003082 spin_unlock(&block_group->lock);
3083
3084 return ret;
3085}
3086
Chris Mason96b51792007-10-15 16:15:19 -04003087int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3088 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003089{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003090 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003091 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003092 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003093 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003094
3095 path = btrfs_alloc_path();
3096 if (!path)
3097 return -ENOMEM;
3098
Josef Bacik0af3d002010-06-21 14:48:16 -04003099again:
3100 while (1) {
3101 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3102 while (cache) {
3103 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3104 break;
3105 cache = next_block_group(root, cache);
3106 }
3107 if (!cache) {
3108 if (last == 0)
3109 break;
3110 last = 0;
3111 continue;
3112 }
3113 err = cache_save_setup(cache, trans, path);
3114 last = cache->key.objectid + cache->key.offset;
3115 btrfs_put_block_group(cache);
3116 }
3117
Chris Masond3977122009-01-05 21:25:51 -05003118 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003119 if (last == 0) {
3120 err = btrfs_run_delayed_refs(trans, root,
3121 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003122 if (err) /* File system offline */
3123 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003124 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003125
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003126 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3127 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003128 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3129 btrfs_put_block_group(cache);
3130 goto again;
3131 }
3132
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003133 if (cache->dirty)
3134 break;
3135 cache = next_block_group(root, cache);
3136 }
3137 if (!cache) {
3138 if (last == 0)
3139 break;
3140 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003141 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003142 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003143
Josef Bacik0cb59c92010-07-02 12:14:14 -04003144 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3145 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003146 cache->dirty = 0;
3147 last = cache->key.objectid + cache->key.offset;
3148
3149 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003150 if (err) /* File system offline */
3151 goto out;
3152
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003153 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003154 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003155
Josef Bacik0cb59c92010-07-02 12:14:14 -04003156 while (1) {
3157 /*
3158 * I don't think this is needed since we're just marking our
3159 * preallocated extent as written, but just in case it can't
3160 * hurt.
3161 */
3162 if (last == 0) {
3163 err = btrfs_run_delayed_refs(trans, root,
3164 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003165 if (err) /* File system offline */
3166 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003167 }
3168
3169 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3170 while (cache) {
3171 /*
3172 * Really this shouldn't happen, but it could if we
3173 * couldn't write the entire preallocated extent and
3174 * splitting the extent resulted in a new block.
3175 */
3176 if (cache->dirty) {
3177 btrfs_put_block_group(cache);
3178 goto again;
3179 }
3180 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3181 break;
3182 cache = next_block_group(root, cache);
3183 }
3184 if (!cache) {
3185 if (last == 0)
3186 break;
3187 last = 0;
3188 continue;
3189 }
3190
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003191 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003192
3193 /*
3194 * If we didn't have an error then the cache state is still
3195 * NEED_WRITE, so we can set it to WRITTEN.
3196 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003197 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003198 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3199 last = cache->key.objectid + cache->key.offset;
3200 btrfs_put_block_group(cache);
3201 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003202out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003203
Chris Mason9078a3e2007-04-26 16:46:15 -04003204 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003205 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003206}
3207
Yan Zhengd2fb3432008-12-11 16:30:39 -05003208int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3209{
3210 struct btrfs_block_group_cache *block_group;
3211 int readonly = 0;
3212
3213 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3214 if (!block_group || block_group->ro)
3215 readonly = 1;
3216 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003217 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003218 return readonly;
3219}
3220
Chris Mason593060d2008-03-25 16:50:33 -04003221static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3222 u64 total_bytes, u64 bytes_used,
3223 struct btrfs_space_info **space_info)
3224{
3225 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003226 int i;
3227 int factor;
3228
3229 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3230 BTRFS_BLOCK_GROUP_RAID10))
3231 factor = 2;
3232 else
3233 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003234
3235 found = __find_space_info(info, flags);
3236 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003237 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003238 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003239 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003240 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003241 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003242 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003243 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003244 *space_info = found;
3245 return 0;
3246 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003247 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003248 if (!found)
3249 return -ENOMEM;
3250
Yan, Zhengb742bb82010-05-16 10:46:24 -04003251 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3252 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003253 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003254 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003255 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003256 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003257 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003258 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003259 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003260 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003261 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003262 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003263 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003264 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003265 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003266 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003267 found->flush = 0;
3268 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003269 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003270 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003271 if (flags & BTRFS_BLOCK_GROUP_DATA)
3272 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003273 return 0;
3274}
3275
Chris Mason8790d502008-04-03 16:29:03 -04003276static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3277{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003278 u64 extra_flags = chunk_to_extended(flags) &
3279 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003280
3281 if (flags & BTRFS_BLOCK_GROUP_DATA)
3282 fs_info->avail_data_alloc_bits |= extra_flags;
3283 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3284 fs_info->avail_metadata_alloc_bits |= extra_flags;
3285 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3286 fs_info->avail_system_alloc_bits |= extra_flags;
Chris Mason8790d502008-04-03 16:29:03 -04003287}
Chris Mason593060d2008-03-25 16:50:33 -04003288
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003289/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003290 * returns target flags in extended format or 0 if restripe for this
3291 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003292 *
3293 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003294 */
3295static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3296{
3297 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3298 u64 target = 0;
3299
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003300 if (!bctl)
3301 return 0;
3302
3303 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3304 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3305 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3306 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3307 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3308 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3309 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3310 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3311 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3312 }
3313
3314 return target;
3315}
3316
3317/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003318 * @flags: available profiles in extended format (see ctree.h)
3319 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003320 * Returns reduced profile in chunk format. If profile changing is in
3321 * progress (either running or paused) picks the target profile (if it's
3322 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003323 */
Yan Zheng2b820322008-11-17 21:11:30 -05003324u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003325{
Chris Masoncd02dca2010-12-13 14:56:23 -05003326 /*
3327 * we add in the count of missing devices because we want
3328 * to make sure that any RAID levels on a degraded FS
3329 * continue to be honored.
3330 */
3331 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3332 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003333 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003334 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003335
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003336 /*
3337 * see if restripe for this chunk_type is in progress, if so
3338 * try to reduce to the target profile
3339 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003340 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003341 target = get_restripe_target(root->fs_info, flags);
3342 if (target) {
3343 /* pick target profile only if it's already available */
3344 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003345 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003346 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003347 }
3348 }
3349 spin_unlock(&root->fs_info->balance_lock);
3350
David Woodhouse53b381b2013-01-29 18:40:14 -05003351 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003352 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003353 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3354 BTRFS_BLOCK_GROUP_RAID5);
3355 if (num_devices < 3)
3356 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003357 if (num_devices < 4)
3358 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3359
David Woodhouse53b381b2013-01-29 18:40:14 -05003360 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3361 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3362 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3363 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003364
David Woodhouse53b381b2013-01-29 18:40:14 -05003365 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3366 tmp = BTRFS_BLOCK_GROUP_RAID6;
3367 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3368 tmp = BTRFS_BLOCK_GROUP_RAID5;
3369 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3370 tmp = BTRFS_BLOCK_GROUP_RAID10;
3371 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3372 tmp = BTRFS_BLOCK_GROUP_RAID1;
3373 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3374 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003375
David Woodhouse53b381b2013-01-29 18:40:14 -05003376 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003377}
3378
Yan, Zhengb742bb82010-05-16 10:46:24 -04003379static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003380{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003381 if (flags & BTRFS_BLOCK_GROUP_DATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003382 flags |= root->fs_info->avail_data_alloc_bits;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003383 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003384 flags |= root->fs_info->avail_system_alloc_bits;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003385 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003386 flags |= root->fs_info->avail_metadata_alloc_bits;
3387
Yan, Zhengb742bb82010-05-16 10:46:24 -04003388 return btrfs_reduce_alloc_profile(root, flags);
3389}
Josef Bacik6a632092009-02-20 11:00:09 -05003390
Miao Xie6d07bce2011-01-05 10:07:31 +00003391u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003392{
3393 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003394 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003395
Yan, Zhengb742bb82010-05-16 10:46:24 -04003396 if (data)
3397 flags = BTRFS_BLOCK_GROUP_DATA;
3398 else if (root == root->fs_info->chunk_root)
3399 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3400 else
3401 flags = BTRFS_BLOCK_GROUP_METADATA;
3402
David Woodhouse53b381b2013-01-29 18:40:14 -05003403 ret = get_alloc_profile(root, flags);
3404 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003405}
3406
Josef Bacik6a632092009-02-20 11:00:09 -05003407/*
3408 * This will check the space that the inode allocates from to make sure we have
3409 * enough space for bytes.
3410 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003411int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003412{
3413 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003414 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003415 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003416 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003417 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003418
3419 /* make sure bytes are sectorsize aligned */
3420 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3421
Li Zefan82d59022011-04-20 10:33:24 +08003422 if (root == root->fs_info->tree_root ||
3423 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003424 alloc_chunk = 0;
3425 committed = 1;
3426 }
3427
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003428 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003429 if (!data_sinfo)
3430 goto alloc;
3431
Josef Bacik6a632092009-02-20 11:00:09 -05003432again:
3433 /* make sure we have enough space to handle the data first */
3434 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003435 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3436 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3437 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003438
3439 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003440 struct btrfs_trans_handle *trans;
3441
Josef Bacik6a632092009-02-20 11:00:09 -05003442 /*
3443 * if we don't have enough free bytes in this space then we need
3444 * to alloc a new chunk.
3445 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003446 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003447 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003448
Chris Mason0e4f8f82011-04-15 16:05:44 -04003449 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003450 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003451alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003452 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003453 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003454 if (IS_ERR(trans))
3455 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003456
3457 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003458 alloc_target,
3459 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003460 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003461 if (ret < 0) {
3462 if (ret != -ENOSPC)
3463 return ret;
3464 else
3465 goto commit_trans;
3466 }
Chris Mason33b4d472009-09-22 14:45:50 -04003467
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003468 if (!data_sinfo)
3469 data_sinfo = fs_info->data_sinfo;
3470
Josef Bacik6a632092009-02-20 11:00:09 -05003471 goto again;
3472 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003473
3474 /*
3475 * If we have less pinned bytes than we want to allocate then
3476 * don't bother committing the transaction, it won't help us.
3477 */
3478 if (data_sinfo->bytes_pinned < bytes)
3479 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003480 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003481
3482 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003483commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003484 if (!committed &&
3485 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003486 committed = 1;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003487 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003488 if (IS_ERR(trans))
3489 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003490 ret = btrfs_commit_transaction(trans, root);
3491 if (ret)
3492 return ret;
3493 goto again;
3494 }
3495
Josef Bacik6a632092009-02-20 11:00:09 -05003496 return -ENOSPC;
3497 }
3498 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003499 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003500 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003501 spin_unlock(&data_sinfo->lock);
3502
Josef Bacik9ed74f22009-09-11 16:12:44 -04003503 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003504}
3505
3506/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003507 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003508 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003509void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003510{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003511 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003512 struct btrfs_space_info *data_sinfo;
3513
3514 /* make sure bytes are sectorsize aligned */
3515 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3516
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003517 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003518 spin_lock(&data_sinfo->lock);
3519 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003520 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003521 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003522 spin_unlock(&data_sinfo->lock);
3523}
3524
Josef Bacik97e728d2009-04-21 17:40:57 -04003525static void force_metadata_allocation(struct btrfs_fs_info *info)
3526{
3527 struct list_head *head = &info->space_info;
3528 struct btrfs_space_info *found;
3529
3530 rcu_read_lock();
3531 list_for_each_entry_rcu(found, head, list) {
3532 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003533 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003534 }
3535 rcu_read_unlock();
3536}
3537
Chris Masone5bc2452010-10-26 13:37:56 -04003538static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003539 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003540{
Josef Bacikfb25e912011-07-26 17:00:46 -04003541 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003542 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003543 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003544 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003545
Chris Mason0e4f8f82011-04-15 16:05:44 -04003546 if (force == CHUNK_ALLOC_FORCE)
3547 return 1;
3548
3549 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003550 * We need to take into account the global rsv because for all intents
3551 * and purposes it's used space. Don't worry about locking the
3552 * global_rsv, it doesn't change except when the transaction commits.
3553 */
Josef Bacik54338b52012-08-14 16:20:52 -04003554 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3555 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003556
3557 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003558 * in limited mode, we want to have some free space up to
3559 * about 1% of the FS size.
3560 */
3561 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003562 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003563 thresh = max_t(u64, 64 * 1024 * 1024,
3564 div_factor_fine(thresh, 1));
3565
3566 if (num_bytes - num_allocated < thresh)
3567 return 1;
3568 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003569
Josef Bacik698d0082012-09-12 14:08:47 -04003570 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003571 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003572 return 1;
3573}
3574
Liu Bo15d1ff82012-03-29 09:57:44 -04003575static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3576{
3577 u64 num_dev;
3578
David Woodhouse53b381b2013-01-29 18:40:14 -05003579 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3580 BTRFS_BLOCK_GROUP_RAID0 |
3581 BTRFS_BLOCK_GROUP_RAID5 |
3582 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003583 num_dev = root->fs_info->fs_devices->rw_devices;
3584 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3585 num_dev = 2;
3586 else
3587 num_dev = 1; /* DUP or single */
3588
3589 /* metadata for updaing devices and chunk tree */
3590 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3591}
3592
3593static void check_system_chunk(struct btrfs_trans_handle *trans,
3594 struct btrfs_root *root, u64 type)
3595{
3596 struct btrfs_space_info *info;
3597 u64 left;
3598 u64 thresh;
3599
3600 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3601 spin_lock(&info->lock);
3602 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3603 info->bytes_reserved - info->bytes_readonly;
3604 spin_unlock(&info->lock);
3605
3606 thresh = get_system_chunk_thresh(root, type);
3607 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3608 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3609 left, thresh, type);
3610 dump_space_info(info, 0, 0);
3611 }
3612
3613 if (left < thresh) {
3614 u64 flags;
3615
3616 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3617 btrfs_alloc_chunk(trans, root, flags);
3618 }
3619}
3620
Chris Mason6324fbf2008-03-24 15:01:59 -04003621static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003622 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003623{
3624 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003625 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003626 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003627 int ret = 0;
3628
Chris Mason6324fbf2008-03-24 15:01:59 -04003629 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003630 if (!space_info) {
3631 ret = update_space_info(extent_root->fs_info, flags,
3632 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003633 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003634 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003635 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003636
Josef Bacik6d741192011-04-11 20:20:11 -04003637again:
Josef Bacik25179202008-10-29 14:49:05 -04003638 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003639 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003640 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003641 if (space_info->full) {
3642 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003643 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003644 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003645
Josef Bacik698d0082012-09-12 14:08:47 -04003646 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003647 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003648 return 0;
3649 } else if (space_info->chunk_alloc) {
3650 wait_for_alloc = 1;
3651 } else {
3652 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003653 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003654
Josef Bacik25179202008-10-29 14:49:05 -04003655 spin_unlock(&space_info->lock);
3656
Josef Bacik6d741192011-04-11 20:20:11 -04003657 mutex_lock(&fs_info->chunk_mutex);
3658
3659 /*
3660 * The chunk_mutex is held throughout the entirety of a chunk
3661 * allocation, so once we've acquired the chunk_mutex we know that the
3662 * other guy is done and we need to recheck and see if we should
3663 * allocate.
3664 */
3665 if (wait_for_alloc) {
3666 mutex_unlock(&fs_info->chunk_mutex);
3667 wait_for_alloc = 0;
3668 goto again;
3669 }
3670
Josef Bacik97e728d2009-04-21 17:40:57 -04003671 /*
Josef Bacik67377732010-09-16 16:19:09 -04003672 * If we have mixed data/metadata chunks we want to make sure we keep
3673 * allocating mixed chunks instead of individual chunks.
3674 */
3675 if (btrfs_mixed_space_info(space_info))
3676 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3677
3678 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003679 * if we're doing a data chunk, go ahead and make sure that
3680 * we keep a reasonable number of metadata chunks allocated in the
3681 * FS as well.
3682 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003683 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003684 fs_info->data_chunk_allocations++;
3685 if (!(fs_info->data_chunk_allocations %
3686 fs_info->metadata_ratio))
3687 force_metadata_allocation(fs_info);
3688 }
3689
Liu Bo15d1ff82012-03-29 09:57:44 -04003690 /*
3691 * Check if we have enough space in SYSTEM chunk because we may need
3692 * to update devices.
3693 */
3694 check_system_chunk(trans, extent_root, flags);
3695
Yan Zheng2b820322008-11-17 21:11:30 -05003696 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003697 if (ret < 0 && ret != -ENOSPC)
3698 goto out;
3699
Josef Bacik9ed74f22009-09-11 16:12:44 -04003700 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003701 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003702 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003703 else
3704 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003705
Chris Mason0e4f8f82011-04-15 16:05:44 -04003706 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003707 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003708 spin_unlock(&space_info->lock);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003709out:
Dan Carpentera25c75d2012-04-18 09:59:29 +03003710 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003711 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003712}
3713
Josef Bacika80c8dc2012-09-06 16:59:33 -04003714static int can_overcommit(struct btrfs_root *root,
3715 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003716 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003717{
3718 u64 profile = btrfs_get_alloc_profile(root, 0);
3719 u64 avail;
3720 u64 used;
3721
3722 used = space_info->bytes_used + space_info->bytes_reserved +
3723 space_info->bytes_pinned + space_info->bytes_readonly +
3724 space_info->bytes_may_use;
3725
3726 spin_lock(&root->fs_info->free_chunk_lock);
3727 avail = root->fs_info->free_chunk_space;
3728 spin_unlock(&root->fs_info->free_chunk_lock);
3729
3730 /*
3731 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003732 * space is actually useable. For raid56, the space info used
3733 * doesn't include the parity drive, so we don't have to
3734 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003735 */
3736 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3737 BTRFS_BLOCK_GROUP_RAID1 |
3738 BTRFS_BLOCK_GROUP_RAID10))
3739 avail >>= 1;
3740
3741 /*
Miao Xie561c2942012-10-16 11:32:18 +00003742 * If we aren't flushing all things, let us overcommit up to
3743 * 1/2th of the space. If we can flush, don't let us overcommit
3744 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003745 */
Miao Xie08e007d2012-10-16 11:33:38 +00003746 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003747 avail >>= 3;
3748 else
3749 avail >>= 1;
3750
3751 if (used + bytes < space_info->total_bytes + avail)
3752 return 1;
3753 return 0;
3754}
3755
Josef Bacik1135d6d2012-12-14 13:46:43 -05003756static int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
3757 unsigned long nr_pages,
3758 enum wb_reason reason)
3759{
3760 if (!writeback_in_progress(sb->s_bdi) &&
3761 down_read_trylock(&sb->s_umount)) {
3762 writeback_inodes_sb_nr(sb, nr_pages, reason);
3763 up_read(&sb->s_umount);
3764 return 1;
3765 }
3766
3767 return 0;
3768}
3769
Yan, Zheng5da9d012010-05-16 10:46:25 -04003770/*
3771 * shrink metadata reservation for delalloc
3772 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003773static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3774 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003775{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003776 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003777 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003778 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003779 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003780 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003781 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003782 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003783 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003784 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003785
Josef Bacik663350a2011-11-03 22:54:25 -04003786 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003787 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003788 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003789
3790 smp_mb();
Josef Bacikf4c738c2012-07-02 17:10:51 -04003791 delalloc_bytes = root->fs_info->delalloc_bytes;
3792 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003793 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003794 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003795 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003796 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003797 }
3798
Josef Bacikf4c738c2012-07-02 17:10:51 -04003799 while (delalloc_bytes && loops < 3) {
3800 max_reclaim = min(delalloc_bytes, to_reclaim);
3801 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Josef Bacik1135d6d2012-12-14 13:46:43 -05003802 writeback_inodes_sb_nr_if_idle_safe(root->fs_info->sb,
3803 nr_pages,
3804 WB_REASON_FS_FREE_SPACE);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003805
Josef Bacikdea31f52012-09-06 16:47:00 -04003806 /*
3807 * We need to wait for the async pages to actually start before
3808 * we do anything.
3809 */
3810 wait_event(root->fs_info->async_submit_wait,
3811 !atomic_read(&root->fs_info->async_delalloc_pages));
3812
Miao Xie08e007d2012-10-16 11:33:38 +00003813 if (!trans)
3814 flush = BTRFS_RESERVE_FLUSH_ALL;
3815 else
3816 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003817 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003818 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003819 spin_unlock(&space_info->lock);
3820 break;
3821 }
Josef Bacik0019f102010-10-15 15:18:40 -04003822 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003823
Chris Mason36e39c42011-03-12 07:08:42 -05003824 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003825 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003826 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003827 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003828 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003829 if (time_left)
3830 break;
3831 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003832 smp_mb();
3833 delalloc_bytes = root->fs_info->delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003834 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003835}
3836
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003837/**
Josef Bacik663350a2011-11-03 22:54:25 -04003838 * maybe_commit_transaction - possibly commit the transaction if its ok to
3839 * @root - the root we're allocating for
3840 * @bytes - the number of bytes we want to reserve
3841 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003842 *
Josef Bacik663350a2011-11-03 22:54:25 -04003843 * This will check to make sure that committing the transaction will actually
3844 * get us somewhere and then commit the transaction if it does. Otherwise it
3845 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003846 */
Josef Bacik663350a2011-11-03 22:54:25 -04003847static int may_commit_transaction(struct btrfs_root *root,
3848 struct btrfs_space_info *space_info,
3849 u64 bytes, int force)
3850{
3851 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3852 struct btrfs_trans_handle *trans;
3853
3854 trans = (struct btrfs_trans_handle *)current->journal_info;
3855 if (trans)
3856 return -EAGAIN;
3857
3858 if (force)
3859 goto commit;
3860
3861 /* See if there is enough pinned space to make this reservation */
3862 spin_lock(&space_info->lock);
3863 if (space_info->bytes_pinned >= bytes) {
3864 spin_unlock(&space_info->lock);
3865 goto commit;
3866 }
3867 spin_unlock(&space_info->lock);
3868
3869 /*
3870 * See if there is some space in the delayed insertion reservation for
3871 * this reservation.
3872 */
3873 if (space_info != delayed_rsv->space_info)
3874 return -ENOSPC;
3875
Liu Bod9b02182012-02-16 18:34:39 +08003876 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003877 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003878 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04003879 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003880 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003881 return -ENOSPC;
3882 }
3883 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003884 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003885
3886commit:
3887 trans = btrfs_join_transaction(root);
3888 if (IS_ERR(trans))
3889 return -ENOSPC;
3890
3891 return btrfs_commit_transaction(trans, root);
3892}
3893
Josef Bacik96c3f432012-06-21 14:05:49 -04003894enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04003895 FLUSH_DELAYED_ITEMS_NR = 1,
3896 FLUSH_DELAYED_ITEMS = 2,
3897 FLUSH_DELALLOC = 3,
3898 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04003899 ALLOC_CHUNK = 5,
3900 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04003901};
3902
3903static int flush_space(struct btrfs_root *root,
3904 struct btrfs_space_info *space_info, u64 num_bytes,
3905 u64 orig_bytes, int state)
3906{
3907 struct btrfs_trans_handle *trans;
3908 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003909 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04003910
3911 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04003912 case FLUSH_DELAYED_ITEMS_NR:
3913 case FLUSH_DELAYED_ITEMS:
3914 if (state == FLUSH_DELAYED_ITEMS_NR) {
3915 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3916
3917 nr = (int)div64_u64(num_bytes, bytes);
3918 if (!nr)
3919 nr = 1;
3920 nr *= 2;
3921 } else {
3922 nr = -1;
3923 }
3924 trans = btrfs_join_transaction(root);
3925 if (IS_ERR(trans)) {
3926 ret = PTR_ERR(trans);
3927 break;
3928 }
3929 ret = btrfs_run_delayed_items_nr(trans, root, nr);
3930 btrfs_end_transaction(trans, root);
3931 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003932 case FLUSH_DELALLOC:
3933 case FLUSH_DELALLOC_WAIT:
3934 shrink_delalloc(root, num_bytes, orig_bytes,
3935 state == FLUSH_DELALLOC_WAIT);
3936 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04003937 case ALLOC_CHUNK:
3938 trans = btrfs_join_transaction(root);
3939 if (IS_ERR(trans)) {
3940 ret = PTR_ERR(trans);
3941 break;
3942 }
3943 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04003944 btrfs_get_alloc_profile(root, 0),
3945 CHUNK_ALLOC_NO_FORCE);
3946 btrfs_end_transaction(trans, root);
3947 if (ret == -ENOSPC)
3948 ret = 0;
3949 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04003950 case COMMIT_TRANS:
3951 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3952 break;
3953 default:
3954 ret = -ENOSPC;
3955 break;
3956 }
3957
3958 return ret;
3959}
Josef Bacik663350a2011-11-03 22:54:25 -04003960/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003961 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3962 * @root - the root we're allocating for
3963 * @block_rsv - the block_rsv we're allocating for
3964 * @orig_bytes - the number of bytes we want
3965 * @flush - wether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003966 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003967 * This will reserve orgi_bytes number of bytes from the space info associated
3968 * with the block_rsv. If there is not enough space it will make an attempt to
3969 * flush out space to make room. It will do this by flushing delalloc if
3970 * possible or committing the transaction. If flush is 0 then no attempts to
3971 * regain reservations will be made and this will fail if there is not enough
3972 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003973 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003974static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003975 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00003976 u64 orig_bytes,
3977 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003978{
3979 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04003980 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003981 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003982 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003983 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003984 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003985
3986again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003987 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003988 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003989 /*
Miao Xie08e007d2012-10-16 11:33:38 +00003990 * We only want to wait if somebody other than us is flushing and we
3991 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003992 */
Miao Xie08e007d2012-10-16 11:33:38 +00003993 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
3994 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003995 spin_unlock(&space_info->lock);
3996 /*
3997 * If we have a trans handle we can't wait because the flusher
3998 * may have to commit the transaction, which would mean we would
3999 * deadlock since we are waiting for the flusher to finish, but
4000 * hold the current transaction open.
4001 */
Josef Bacik663350a2011-11-03 22:54:25 -04004002 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004003 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004004 ret = wait_event_killable(space_info->wait, !space_info->flush);
4005 /* Must have been killed, return */
4006 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004007 return -EINTR;
4008
4009 spin_lock(&space_info->lock);
4010 }
4011
4012 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004013 used = space_info->bytes_used + space_info->bytes_reserved +
4014 space_info->bytes_pinned + space_info->bytes_readonly +
4015 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004016
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004017 /*
4018 * The idea here is that we've not already over-reserved the block group
4019 * then we can go ahead and save our reservation first and then start
4020 * flushing if we need to. Otherwise if we've already overcommitted
4021 * lets start flushing stuff first and then come back and try to make
4022 * our reservation.
4023 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004024 if (used <= space_info->total_bytes) {
4025 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004026 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004027 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004028 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004029 ret = 0;
4030 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004031 /*
4032 * Ok set num_bytes to orig_bytes since we aren't
4033 * overocmmitted, this way we only try and reclaim what
4034 * we need.
4035 */
4036 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004037 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004038 } else {
4039 /*
4040 * Ok we're over committed, set num_bytes to the overcommitted
4041 * amount plus the amount of bytes that we need for this
4042 * reservation.
4043 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004044 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004045 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004046 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004047
Josef Bacik44734ed2012-09-28 16:04:19 -04004048 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4049 space_info->bytes_may_use += orig_bytes;
4050 trace_btrfs_space_reservation(root->fs_info, "space_info",
4051 space_info->flags, orig_bytes,
4052 1);
4053 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004054 }
4055
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004056 /*
4057 * Couldn't make our reservation, save our place so while we're trying
4058 * to reclaim space we can actually use it instead of somebody else
4059 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004060 *
4061 * We make the other tasks wait for the flush only when we can flush
4062 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004063 */
Miao Xie08e007d2012-10-16 11:33:38 +00004064 if (ret && flush == BTRFS_RESERVE_FLUSH_ALL) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004065 flushing = true;
4066 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004067 }
4068
Yan, Zhengf0486c62010-05-16 10:46:25 -04004069 spin_unlock(&space_info->lock);
4070
Miao Xie08e007d2012-10-16 11:33:38 +00004071 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004072 goto out;
4073
Josef Bacik96c3f432012-06-21 14:05:49 -04004074 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4075 flush_state);
4076 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004077
4078 /*
4079 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4080 * would happen. So skip delalloc flush.
4081 */
4082 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4083 (flush_state == FLUSH_DELALLOC ||
4084 flush_state == FLUSH_DELALLOC_WAIT))
4085 flush_state = ALLOC_CHUNK;
4086
Josef Bacik96c3f432012-06-21 14:05:49 -04004087 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004088 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004089 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4090 flush_state < COMMIT_TRANS)
4091 goto again;
4092 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4093 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004094 goto again;
4095
4096out:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004097 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004098 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004099 space_info->flush = 0;
4100 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004101 spin_unlock(&space_info->lock);
4102 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004103 return ret;
4104}
4105
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106static struct btrfs_block_rsv *get_block_rsv(
4107 const struct btrfs_trans_handle *trans,
4108 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004109{
Josef Bacik4c13d752011-08-30 11:31:29 -04004110 struct btrfs_block_rsv *block_rsv = NULL;
4111
Josef Bacik0e721102012-06-26 16:13:18 -04004112 if (root->ref_cows)
4113 block_rsv = trans->block_rsv;
4114
4115 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004116 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004117
4118 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004119 block_rsv = root->block_rsv;
4120
4121 if (!block_rsv)
4122 block_rsv = &root->fs_info->empty_block_rsv;
4123
4124 return block_rsv;
4125}
4126
4127static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4128 u64 num_bytes)
4129{
4130 int ret = -ENOSPC;
4131 spin_lock(&block_rsv->lock);
4132 if (block_rsv->reserved >= num_bytes) {
4133 block_rsv->reserved -= num_bytes;
4134 if (block_rsv->reserved < block_rsv->size)
4135 block_rsv->full = 0;
4136 ret = 0;
4137 }
4138 spin_unlock(&block_rsv->lock);
4139 return ret;
4140}
4141
4142static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4143 u64 num_bytes, int update_size)
4144{
4145 spin_lock(&block_rsv->lock);
4146 block_rsv->reserved += num_bytes;
4147 if (update_size)
4148 block_rsv->size += num_bytes;
4149 else if (block_rsv->reserved >= block_rsv->size)
4150 block_rsv->full = 1;
4151 spin_unlock(&block_rsv->lock);
4152}
4153
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004154static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4155 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004156 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004157{
4158 struct btrfs_space_info *space_info = block_rsv->space_info;
4159
4160 spin_lock(&block_rsv->lock);
4161 if (num_bytes == (u64)-1)
4162 num_bytes = block_rsv->size;
4163 block_rsv->size -= num_bytes;
4164 if (block_rsv->reserved >= block_rsv->size) {
4165 num_bytes = block_rsv->reserved - block_rsv->size;
4166 block_rsv->reserved = block_rsv->size;
4167 block_rsv->full = 1;
4168 } else {
4169 num_bytes = 0;
4170 }
4171 spin_unlock(&block_rsv->lock);
4172
4173 if (num_bytes > 0) {
4174 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004175 spin_lock(&dest->lock);
4176 if (!dest->full) {
4177 u64 bytes_to_add;
4178
4179 bytes_to_add = dest->size - dest->reserved;
4180 bytes_to_add = min(num_bytes, bytes_to_add);
4181 dest->reserved += bytes_to_add;
4182 if (dest->reserved >= dest->size)
4183 dest->full = 1;
4184 num_bytes -= bytes_to_add;
4185 }
4186 spin_unlock(&dest->lock);
4187 }
4188 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004189 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004190 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004191 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004192 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004193 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004194 spin_unlock(&space_info->lock);
4195 }
4196 }
4197}
4198
4199static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4200 struct btrfs_block_rsv *dst, u64 num_bytes)
4201{
4202 int ret;
4203
4204 ret = block_rsv_use_bytes(src, num_bytes);
4205 if (ret)
4206 return ret;
4207
4208 block_rsv_add_bytes(dst, num_bytes, 1);
4209 return 0;
4210}
4211
Miao Xie66d8f3d2012-09-06 04:02:28 -06004212void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004213{
4214 memset(rsv, 0, sizeof(*rsv));
4215 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004216 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004217}
4218
Miao Xie66d8f3d2012-09-06 04:02:28 -06004219struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4220 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004221{
4222 struct btrfs_block_rsv *block_rsv;
4223 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004224
4225 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4226 if (!block_rsv)
4227 return NULL;
4228
Miao Xie66d8f3d2012-09-06 04:02:28 -06004229 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004230 block_rsv->space_info = __find_space_info(fs_info,
4231 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004232 return block_rsv;
4233}
4234
4235void btrfs_free_block_rsv(struct btrfs_root *root,
4236 struct btrfs_block_rsv *rsv)
4237{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004238 if (!rsv)
4239 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004240 btrfs_block_rsv_release(root, rsv, (u64)-1);
4241 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004242}
4243
Miao Xie08e007d2012-10-16 11:33:38 +00004244int btrfs_block_rsv_add(struct btrfs_root *root,
4245 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4246 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004247{
4248 int ret;
4249
4250 if (num_bytes == 0)
4251 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004252
Miao Xie61b520a2011-11-10 20:45:05 -05004253 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004254 if (!ret) {
4255 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4256 return 0;
4257 }
4258
Yan, Zhengf0486c62010-05-16 10:46:25 -04004259 return ret;
4260}
4261
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004262int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004263 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004264{
4265 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004266 int ret = -ENOSPC;
4267
4268 if (!block_rsv)
4269 return 0;
4270
4271 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004272 num_bytes = div_factor(block_rsv->size, min_factor);
4273 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004274 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004275 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004276
Josef Bacik36ba0222011-10-18 12:15:48 -04004277 return ret;
4278}
4279
Miao Xie08e007d2012-10-16 11:33:38 +00004280int btrfs_block_rsv_refill(struct btrfs_root *root,
4281 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4282 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004283{
4284 u64 num_bytes = 0;
4285 int ret = -ENOSPC;
4286
4287 if (!block_rsv)
4288 return 0;
4289
4290 spin_lock(&block_rsv->lock);
4291 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004292 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004293 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004294 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004295 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004296 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004297
Yan, Zhengf0486c62010-05-16 10:46:25 -04004298 if (!ret)
4299 return 0;
4300
Miao Xieaa38a712011-11-18 17:43:00 +08004301 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004302 if (!ret) {
4303 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004304 return 0;
4305 }
4306
Josef Bacik13553e52011-08-08 13:33:21 -04004307 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004308}
4309
4310int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4311 struct btrfs_block_rsv *dst_rsv,
4312 u64 num_bytes)
4313{
4314 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4315}
4316
4317void btrfs_block_rsv_release(struct btrfs_root *root,
4318 struct btrfs_block_rsv *block_rsv,
4319 u64 num_bytes)
4320{
4321 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4322 if (global_rsv->full || global_rsv == block_rsv ||
4323 block_rsv->space_info != global_rsv->space_info)
4324 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004325 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4326 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004327}
4328
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004329/*
4330 * helper to calculate size of global block reservation.
4331 * the desired value is sum of space used by extent tree,
4332 * checksum tree and root tree
4333 */
4334static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4335{
4336 struct btrfs_space_info *sinfo;
4337 u64 num_bytes;
4338 u64 meta_used;
4339 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004340 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004341
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004342 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4343 spin_lock(&sinfo->lock);
4344 data_used = sinfo->bytes_used;
4345 spin_unlock(&sinfo->lock);
4346
4347 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4348 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004349 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4350 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004351 meta_used = sinfo->bytes_used;
4352 spin_unlock(&sinfo->lock);
4353
4354 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4355 csum_size * 2;
4356 num_bytes += div64_u64(data_used + meta_used, 50);
4357
4358 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004359 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004360
4361 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4362}
4363
4364static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4365{
4366 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4367 struct btrfs_space_info *sinfo = block_rsv->space_info;
4368 u64 num_bytes;
4369
4370 num_bytes = calc_global_metadata_size(fs_info);
4371
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004372 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004373 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004374
4375 block_rsv->size = num_bytes;
4376
4377 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004378 sinfo->bytes_reserved + sinfo->bytes_readonly +
4379 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004380
4381 if (sinfo->total_bytes > num_bytes) {
4382 num_bytes = sinfo->total_bytes - num_bytes;
4383 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004384 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004385 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004386 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004387 }
4388
4389 if (block_rsv->reserved >= block_rsv->size) {
4390 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004391 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004392 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004393 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004394 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004395 block_rsv->reserved = block_rsv->size;
4396 block_rsv->full = 1;
4397 }
David Sterba182608c2011-05-05 13:13:16 +02004398
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004399 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004400 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004401}
4402
Yan, Zhengf0486c62010-05-16 10:46:25 -04004403static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4404{
4405 struct btrfs_space_info *space_info;
4406
4407 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4408 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004409
4410 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004411 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004412 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004413 fs_info->trans_block_rsv.space_info = space_info;
4414 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004415 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004416
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004417 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4418 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4419 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4420 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004421 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004422
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004423 update_global_block_rsv(fs_info);
4424}
4425
4426static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4427{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004428 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4429 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004430 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4431 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4432 WARN_ON(fs_info->trans_block_rsv.size > 0);
4433 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4434 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4435 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004436 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4437 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004438}
4439
Yan, Zhenga22285a2010-05-16 10:48:46 -04004440void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4441 struct btrfs_root *root)
4442{
Josef Bacik0e721102012-06-26 16:13:18 -04004443 if (!trans->block_rsv)
4444 return;
4445
Yan, Zhenga22285a2010-05-16 10:48:46 -04004446 if (!trans->bytes_reserved)
4447 return;
4448
Chris Masone77266e2012-02-24 10:39:05 -05004449 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004450 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004451 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004452 trans->bytes_reserved = 0;
4453}
4454
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004455/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004456int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4457 struct inode *inode)
4458{
4459 struct btrfs_root *root = BTRFS_I(inode)->root;
4460 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4461 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4462
4463 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004464 * We need to hold space in order to delete our orphan item once we've
4465 * added it, so this takes the reservation so we can release it later
4466 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004467 */
Chris Masonff5714c2011-05-28 07:00:39 -04004468 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004469 trace_btrfs_space_reservation(root->fs_info, "orphan",
4470 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004471 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4472}
4473
4474void btrfs_orphan_release_metadata(struct inode *inode)
4475{
4476 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004477 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004478 trace_btrfs_space_reservation(root->fs_info, "orphan",
4479 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004480 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4481}
4482
Yan, Zhenga22285a2010-05-16 10:48:46 -04004483int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4484 struct btrfs_pending_snapshot *pending)
4485{
4486 struct btrfs_root *root = pending->root;
4487 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4488 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4489 /*
Miao Xie48c03c42012-09-06 04:03:56 -06004490 * two for root back/forward refs, two for directory entries,
4491 * one for root of the snapshot and one for parent inode.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004492 */
Miao Xie48c03c42012-09-06 04:03:56 -06004493 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004494 dst_rsv->space_info = src_rsv->space_info;
4495 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4496}
4497
Josef Bacik7709cde2011-08-04 10:25:02 -04004498/**
4499 * drop_outstanding_extent - drop an outstanding extent
4500 * @inode: the inode we're dropping the extent for
4501 *
4502 * This is called when we are freeing up an outstanding extent, either called
4503 * after an error or after an extent is written. This will return the number of
4504 * reserved extents that need to be freed. This must be called with
4505 * BTRFS_I(inode)->lock held.
4506 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004507static unsigned drop_outstanding_extent(struct inode *inode)
4508{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004509 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004510 unsigned dropped_extents = 0;
4511
Josef Bacik9e0baf62011-07-15 15:16:44 +00004512 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4513 BTRFS_I(inode)->outstanding_extents--;
4514
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004515 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004516 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4517 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004518 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004519
Josef Bacik9e0baf62011-07-15 15:16:44 +00004520 /*
4521 * If we have more or the same amount of outsanding extents than we have
4522 * reserved then we need to leave the reserved extents count alone.
4523 */
4524 if (BTRFS_I(inode)->outstanding_extents >=
4525 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004526 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004527
4528 dropped_extents = BTRFS_I(inode)->reserved_extents -
4529 BTRFS_I(inode)->outstanding_extents;
4530 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004531 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004532}
4533
Josef Bacik7709cde2011-08-04 10:25:02 -04004534/**
4535 * calc_csum_metadata_size - return the amount of metada space that must be
4536 * reserved/free'd for the given bytes.
4537 * @inode: the inode we're manipulating
4538 * @num_bytes: the number of bytes in question
4539 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4540 *
4541 * This adjusts the number of csum_bytes in the inode and then returns the
4542 * correct amount of metadata that must either be reserved or freed. We
4543 * calculate how many checksums we can fit into one leaf and then divide the
4544 * number of bytes that will need to be checksumed by this value to figure out
4545 * how many checksums will be required. If we are adding bytes then the number
4546 * may go up and we will return the number of additional bytes that must be
4547 * reserved. If it is going down we will return the number of bytes that must
4548 * be freed.
4549 *
4550 * This must be called with BTRFS_I(inode)->lock held.
4551 */
4552static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4553 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004554{
Josef Bacik7709cde2011-08-04 10:25:02 -04004555 struct btrfs_root *root = BTRFS_I(inode)->root;
4556 u64 csum_size;
4557 int num_csums_per_leaf;
4558 int num_csums;
4559 int old_csums;
4560
4561 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4562 BTRFS_I(inode)->csum_bytes == 0)
4563 return 0;
4564
4565 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4566 if (reserve)
4567 BTRFS_I(inode)->csum_bytes += num_bytes;
4568 else
4569 BTRFS_I(inode)->csum_bytes -= num_bytes;
4570 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4571 num_csums_per_leaf = (int)div64_u64(csum_size,
4572 sizeof(struct btrfs_csum_item) +
4573 sizeof(struct btrfs_disk_key));
4574 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4575 num_csums = num_csums + num_csums_per_leaf - 1;
4576 num_csums = num_csums / num_csums_per_leaf;
4577
4578 old_csums = old_csums + num_csums_per_leaf - 1;
4579 old_csums = old_csums / num_csums_per_leaf;
4580
4581 /* No change, no need to reserve more */
4582 if (old_csums == num_csums)
4583 return 0;
4584
4585 if (reserve)
4586 return btrfs_calc_trans_metadata_size(root,
4587 num_csums - old_csums);
4588
4589 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004590}
4591
4592int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4593{
4594 struct btrfs_root *root = BTRFS_I(inode)->root;
4595 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004596 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004597 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004598 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004599 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004600 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004601 int ret;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004602 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004603
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004604 /* If we are a free space inode we need to not flush since we will be in
4605 * the middle of a transaction commit. We also don't need the delalloc
4606 * mutex since we won't race with anybody. We need this mostly to make
4607 * lockdep shut its filthy mouth.
4608 */
4609 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004610 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004611 delalloc_lock = false;
4612 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004613
Miao Xie08e007d2012-10-16 11:33:38 +00004614 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4615 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004616 schedule_timeout(1);
4617
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004618 if (delalloc_lock)
4619 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4620
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004621 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004622
Josef Bacik9e0baf62011-07-15 15:16:44 +00004623 spin_lock(&BTRFS_I(inode)->lock);
4624 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004625
Josef Bacik9e0baf62011-07-15 15:16:44 +00004626 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004627 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004628 nr_extents = BTRFS_I(inode)->outstanding_extents -
4629 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004630
4631 /*
4632 * Add an item to reserve for updating the inode when we complete the
4633 * delalloc io.
4634 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004635 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4636 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004637 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004638 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004639 }
4640
4641 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004642 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004643 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004644 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004645
Arne Jansenc5567232011-09-14 15:44:05 +02004646 if (root->fs_info->quota_enabled) {
4647 ret = btrfs_qgroup_reserve(root, num_bytes +
4648 nr_extents * root->leafsize);
Dan Carpenter55e591f2012-07-30 02:15:15 -06004649 if (ret) {
Miao Xie4b5829a2012-12-05 10:53:25 +00004650 spin_lock(&BTRFS_I(inode)->lock);
4651 calc_csum_metadata_size(inode, num_bytes, 0);
4652 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004653 if (delalloc_lock)
4654 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Arne Jansenc5567232011-09-14 15:44:05 +02004655 return ret;
Dan Carpenter55e591f2012-07-30 02:15:15 -06004656 }
Arne Jansenc5567232011-09-14 15:44:05 +02004657 }
4658
Josef Bacik36ba0222011-10-18 12:15:48 -04004659 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004660 if (ret) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004661 u64 to_free = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004662 unsigned dropped;
Josef Bacik7ed49f12011-08-19 15:45:52 -04004663
Josef Bacik7709cde2011-08-04 10:25:02 -04004664 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004665 dropped = drop_outstanding_extent(inode);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004666 /*
Josef Bacik660d3f62011-12-09 11:18:51 -05004667 * If the inodes csum_bytes is the same as the original
4668 * csum_bytes then we know we haven't raced with any free()ers
4669 * so we can just reduce our inodes csum bytes and carry on.
4670 * Otherwise we have to do the normal free thing to account for
4671 * the case that the free side didn't free up its reserve
4672 * because of this outstanding reservation.
Josef Bacik7ed49f12011-08-19 15:45:52 -04004673 */
Josef Bacik660d3f62011-12-09 11:18:51 -05004674 if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4675 calc_csum_metadata_size(inode, num_bytes, 0);
4676 else
4677 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4678 spin_unlock(&BTRFS_I(inode)->lock);
4679 if (dropped)
4680 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4681
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004682 if (to_free) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004683 btrfs_block_rsv_release(root, block_rsv, to_free);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004684 trace_btrfs_space_reservation(root->fs_info,
4685 "delalloc",
4686 btrfs_ino(inode),
4687 to_free, 0);
4688 }
Miao Xie4b5829a2012-12-05 10:53:25 +00004689 if (root->fs_info->quota_enabled) {
4690 btrfs_qgroup_free(root, num_bytes +
4691 nr_extents * root->leafsize);
4692 }
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004693 if (delalloc_lock)
4694 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004695 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004696 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004697
Josef Bacik660d3f62011-12-09 11:18:51 -05004698 spin_lock(&BTRFS_I(inode)->lock);
4699 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004700 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4701 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004702 nr_extents--;
4703 }
4704 BTRFS_I(inode)->reserved_extents += nr_extents;
4705 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004706
4707 if (delalloc_lock)
4708 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004709
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004710 if (to_reserve)
4711 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4712 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004713 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4714
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004715 return 0;
4716}
4717
Josef Bacik7709cde2011-08-04 10:25:02 -04004718/**
4719 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4720 * @inode: the inode to release the reservation for
4721 * @num_bytes: the number of bytes we're releasing
4722 *
4723 * This will release the metadata reservation for an inode. This can be called
4724 * once we complete IO for a given set of bytes to release their metadata
4725 * reservations.
4726 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004727void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4728{
4729 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004730 u64 to_free = 0;
4731 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004732
4733 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004734 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004735 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004736
Josef Bacik7709cde2011-08-04 10:25:02 -04004737 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4738 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004739 if (dropped > 0)
4740 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004741
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004742 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4743 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004744 if (root->fs_info->quota_enabled) {
4745 btrfs_qgroup_free(root, num_bytes +
4746 dropped * root->leafsize);
4747 }
4748
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004749 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4750 to_free);
4751}
4752
Josef Bacik7709cde2011-08-04 10:25:02 -04004753/**
4754 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4755 * @inode: inode we're writing to
4756 * @num_bytes: the number of bytes we want to allocate
4757 *
4758 * This will do the following things
4759 *
4760 * o reserve space in the data space info for num_bytes
4761 * o reserve space in the metadata space info based on number of outstanding
4762 * extents and how much csums will be needed
4763 * o add to the inodes ->delalloc_bytes
4764 * o add it to the fs_info's delalloc inodes list.
4765 *
4766 * This will return 0 for success and -ENOSPC if there is no space left.
4767 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004768int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4769{
4770 int ret;
4771
4772 ret = btrfs_check_data_free_space(inode, num_bytes);
4773 if (ret)
4774 return ret;
4775
4776 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4777 if (ret) {
4778 btrfs_free_reserved_data_space(inode, num_bytes);
4779 return ret;
4780 }
4781
4782 return 0;
4783}
4784
Josef Bacik7709cde2011-08-04 10:25:02 -04004785/**
4786 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4787 * @inode: inode we're releasing space for
4788 * @num_bytes: the number of bytes we want to free up
4789 *
4790 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4791 * called in the case that we don't need the metadata AND data reservations
4792 * anymore. So if there is an error or we insert an inline extent.
4793 *
4794 * This function will release the metadata space that was not used and will
4795 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4796 * list if there are no delalloc bytes left.
4797 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004798void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4799{
4800 btrfs_delalloc_release_metadata(inode, num_bytes);
4801 btrfs_free_reserved_data_space(inode, num_bytes);
4802}
4803
Chris Mason9078a3e2007-04-26 16:46:15 -04004804static int update_block_group(struct btrfs_trans_handle *trans,
4805 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004806 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004807{
Josef Bacik0af3d002010-06-21 14:48:16 -04004808 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004809 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004810 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004811 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004812 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004813 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004814
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004815 /* block accounting for super block */
4816 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02004817 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004818 if (alloc)
4819 old_val += num_bytes;
4820 else
4821 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02004822 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004823 spin_unlock(&info->delalloc_lock);
4824
Chris Masond3977122009-01-05 21:25:51 -05004825 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004826 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004827 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004828 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004829 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4830 BTRFS_BLOCK_GROUP_RAID1 |
4831 BTRFS_BLOCK_GROUP_RAID10))
4832 factor = 2;
4833 else
4834 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004835 /*
4836 * If this block group has free space cache written out, we
4837 * need to make sure to load it if we are removing space. This
4838 * is because we need the unpinning stage to actually add the
4839 * space back to the block group, otherwise we will leak space.
4840 */
4841 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004842 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004843
Chris Masondb945352007-10-15 16:15:53 -04004844 byte_in_group = bytenr - cache->key.objectid;
4845 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004846
Josef Bacik25179202008-10-29 14:49:05 -04004847 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004848 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004849
Josef Bacik73bc1872011-10-03 14:07:49 -04004850 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04004851 cache->disk_cache_state < BTRFS_DC_CLEAR)
4852 cache->disk_cache_state = BTRFS_DC_CLEAR;
4853
Josef Bacik0f9dd462008-09-23 13:14:11 -04004854 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004855 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004856 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004857 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004858 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004859 btrfs_set_block_group_used(&cache->item, old_val);
4860 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004861 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004862 cache->space_info->bytes_used += num_bytes;
4863 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004864 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004865 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004866 } else {
Chris Masondb945352007-10-15 16:15:53 -04004867 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004868 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004869 cache->pinned += num_bytes;
4870 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004871 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004872 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004873 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004874 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004875
Yan, Zhengf0486c62010-05-16 10:46:25 -04004876 set_extent_dirty(info->pinned_extents,
4877 bytenr, bytenr + num_bytes - 1,
4878 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004879 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004880 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004881 total -= num_bytes;
4882 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004883 }
4884 return 0;
4885}
Chris Mason6324fbf2008-03-24 15:01:59 -04004886
Chris Masona061fc82008-05-07 11:43:44 -04004887static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4888{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004889 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004890 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004891
4892 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4893 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004894 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004895
Yan Zhengd2fb3432008-12-11 16:30:39 -05004896 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004897 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004898
4899 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004900}
4901
Yan, Zhengf0486c62010-05-16 10:46:25 -04004902static int pin_down_extent(struct btrfs_root *root,
4903 struct btrfs_block_group_cache *cache,
4904 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004905{
Yan Zheng11833d62009-09-11 16:11:19 -04004906 spin_lock(&cache->space_info->lock);
4907 spin_lock(&cache->lock);
4908 cache->pinned += num_bytes;
4909 cache->space_info->bytes_pinned += num_bytes;
4910 if (reserved) {
4911 cache->reserved -= num_bytes;
4912 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004913 }
Yan Zheng11833d62009-09-11 16:11:19 -04004914 spin_unlock(&cache->lock);
4915 spin_unlock(&cache->space_info->lock);
4916
Yan, Zhengf0486c62010-05-16 10:46:25 -04004917 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4918 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004919 return 0;
4920}
Chris Mason9078a3e2007-04-26 16:46:15 -04004921
Yan, Zhengf0486c62010-05-16 10:46:25 -04004922/*
4923 * this function must be called within transaction
4924 */
4925int btrfs_pin_extent(struct btrfs_root *root,
4926 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004927{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004928 struct btrfs_block_group_cache *cache;
4929
4930 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004931 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04004932
4933 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4934
4935 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004936 return 0;
4937}
Zheng Yane8569812008-09-26 10:05:48 -04004938
Yan, Zhengf0486c62010-05-16 10:46:25 -04004939/*
Chris Masone688b722011-10-31 20:52:39 -04004940 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04004941 */
Chris Masone688b722011-10-31 20:52:39 -04004942int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
4943 struct btrfs_root *root,
4944 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004945{
Chris Masone688b722011-10-31 20:52:39 -04004946 struct btrfs_block_group_cache *cache;
4947
4948 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004949 BUG_ON(!cache); /* Logic error */
Chris Masone688b722011-10-31 20:52:39 -04004950
4951 /*
4952 * pull in the free space cache (if any) so that our pin
4953 * removes the free space from the cache. We have load_only set
4954 * to one because the slow code to read in the free extents does check
4955 * the pinned extents.
4956 */
4957 cache_block_group(cache, trans, root, 1);
4958
4959 pin_down_extent(root, cache, bytenr, num_bytes, 0);
4960
4961 /* remove us from the free space cache (if we're there at all) */
4962 btrfs_remove_free_space(cache, bytenr, num_bytes);
4963 btrfs_put_block_group(cache);
4964 return 0;
4965}
4966
Josef Bacikfb25e912011-07-26 17:00:46 -04004967/**
4968 * btrfs_update_reserved_bytes - update the block_group and space info counters
4969 * @cache: The cache we are manipulating
4970 * @num_bytes: The number of bytes in question
4971 * @reserve: One of the reservation enums
4972 *
4973 * This is called by the allocator when it reserves space, or by somebody who is
4974 * freeing space that was never actually used on disk. For example if you
4975 * reserve some space for a new leaf in transaction A and before transaction A
4976 * commits you free that leaf, you call this with reserve set to 0 in order to
4977 * clear the reservation.
4978 *
4979 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4980 * ENOSPC accounting. For data we handle the reservation through clearing the
4981 * delalloc bits in the io_tree. We have to do this since we could end up
4982 * allocating less disk space for the amount of data we have reserved in the
4983 * case of compression.
4984 *
4985 * If this is a reservation and the block group has become read only we cannot
4986 * make the reservation and return -EAGAIN, otherwise this function always
4987 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04004988 */
Josef Bacikfb25e912011-07-26 17:00:46 -04004989static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4990 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004991{
Josef Bacikfb25e912011-07-26 17:00:46 -04004992 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004993 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004994
Josef Bacikfb25e912011-07-26 17:00:46 -04004995 spin_lock(&space_info->lock);
4996 spin_lock(&cache->lock);
4997 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004998 if (cache->ro) {
4999 ret = -EAGAIN;
5000 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005001 cache->reserved += num_bytes;
5002 space_info->bytes_reserved += num_bytes;
5003 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005004 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005005 "space_info", space_info->flags,
5006 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005007 space_info->bytes_may_use -= num_bytes;
5008 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005009 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005010 } else {
5011 if (cache->ro)
5012 space_info->bytes_readonly += num_bytes;
5013 cache->reserved -= num_bytes;
5014 space_info->bytes_reserved -= num_bytes;
5015 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005016 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005017 spin_unlock(&cache->lock);
5018 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005019 return ret;
5020}
5021
Jeff Mahoney143bede2012-03-01 14:56:26 +01005022void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005023 struct btrfs_root *root)
5024{
5025 struct btrfs_fs_info *fs_info = root->fs_info;
5026 struct btrfs_caching_control *next;
5027 struct btrfs_caching_control *caching_ctl;
5028 struct btrfs_block_group_cache *cache;
5029
5030 down_write(&fs_info->extent_commit_sem);
5031
5032 list_for_each_entry_safe(caching_ctl, next,
5033 &fs_info->caching_block_groups, list) {
5034 cache = caching_ctl->block_group;
5035 if (block_group_cache_done(cache)) {
5036 cache->last_byte_to_unpin = (u64)-1;
5037 list_del_init(&caching_ctl->list);
5038 put_caching_control(caching_ctl);
5039 } else {
5040 cache->last_byte_to_unpin = caching_ctl->progress;
5041 }
5042 }
5043
5044 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5045 fs_info->pinned_extents = &fs_info->freed_extents[1];
5046 else
5047 fs_info->pinned_extents = &fs_info->freed_extents[0];
5048
5049 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005050
5051 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005052}
5053
5054static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5055{
5056 struct btrfs_fs_info *fs_info = root->fs_info;
5057 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005058 struct btrfs_space_info *space_info;
5059 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005060 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005061 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005062
5063 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005064 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005065 if (!cache ||
5066 start >= cache->key.objectid + cache->key.offset) {
5067 if (cache)
5068 btrfs_put_block_group(cache);
5069 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005070 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005071 }
5072
5073 len = cache->key.objectid + cache->key.offset - start;
5074 len = min(len, end + 1 - start);
5075
5076 if (start < cache->last_byte_to_unpin) {
5077 len = min(len, cache->last_byte_to_unpin - start);
5078 btrfs_add_free_space(cache, start, len);
5079 }
Josef Bacik25179202008-10-29 14:49:05 -04005080
Yan, Zhengf0486c62010-05-16 10:46:25 -04005081 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005082 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005083
Josef Bacik7b398f82012-10-22 15:52:28 -04005084 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005085 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005086 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005087 space_info->bytes_pinned -= len;
5088 if (cache->ro) {
5089 space_info->bytes_readonly += len;
5090 readonly = true;
5091 }
Josef Bacik25179202008-10-29 14:49:05 -04005092 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005093 if (!readonly && global_rsv->space_info == space_info) {
5094 spin_lock(&global_rsv->lock);
5095 if (!global_rsv->full) {
5096 len = min(len, global_rsv->size -
5097 global_rsv->reserved);
5098 global_rsv->reserved += len;
5099 space_info->bytes_may_use += len;
5100 if (global_rsv->reserved >= global_rsv->size)
5101 global_rsv->full = 1;
5102 }
5103 spin_unlock(&global_rsv->lock);
5104 }
5105 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005106 }
5107
5108 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005109 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005110 return 0;
5111}
5112
5113int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005114 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005115{
Yan Zheng11833d62009-09-11 16:11:19 -04005116 struct btrfs_fs_info *fs_info = root->fs_info;
5117 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005118 u64 start;
5119 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005120 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005121
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005122 if (trans->aborted)
5123 return 0;
5124
Yan Zheng11833d62009-09-11 16:11:19 -04005125 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5126 unpin = &fs_info->freed_extents[1];
5127 else
5128 unpin = &fs_info->freed_extents[0];
5129
Chris Masond3977122009-01-05 21:25:51 -05005130 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005131 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005132 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005133 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005134 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005135
Li Dongyang5378e602011-03-24 10:24:27 +00005136 if (btrfs_test_opt(root, DISCARD))
5137 ret = btrfs_discard_extent(root, start,
5138 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005139
Chris Mason1a5bc162007-10-15 16:15:26 -04005140 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005141 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005142 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005143 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005144
Chris Masone20d96d2007-03-22 12:13:20 -04005145 return 0;
5146}
5147
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005148static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5149 struct btrfs_root *root,
5150 u64 bytenr, u64 num_bytes, u64 parent,
5151 u64 root_objectid, u64 owner_objectid,
5152 u64 owner_offset, int refs_to_drop,
5153 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005154{
Chris Masone2fa7222007-03-12 16:22:34 -04005155 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005156 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005157 struct btrfs_fs_info *info = root->fs_info;
5158 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005159 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005160 struct btrfs_extent_item *ei;
5161 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005162 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005163 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005164 int extent_slot = 0;
5165 int found_extent = 0;
5166 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005167 u32 item_size;
5168 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05005169
Chris Mason5caf2a02007-04-02 11:20:42 -04005170 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005171 if (!path)
5172 return -ENOMEM;
5173
Chris Mason3c12ac72008-04-21 12:01:38 -04005174 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005175 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005176
5177 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5178 BUG_ON(!is_data && refs_to_drop != 1);
5179
5180 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5181 bytenr, num_bytes, parent,
5182 root_objectid, owner_objectid,
5183 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005184 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005185 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005186 while (extent_slot >= 0) {
5187 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005188 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005189 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005190 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005191 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5192 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005193 found_extent = 1;
5194 break;
5195 }
5196 if (path->slots[0] - extent_slot > 5)
5197 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005198 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005199 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005200#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5201 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5202 if (found_extent && item_size < sizeof(*ei))
5203 found_extent = 0;
5204#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005205 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005206 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005207 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005208 NULL, refs_to_drop,
5209 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005210 if (ret) {
5211 btrfs_abort_transaction(trans, extent_root, ret);
5212 goto out;
5213 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005214 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005215 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005216
5217 key.objectid = bytenr;
5218 key.type = BTRFS_EXTENT_ITEM_KEY;
5219 key.offset = num_bytes;
5220
Zheng Yan31840ae2008-09-23 13:14:14 -04005221 ret = btrfs_search_slot(trans, extent_root,
5222 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005223 if (ret) {
5224 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05005225 ", was looking for %llu\n", ret,
5226 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005227 if (ret > 0)
5228 btrfs_print_leaf(extent_root,
5229 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005230 }
David Sterba005d6422012-09-18 07:52:32 -06005231 if (ret < 0) {
5232 btrfs_abort_transaction(trans, extent_root, ret);
5233 goto out;
5234 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005235 extent_slot = path->slots[0];
5236 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005237 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005238 btrfs_print_leaf(extent_root, path->nodes[0]);
5239 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05005240 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005241 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05005242 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04005243 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05005244 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005245 (unsigned long long)owner_objectid,
5246 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005247 } else {
David Sterba005d6422012-09-18 07:52:32 -06005248 btrfs_abort_transaction(trans, extent_root, ret);
5249 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005250 }
Chris Mason5f39d392007-10-15 16:14:19 -04005251
5252 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005253 item_size = btrfs_item_size_nr(leaf, extent_slot);
5254#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5255 if (item_size < sizeof(*ei)) {
5256 BUG_ON(found_extent || extent_slot != path->slots[0]);
5257 ret = convert_extent_item_v0(trans, extent_root, path,
5258 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005259 if (ret < 0) {
5260 btrfs_abort_transaction(trans, extent_root, ret);
5261 goto out;
5262 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005263
David Sterbab3b4aa72011-04-21 01:20:15 +02005264 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005265 path->leave_spinning = 1;
5266
5267 key.objectid = bytenr;
5268 key.type = BTRFS_EXTENT_ITEM_KEY;
5269 key.offset = num_bytes;
5270
5271 ret = btrfs_search_slot(trans, extent_root, &key, path,
5272 -1, 1);
5273 if (ret) {
5274 printk(KERN_ERR "umm, got %d back from search"
5275 ", was looking for %llu\n", ret,
5276 (unsigned long long)bytenr);
5277 btrfs_print_leaf(extent_root, path->nodes[0]);
5278 }
David Sterba005d6422012-09-18 07:52:32 -06005279 if (ret < 0) {
5280 btrfs_abort_transaction(trans, extent_root, ret);
5281 goto out;
5282 }
5283
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005284 extent_slot = path->slots[0];
5285 leaf = path->nodes[0];
5286 item_size = btrfs_item_size_nr(leaf, extent_slot);
5287 }
5288#endif
5289 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005290 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005291 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005292 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5293 struct btrfs_tree_block_info *bi;
5294 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5295 bi = (struct btrfs_tree_block_info *)(ei + 1);
5296 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005297 }
5298
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005299 refs = btrfs_extent_refs(leaf, ei);
5300 BUG_ON(refs < refs_to_drop);
5301 refs -= refs_to_drop;
5302
5303 if (refs > 0) {
5304 if (extent_op)
5305 __run_delayed_extent_op(extent_op, leaf, ei);
5306 /*
5307 * In the case of inline back ref, reference count will
5308 * be updated by remove_extent_backref
5309 */
5310 if (iref) {
5311 BUG_ON(!found_extent);
5312 } else {
5313 btrfs_set_extent_refs(leaf, ei, refs);
5314 btrfs_mark_buffer_dirty(leaf);
5315 }
5316 if (found_extent) {
5317 ret = remove_extent_backref(trans, extent_root, path,
5318 iref, refs_to_drop,
5319 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005320 if (ret) {
5321 btrfs_abort_transaction(trans, extent_root, ret);
5322 goto out;
5323 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005324 }
5325 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005326 if (found_extent) {
5327 BUG_ON(is_data && refs_to_drop !=
5328 extent_data_ref_count(root, path, iref));
5329 if (iref) {
5330 BUG_ON(path->slots[0] != extent_slot);
5331 } else {
5332 BUG_ON(path->slots[0] != extent_slot + 1);
5333 path->slots[0] = extent_slot;
5334 num_to_del = 2;
5335 }
Chris Mason78fae272007-03-25 11:35:08 -04005336 }
Chris Masonb9473432009-03-13 11:00:37 -04005337
Chris Mason952fcca2008-02-18 16:33:44 -05005338 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5339 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005340 if (ret) {
5341 btrfs_abort_transaction(trans, extent_root, ret);
5342 goto out;
5343 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005344 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005345
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005346 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005347 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005348 if (ret) {
5349 btrfs_abort_transaction(trans, extent_root, ret);
5350 goto out;
5351 }
Chris Mason459931e2008-12-10 09:10:46 -05005352 }
5353
Yan, Zhengf0486c62010-05-16 10:46:25 -04005354 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005355 if (ret) {
5356 btrfs_abort_transaction(trans, extent_root, ret);
5357 goto out;
5358 }
Chris Masona28ec192007-03-06 20:08:01 -05005359 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005360out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005361 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005362 return ret;
5363}
5364
5365/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005366 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005367 * delayed ref for that extent as well. This searches the delayed ref tree for
5368 * a given extent, and if there are no other delayed refs to be processed, it
5369 * removes it from the tree.
5370 */
5371static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5372 struct btrfs_root *root, u64 bytenr)
5373{
5374 struct btrfs_delayed_ref_head *head;
5375 struct btrfs_delayed_ref_root *delayed_refs;
5376 struct btrfs_delayed_ref_node *ref;
5377 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005378 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005379
5380 delayed_refs = &trans->transaction->delayed_refs;
5381 spin_lock(&delayed_refs->lock);
5382 head = btrfs_find_delayed_ref_head(trans, bytenr);
5383 if (!head)
5384 goto out;
5385
5386 node = rb_prev(&head->node.rb_node);
5387 if (!node)
5388 goto out;
5389
5390 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5391
5392 /* there are still entries for this ref, we can't drop it */
5393 if (ref->bytenr == bytenr)
5394 goto out;
5395
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005396 if (head->extent_op) {
5397 if (!head->must_insert_reserved)
5398 goto out;
5399 kfree(head->extent_op);
5400 head->extent_op = NULL;
5401 }
5402
Chris Mason1887be62009-03-13 10:11:24 -04005403 /*
5404 * waiting for the lock here would deadlock. If someone else has it
5405 * locked they are already in the process of dropping it anyway
5406 */
5407 if (!mutex_trylock(&head->mutex))
5408 goto out;
5409
5410 /*
5411 * at this point we have a head with no other entries. Go
5412 * ahead and process it.
5413 */
5414 head->node.in_tree = 0;
5415 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005416
Chris Mason1887be62009-03-13 10:11:24 -04005417 delayed_refs->num_entries--;
5418
5419 /*
5420 * we don't take a ref on the node because we're removing it from the
5421 * tree, so we just steal the ref the tree was holding.
5422 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005423 delayed_refs->num_heads--;
5424 if (list_empty(&head->cluster))
5425 delayed_refs->num_heads_ready--;
5426
5427 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005428 spin_unlock(&delayed_refs->lock);
5429
Yan, Zhengf0486c62010-05-16 10:46:25 -04005430 BUG_ON(head->extent_op);
5431 if (head->must_insert_reserved)
5432 ret = 1;
5433
5434 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005435 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005436 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005437out:
5438 spin_unlock(&delayed_refs->lock);
5439 return 0;
5440}
5441
Yan, Zhengf0486c62010-05-16 10:46:25 -04005442void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5443 struct btrfs_root *root,
5444 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005445 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005446{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005447 struct btrfs_block_group_cache *cache = NULL;
5448 int ret;
5449
5450 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005451 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5452 buf->start, buf->len,
5453 parent, root->root_key.objectid,
5454 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005455 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005456 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005457 }
5458
5459 if (!last_ref)
5460 return;
5461
Yan, Zhengf0486c62010-05-16 10:46:25 -04005462 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005463
5464 if (btrfs_header_generation(buf) == trans->transid) {
5465 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5466 ret = check_ref_cleanup(trans, root, buf->start);
5467 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005468 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005469 }
5470
5471 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5472 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005473 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005474 }
5475
5476 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5477
5478 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005479 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005480 }
5481out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005482 /*
5483 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5484 * anymore.
5485 */
5486 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005487 btrfs_put_block_group(cache);
5488}
5489
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005490/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005491int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5492 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5493 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005494{
5495 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005496 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005497
Chris Mason56bec292009-03-13 10:10:06 -04005498 /*
5499 * tree log blocks never actually go into the extent allocation
5500 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005501 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005502 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5503 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005504 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005505 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005506 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005507 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005508 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5509 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005510 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005511 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005512 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005513 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5514 num_bytes,
5515 parent, root_objectid, owner,
5516 offset, BTRFS_DROP_DELAYED_REF,
5517 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005518 }
Chris Mason925baed2008-06-25 16:01:30 -04005519 return ret;
5520}
5521
David Woodhouse53b381b2013-01-29 18:40:14 -05005522static u64 stripe_align(struct btrfs_root *root,
5523 struct btrfs_block_group_cache *cache,
5524 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05005525{
David Woodhouse53b381b2013-01-29 18:40:14 -05005526 u64 mask;
5527 u64 ret;
5528 mask = ((u64)root->stripesize - 1);
5529 ret = (val + mask) & ~mask;
Chris Mason87ee04e2007-11-30 11:30:34 -05005530 return ret;
5531}
5532
Chris Masonfec577f2007-02-26 10:40:21 -05005533/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005534 * when we wait for progress in the block group caching, its because
5535 * our allocation attempt failed at least once. So, we must sleep
5536 * and let some progress happen before we try again.
5537 *
5538 * This function will sleep at least once waiting for new free space to
5539 * show up, and then it will check the block group free space numbers
5540 * for our min num_bytes. Another option is to have it go ahead
5541 * and look in the rbtree for a free extent of a given size, but this
5542 * is a good start.
5543 */
5544static noinline int
5545wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5546 u64 num_bytes)
5547{
Yan Zheng11833d62009-09-11 16:11:19 -04005548 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005549 DEFINE_WAIT(wait);
5550
Yan Zheng11833d62009-09-11 16:11:19 -04005551 caching_ctl = get_caching_control(cache);
5552 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005553 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005554
Yan Zheng11833d62009-09-11 16:11:19 -04005555 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb6c2011-03-29 13:46:06 +08005556 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005557
5558 put_caching_control(caching_ctl);
5559 return 0;
5560}
5561
5562static noinline int
5563wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5564{
5565 struct btrfs_caching_control *caching_ctl;
5566 DEFINE_WAIT(wait);
5567
5568 caching_ctl = get_caching_control(cache);
5569 if (!caching_ctl)
5570 return 0;
5571
5572 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5573
5574 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005575 return 0;
5576}
5577
Liu Bo31e50222012-11-21 14:18:10 +00005578int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005579{
5580 int index;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005581
5582 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005583 index = 0;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005584 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005585 index = 1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005586 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005587 index = 2;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005588 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005589 index = 3;
David Woodhouse53b381b2013-01-29 18:40:14 -05005590 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
5591 index = 5;
5592 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
5593 index = 6;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005594 else
David Woodhouse53b381b2013-01-29 18:40:14 -05005595 index = 4; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04005596 return index;
5597}
5598
Ilya Dryomov7738a532012-03-27 17:09:17 +03005599static int get_block_group_index(struct btrfs_block_group_cache *cache)
5600{
Liu Bo31e50222012-11-21 14:18:10 +00005601 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005602}
5603
Josef Bacik817d52f2009-07-13 21:29:25 -04005604enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005605 LOOP_CACHING_NOWAIT = 0,
5606 LOOP_CACHING_WAIT = 1,
5607 LOOP_ALLOC_CHUNK = 2,
5608 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005609};
5610
5611/*
Chris Masonfec577f2007-02-26 10:40:21 -05005612 * walks the btree of allocated extents and find a hole of a given size.
5613 * The key ins is changed to record the hole:
5614 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005615 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005616 * ins->offset == number of blocks
5617 * Any available blocks before search_start are skipped.
5618 */
Chris Masond3977122009-01-05 21:25:51 -05005619static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005620 struct btrfs_root *orig_root,
5621 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005622 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005623 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005624{
Josef Bacik80eb2342008-10-29 14:49:05 -04005625 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005626 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005627 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005628 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005629 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005630 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005631 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005632 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005633 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005634 int index = 0;
Josef Bacikfb25e912011-07-26 17:00:46 -04005635 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5636 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005637 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005638 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005639 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005640 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005641 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005642
Chris Masondb945352007-10-15 16:15:53 -04005643 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005644 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005645 ins->objectid = 0;
5646 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005647
Josef Bacik3f7de032011-11-10 08:29:20 -05005648 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5649
Josef Bacik2552d172009-04-03 10:14:19 -04005650 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005651 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00005652 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005653 return -ENOSPC;
5654 }
Josef Bacik2552d172009-04-03 10:14:19 -04005655
Josef Bacik67377732010-09-16 16:19:09 -04005656 /*
5657 * If the space info is for both data and metadata it means we have a
5658 * small filesystem and we can't use the clustering stuff.
5659 */
5660 if (btrfs_mixed_space_info(space_info))
5661 use_cluster = false;
5662
Josef Bacik67377732010-09-16 16:19:09 -04005663 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005664 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005665 if (!btrfs_test_opt(root, SSD))
5666 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005667 }
5668
Josef Bacik67377732010-09-16 16:19:09 -04005669 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5670 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005671 last_ptr = &root->fs_info->data_alloc_cluster;
5672 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005673
Chris Mason239b14b2008-03-24 15:02:07 -04005674 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005675 spin_lock(&last_ptr->lock);
5676 if (last_ptr->block_group)
5677 hint_byte = last_ptr->window_start;
5678 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005679 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005680
Chris Masona061fc82008-05-07 11:43:44 -04005681 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005682 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005683
Josef Bacik817d52f2009-07-13 21:29:25 -04005684 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005685 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005686
Josef Bacik2552d172009-04-03 10:14:19 -04005687 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005688 block_group = btrfs_lookup_block_group(root->fs_info,
5689 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005690 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005691 /*
5692 * we don't want to use the block group if it doesn't match our
5693 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005694 *
5695 * However if we are re-searching with an ideal block group
5696 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005697 */
5698 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005699 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005700 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005701 if (list_empty(&block_group->list) ||
5702 block_group->ro) {
5703 /*
5704 * someone is removing this block group,
5705 * we can't jump into the have_block_group
5706 * target because our list pointers are not
5707 * valid
5708 */
5709 btrfs_put_block_group(block_group);
5710 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005711 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005712 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005713 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005714 }
Josef Bacik2552d172009-04-03 10:14:19 -04005715 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005716 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005717 }
Chris Mason42e70e72008-11-07 18:17:11 -05005718 }
Josef Bacik2552d172009-04-03 10:14:19 -04005719search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005720 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005721 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005722 list_for_each_entry(block_group, &space_info->block_groups[index],
5723 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005724 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005725 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005726
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005727 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005728 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005729 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005730
Chris Mason83a50de2010-12-13 15:06:46 -05005731 /*
5732 * this can happen if we end up cycling through all the
5733 * raid types, but we want to make sure we only allocate
5734 * for the proper type.
5735 */
5736 if (!block_group_bits(block_group, data)) {
5737 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5738 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005739 BTRFS_BLOCK_GROUP_RAID5 |
5740 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05005741 BTRFS_BLOCK_GROUP_RAID10;
5742
5743 /*
5744 * if they asked for extra copies and this block group
5745 * doesn't provide them, bail. This does allow us to
5746 * fill raid0 from raid1.
5747 */
5748 if ((data & extra) && !(block_group->flags & extra))
5749 goto loop;
5750 }
5751
Josef Bacik2552d172009-04-03 10:14:19 -04005752have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05005753 cached = block_group_cache_done(block_group);
5754 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05005755 found_uncached_bg = true;
Josef Bacikb8399de2010-12-08 09:15:11 -05005756 ret = cache_block_group(block_group, trans,
Josef Bacik285ff5a2012-01-13 15:27:45 -05005757 orig_root, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005758 BUG_ON(ret < 0);
5759 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005760 }
5761
Josef Bacikea6a4782008-11-20 12:16:16 -05005762 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005763 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005764
Josef Bacik0a243252009-09-11 16:11:20 -04005765 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005766 * Ok we want to try and use the cluster allocator, so
5767 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005768 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005769 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05005770 unsigned long aligned_cluster;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005771 /*
5772 * the refill lock keeps out other
5773 * people trying to start a new cluster
5774 */
5775 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005776 used_block_group = last_ptr->block_group;
5777 if (used_block_group != block_group &&
5778 (!used_block_group ||
5779 used_block_group->ro ||
5780 !block_group_bits(used_block_group, data))) {
5781 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04005782 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005783 }
Chris Mason44fb5512009-06-04 15:34:51 -04005784
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005785 if (used_block_group != block_group)
5786 btrfs_get_block_group(used_block_group);
5787
5788 offset = btrfs_alloc_from_cluster(used_block_group,
5789 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005790 if (offset) {
5791 /* we have a block, we're done */
5792 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005793 trace_btrfs_reserve_extent_cluster(root,
5794 block_group, search_start, num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005795 goto checks;
5796 }
5797
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005798 WARN_ON(last_ptr->block_group != used_block_group);
5799 if (used_block_group != block_group) {
5800 btrfs_put_block_group(used_block_group);
5801 used_block_group = block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005802 }
Chris Mason44fb5512009-06-04 15:34:51 -04005803refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005804 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005805 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5806 * set up a new clusters, so lets just skip it
5807 * and let the allocator find whatever block
5808 * it can find. If we reach this point, we
5809 * will have tried the cluster allocator
5810 * plenty of times and not have found
5811 * anything, so we are likely way too
5812 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005813 * anything.
5814 *
5815 * However, if the cluster is taken from the
5816 * current block group, release the cluster
5817 * first, so that we stand a better chance of
5818 * succeeding in the unclustered
5819 * allocation. */
5820 if (loop >= LOOP_NO_EMPTY_SIZE &&
5821 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005822 spin_unlock(&last_ptr->refill_lock);
5823 goto unclustered_alloc;
5824 }
5825
Chris Masonfa9c0d72009-04-03 09:47:43 -04005826 /*
5827 * this cluster didn't work out, free it and
5828 * start over
5829 */
5830 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5831
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005832 if (loop >= LOOP_NO_EMPTY_SIZE) {
5833 spin_unlock(&last_ptr->refill_lock);
5834 goto unclustered_alloc;
5835 }
5836
Chris Mason8de972b2013-01-04 15:39:43 -05005837 aligned_cluster = max_t(unsigned long,
5838 empty_cluster + empty_size,
5839 block_group->full_stripe_len);
5840
Chris Masonfa9c0d72009-04-03 09:47:43 -04005841 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005842 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005843 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05005844 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05005845 aligned_cluster);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005846 if (ret == 0) {
5847 /*
5848 * now pull our allocation out of this
5849 * cluster
5850 */
5851 offset = btrfs_alloc_from_cluster(block_group,
5852 last_ptr, num_bytes,
5853 search_start);
5854 if (offset) {
5855 /* we found one, proceed */
5856 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005857 trace_btrfs_reserve_extent_cluster(root,
5858 block_group, search_start,
5859 num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005860 goto checks;
5861 }
Josef Bacik0a243252009-09-11 16:11:20 -04005862 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5863 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005864 spin_unlock(&last_ptr->refill_lock);
5865
Josef Bacik0a243252009-09-11 16:11:20 -04005866 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005867 wait_block_group_cache_progress(block_group,
5868 num_bytes + empty_cluster + empty_size);
5869 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005870 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005871
Chris Masonfa9c0d72009-04-03 09:47:43 -04005872 /*
5873 * at this point we either didn't find a cluster
5874 * or we weren't able to allocate a block from our
5875 * cluster. Free the cluster we've been trying
5876 * to use, and go to the next block group
5877 */
Josef Bacik0a243252009-09-11 16:11:20 -04005878 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005879 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005880 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005881 }
5882
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005883unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005884 spin_lock(&block_group->free_space_ctl->tree_lock);
5885 if (cached &&
5886 block_group->free_space_ctl->free_space <
5887 num_bytes + empty_cluster + empty_size) {
5888 spin_unlock(&block_group->free_space_ctl->tree_lock);
5889 goto loop;
5890 }
5891 spin_unlock(&block_group->free_space_ctl->tree_lock);
5892
Josef Bacik6226cb02009-04-03 10:14:18 -04005893 offset = btrfs_find_space_for_alloc(block_group, search_start,
5894 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005895 /*
5896 * If we didn't find a chunk, and we haven't failed on this
5897 * block group before, and this block group is in the middle of
5898 * caching and we are ok with waiting, then go ahead and wait
5899 * for progress to be made, and set failed_alloc to true.
5900 *
5901 * If failed_alloc is true then we've already waited on this
5902 * block group once and should move on to the next block group.
5903 */
5904 if (!offset && !failed_alloc && !cached &&
5905 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005906 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005907 num_bytes + empty_size);
5908 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005909 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005910 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08005911 if (!cached)
5912 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005913 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005914 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005915checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05005916 search_start = stripe_align(root, used_block_group,
5917 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04005918
Josef Bacik2552d172009-04-03 10:14:19 -04005919 /* move on to the next group */
5920 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005921 used_block_group->key.objectid + used_block_group->key.offset) {
5922 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005923 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005924 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005925
Josef Bacik6226cb02009-04-03 10:14:18 -04005926 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005927 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04005928 search_start - offset);
5929 BUG_ON(offset > search_start);
5930
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005931 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04005932 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005933 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005934 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005935 goto loop;
5936 }
Yan Zheng11833d62009-09-11 16:11:19 -04005937
Josef Bacik2552d172009-04-03 10:14:19 -04005938 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005939 ins->objectid = search_start;
5940 ins->offset = num_bytes;
5941
Josef Bacik3f7de032011-11-10 08:29:20 -05005942 trace_btrfs_reserve_extent(orig_root, block_group,
5943 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005944 if (used_block_group != block_group)
5945 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005946 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005947 break;
5948loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005949 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005950 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005951 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005952 if (used_block_group != block_group)
5953 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005954 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005955 }
5956 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005957
Miao Xie60d2adb2011-09-09 17:34:35 +08005958 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
5959 goto search;
5960
Yan, Zhengb742bb82010-05-16 10:46:24 -04005961 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5962 goto search;
5963
Josef Bacik285ff5a2012-01-13 15:27:45 -05005964 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005965 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5966 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005967 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5968 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5969 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5970 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005971 */
Josef Bacik723bda22011-05-27 16:11:38 -04005972 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005973 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04005974 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005975 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04005976 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04005977 CHUNK_ALLOC_FORCE);
5978 /*
5979 * Do not bail out on ENOSPC since we
5980 * can do more things.
5981 */
5982 if (ret < 0 && ret != -ENOSPC) {
5983 btrfs_abort_transaction(trans,
5984 root, ret);
5985 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04005986 }
Josef Bacik723bda22011-05-27 16:11:38 -04005987 }
5988
5989 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005990 empty_size = 0;
5991 empty_cluster = 0;
5992 }
Chris Mason42e70e72008-11-07 18:17:11 -05005993
Josef Bacik723bda22011-05-27 16:11:38 -04005994 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005995 } else if (!ins->objectid) {
5996 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005997 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005998 ret = 0;
5999 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006000out:
Chris Mason0b86a832008-03-24 15:01:56 -04006001
Chris Mason0f70abe2007-02-28 16:46:22 -05006002 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006003}
Chris Masonec44a352008-04-28 15:29:52 -04006004
Josef Bacik9ed74f22009-09-11 16:12:44 -04006005static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6006 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006007{
6008 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006009 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006010
Josef Bacik9ed74f22009-09-11 16:12:44 -04006011 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006012 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6013 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006014 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006015 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006016 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006017 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006018 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6019 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006020 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006021 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006022 (unsigned long long)info->bytes_pinned,
6023 (unsigned long long)info->bytes_reserved,
6024 (unsigned long long)info->bytes_may_use,
6025 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006026 spin_unlock(&info->lock);
6027
6028 if (!dump_block_groups)
6029 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006030
Josef Bacik80eb2342008-10-29 14:49:05 -04006031 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006032again:
6033 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006034 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006035 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006036 (unsigned long long)cache->key.objectid,
6037 (unsigned long long)cache->key.offset,
6038 (unsigned long long)btrfs_block_group_used(&cache->item),
6039 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006040 (unsigned long long)cache->reserved,
6041 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006042 btrfs_dump_free_space(cache, bytes);
6043 spin_unlock(&cache->lock);
6044 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006045 if (++index < BTRFS_NR_RAID_TYPES)
6046 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006047 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006048}
Zheng Yane8569812008-09-26 10:05:48 -04006049
Yan Zheng11833d62009-09-11 16:11:19 -04006050int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6051 struct btrfs_root *root,
6052 u64 num_bytes, u64 min_alloc_size,
6053 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006054 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006055{
Miao Xie9e622d62012-01-26 15:01:12 -05006056 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006057 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006058
Josef Bacik6a632092009-02-20 11:00:09 -05006059 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006060again:
Chris Masondb945352007-10-15 16:15:53 -04006061 WARN_ON(num_bytes < root->sectorsize);
6062 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006063 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006064
Miao Xie9e622d62012-01-26 15:01:12 -05006065 if (ret == -ENOSPC) {
6066 if (!final_tried) {
6067 num_bytes = num_bytes >> 1;
6068 num_bytes = num_bytes & ~(root->sectorsize - 1);
6069 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006070 if (num_bytes == min_alloc_size)
6071 final_tried = true;
6072 goto again;
6073 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6074 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006075
Miao Xie9e622d62012-01-26 15:01:12 -05006076 sinfo = __find_space_info(root->fs_info, data);
6077 printk(KERN_ERR "btrfs allocation failed flags %llu, "
6078 "wanted %llu\n", (unsigned long long)data,
6079 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006080 if (sinfo)
6081 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006082 }
Chris Mason925baed2008-06-25 16:01:30 -04006083 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006084
liubo1abe9b82011-03-24 11:18:59 +00006085 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6086
Josef Bacik0f9dd462008-09-23 13:14:11 -04006087 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006088}
6089
Chris Masone688b722011-10-31 20:52:39 -04006090static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6091 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006092{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006093 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006094 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006095
Josef Bacik0f9dd462008-09-23 13:14:11 -04006096 cache = btrfs_lookup_block_group(root->fs_info, start);
6097 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05006098 printk(KERN_ERR "Unable to find block group for %llu\n",
6099 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006100 return -ENOSPC;
6101 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006102
Li Dongyang5378e602011-03-24 10:24:27 +00006103 if (btrfs_test_opt(root, DISCARD))
6104 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006105
Chris Masone688b722011-10-31 20:52:39 -04006106 if (pin)
6107 pin_down_extent(root, cache, start, len, 1);
6108 else {
6109 btrfs_add_free_space(cache, start, len);
6110 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6111 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006112 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006113
liubo1abe9b82011-03-24 11:18:59 +00006114 trace_btrfs_reserved_extent_free(root, start, len);
6115
Chris Masone6dcd2d2008-07-17 12:53:50 -04006116 return ret;
6117}
6118
Chris Masone688b722011-10-31 20:52:39 -04006119int btrfs_free_reserved_extent(struct btrfs_root *root,
6120 u64 start, u64 len)
6121{
6122 return __btrfs_free_reserved_extent(root, start, len, 0);
6123}
6124
6125int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6126 u64 start, u64 len)
6127{
6128 return __btrfs_free_reserved_extent(root, start, len, 1);
6129}
6130
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006131static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6132 struct btrfs_root *root,
6133 u64 parent, u64 root_objectid,
6134 u64 flags, u64 owner, u64 offset,
6135 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006136{
6137 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006138 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006139 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006140 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006141 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006142 struct extent_buffer *leaf;
6143 int type;
6144 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006145
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006146 if (parent > 0)
6147 type = BTRFS_SHARED_DATA_REF_KEY;
6148 else
6149 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006150
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006151 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006152
6153 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006154 if (!path)
6155 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006156
Chris Masonb9473432009-03-13 11:00:37 -04006157 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006158 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6159 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006160 if (ret) {
6161 btrfs_free_path(path);
6162 return ret;
6163 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006164
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006165 leaf = path->nodes[0];
6166 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006167 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006168 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6169 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6170 btrfs_set_extent_flags(leaf, extent_item,
6171 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006173 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6174 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6175 if (parent > 0) {
6176 struct btrfs_shared_data_ref *ref;
6177 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6178 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6179 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6180 } else {
6181 struct btrfs_extent_data_ref *ref;
6182 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6183 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6184 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6185 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6186 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6187 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006188
6189 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006190 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006191
Yan, Zhengf0486c62010-05-16 10:46:25 -04006192 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006193 if (ret) { /* -ENOENT, logic error */
Chris Masond3977122009-01-05 21:25:51 -05006194 printk(KERN_ERR "btrfs update block group failed for %llu "
6195 "%llu\n", (unsigned long long)ins->objectid,
6196 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006197 BUG();
6198 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006199 return ret;
6200}
6201
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006202static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6203 struct btrfs_root *root,
6204 u64 parent, u64 root_objectid,
6205 u64 flags, struct btrfs_disk_key *key,
6206 int level, struct btrfs_key *ins)
6207{
6208 int ret;
6209 struct btrfs_fs_info *fs_info = root->fs_info;
6210 struct btrfs_extent_item *extent_item;
6211 struct btrfs_tree_block_info *block_info;
6212 struct btrfs_extent_inline_ref *iref;
6213 struct btrfs_path *path;
6214 struct extent_buffer *leaf;
6215 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
6216
6217 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006218 if (!path)
6219 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006220
6221 path->leave_spinning = 1;
6222 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6223 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006224 if (ret) {
6225 btrfs_free_path(path);
6226 return ret;
6227 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006228
6229 leaf = path->nodes[0];
6230 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6231 struct btrfs_extent_item);
6232 btrfs_set_extent_refs(leaf, extent_item, 1);
6233 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6234 btrfs_set_extent_flags(leaf, extent_item,
6235 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6236 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6237
6238 btrfs_set_tree_block_key(leaf, block_info, key);
6239 btrfs_set_tree_block_level(leaf, block_info, level);
6240
6241 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6242 if (parent > 0) {
6243 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6244 btrfs_set_extent_inline_ref_type(leaf, iref,
6245 BTRFS_SHARED_BLOCK_REF_KEY);
6246 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6247 } else {
6248 btrfs_set_extent_inline_ref_type(leaf, iref,
6249 BTRFS_TREE_BLOCK_REF_KEY);
6250 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6251 }
6252
6253 btrfs_mark_buffer_dirty(leaf);
6254 btrfs_free_path(path);
6255
Yan, Zhengf0486c62010-05-16 10:46:25 -04006256 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006257 if (ret) { /* -ENOENT, logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006258 printk(KERN_ERR "btrfs update block group failed for %llu "
6259 "%llu\n", (unsigned long long)ins->objectid,
6260 (unsigned long long)ins->offset);
6261 BUG();
6262 }
6263 return ret;
6264}
6265
6266int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6267 struct btrfs_root *root,
6268 u64 root_objectid, u64 owner,
6269 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006270{
6271 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006272
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006273 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006274
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006275 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6276 ins->offset, 0,
6277 root_objectid, owner, offset,
6278 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006279 return ret;
6280}
Chris Masone02119d2008-09-05 16:13:11 -04006281
6282/*
6283 * this is used by the tree logging recovery code. It records that
6284 * an extent has been allocated and makes sure to clear the free
6285 * space cache bits as well
6286 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006287int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6288 struct btrfs_root *root,
6289 u64 root_objectid, u64 owner, u64 offset,
6290 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006291{
6292 int ret;
6293 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006294 struct btrfs_caching_control *caching_ctl;
6295 u64 start = ins->objectid;
6296 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006297
Chris Masone02119d2008-09-05 16:13:11 -04006298 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05006299 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006300 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006301
Yan Zheng11833d62009-09-11 16:11:19 -04006302 if (!caching_ctl) {
6303 BUG_ON(!block_group_cache_done(block_group));
6304 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006305 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006306 } else {
6307 mutex_lock(&caching_ctl->mutex);
6308
6309 if (start >= caching_ctl->progress) {
6310 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006311 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006312 } else if (start + num_bytes <= caching_ctl->progress) {
6313 ret = btrfs_remove_free_space(block_group,
6314 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006315 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006316 } else {
6317 num_bytes = caching_ctl->progress - start;
6318 ret = btrfs_remove_free_space(block_group,
6319 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006320 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006321
6322 start = caching_ctl->progress;
6323 num_bytes = ins->objectid + ins->offset -
6324 caching_ctl->progress;
6325 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006326 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006327 }
6328
6329 mutex_unlock(&caching_ctl->mutex);
6330 put_caching_control(caching_ctl);
6331 }
6332
Josef Bacikfb25e912011-07-26 17:00:46 -04006333 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6334 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006335 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d72009-04-03 09:47:43 -04006336 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006337 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6338 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006339 return ret;
6340}
6341
Chris Mason65b51a02008-08-01 15:11:20 -04006342struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6343 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006344 u64 bytenr, u32 blocksize,
6345 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006346{
6347 struct extent_buffer *buf;
6348
6349 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6350 if (!buf)
6351 return ERR_PTR(-ENOMEM);
6352 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006353 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006354 btrfs_tree_lock(buf);
6355 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006356 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006357
6358 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006359 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006360
Chris Masond0c803c2008-09-11 16:17:57 -04006361 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006362 /*
6363 * we allow two log transactions at a time, use different
6364 * EXENT bit to differentiate dirty pages.
6365 */
6366 if (root->log_transid % 2 == 0)
6367 set_extent_dirty(&root->dirty_log_pages, buf->start,
6368 buf->start + buf->len - 1, GFP_NOFS);
6369 else
6370 set_extent_new(&root->dirty_log_pages, buf->start,
6371 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006372 } else {
6373 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6374 buf->start + buf->len - 1, GFP_NOFS);
6375 }
Chris Mason65b51a02008-08-01 15:11:20 -04006376 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006377 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006378 return buf;
6379}
6380
Yan, Zhengf0486c62010-05-16 10:46:25 -04006381static struct btrfs_block_rsv *
6382use_block_rsv(struct btrfs_trans_handle *trans,
6383 struct btrfs_root *root, u32 blocksize)
6384{
6385 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006386 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006387 int ret;
6388
6389 block_rsv = get_block_rsv(trans, root);
6390
6391 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006392 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6393 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006394 /*
6395 * If we couldn't reserve metadata bytes try and use some from
6396 * the global reserve.
6397 */
6398 if (ret && block_rsv != global_rsv) {
6399 ret = block_rsv_use_bytes(global_rsv, blocksize);
6400 if (!ret)
6401 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006402 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006403 } else if (ret) {
6404 return ERR_PTR(ret);
6405 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006406 return block_rsv;
6407 }
6408
6409 ret = block_rsv_use_bytes(block_rsv, blocksize);
6410 if (!ret)
6411 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006412 if (ret && !block_rsv->failfast) {
David Sterbadff51cd2011-06-14 12:52:17 +02006413 static DEFINE_RATELIMIT_STATE(_rs,
6414 DEFAULT_RATELIMIT_INTERVAL,
6415 /*DEFAULT_RATELIMIT_BURST*/ 2);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006416 if (__ratelimit(&_rs))
6417 WARN(1, KERN_DEBUG "btrfs: block rsv returned %d\n",
6418 ret);
Miao Xie08e007d2012-10-16 11:33:38 +00006419 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6420 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006421 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006422 return block_rsv;
6423 } else if (ret && block_rsv != global_rsv) {
6424 ret = block_rsv_use_bytes(global_rsv, blocksize);
6425 if (!ret)
6426 return global_rsv;
6427 }
6428 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006429
Yan, Zhengf0486c62010-05-16 10:46:25 -04006430 return ERR_PTR(-ENOSPC);
6431}
6432
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006433static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6434 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006435{
6436 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006437 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006438}
6439
Chris Masonfec577f2007-02-26 10:40:21 -05006440/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006441 * finds a free extent and does all the dirty work required for allocation
6442 * returns the key for the extent through ins, and a tree buffer for
6443 * the first block of the extent through buf.
6444 *
Chris Masonfec577f2007-02-26 10:40:21 -05006445 * returns the tree buffer or NULL.
6446 */
Chris Mason5f39d392007-10-15 16:14:19 -04006447struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006448 struct btrfs_root *root, u32 blocksize,
6449 u64 parent, u64 root_objectid,
6450 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006451 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006452{
Chris Masone2fa7222007-03-12 16:22:34 -04006453 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006454 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006455 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006456 u64 flags = 0;
6457 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006458
Yan, Zhengf0486c62010-05-16 10:46:25 -04006459
6460 block_rsv = use_block_rsv(trans, root, blocksize);
6461 if (IS_ERR(block_rsv))
6462 return ERR_CAST(block_rsv);
6463
6464 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006465 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006466 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006467 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006468 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006469 }
Chris Mason55c69072008-01-09 15:55:33 -05006470
Chris Mason4008c042009-02-12 14:09:45 -05006471 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6472 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006473 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006474
6475 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6476 if (parent == 0)
6477 parent = ins.objectid;
6478 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6479 } else
6480 BUG_ON(parent > 0);
6481
6482 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6483 struct btrfs_delayed_extent_op *extent_op;
6484 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006485 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006486 if (key)
6487 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6488 else
6489 memset(&extent_op->key, 0, sizeof(extent_op->key));
6490 extent_op->flags_to_set = flags;
6491 extent_op->update_key = 1;
6492 extent_op->update_flags = 1;
6493 extent_op->is_data = 0;
6494
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006495 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6496 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006497 ins.offset, parent, root_objectid,
6498 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006499 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006500 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006501 }
Chris Masonfec577f2007-02-26 10:40:21 -05006502 return buf;
6503}
Chris Masona28ec192007-03-06 20:08:01 -05006504
Yan Zheng2c47e6052009-06-27 21:07:35 -04006505struct walk_control {
6506 u64 refs[BTRFS_MAX_LEVEL];
6507 u64 flags[BTRFS_MAX_LEVEL];
6508 struct btrfs_key update_progress;
6509 int stage;
6510 int level;
6511 int shared_level;
6512 int update_ref;
6513 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006514 int reada_slot;
6515 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006516 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006517};
6518
6519#define DROP_REFERENCE 1
6520#define UPDATE_BACKREF 2
6521
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006522static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6523 struct btrfs_root *root,
6524 struct walk_control *wc,
6525 struct btrfs_path *path)
6526{
6527 u64 bytenr;
6528 u64 generation;
6529 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006530 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006531 u32 nritems;
6532 u32 blocksize;
6533 struct btrfs_key key;
6534 struct extent_buffer *eb;
6535 int ret;
6536 int slot;
6537 int nread = 0;
6538
6539 if (path->slots[wc->level] < wc->reada_slot) {
6540 wc->reada_count = wc->reada_count * 2 / 3;
6541 wc->reada_count = max(wc->reada_count, 2);
6542 } else {
6543 wc->reada_count = wc->reada_count * 3 / 2;
6544 wc->reada_count = min_t(int, wc->reada_count,
6545 BTRFS_NODEPTRS_PER_BLOCK(root));
6546 }
6547
6548 eb = path->nodes[wc->level];
6549 nritems = btrfs_header_nritems(eb);
6550 blocksize = btrfs_level_size(root, wc->level - 1);
6551
6552 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6553 if (nread >= wc->reada_count)
6554 break;
6555
6556 cond_resched();
6557 bytenr = btrfs_node_blockptr(eb, slot);
6558 generation = btrfs_node_ptr_generation(eb, slot);
6559
6560 if (slot == path->slots[wc->level])
6561 goto reada;
6562
6563 if (wc->stage == UPDATE_BACKREF &&
6564 generation <= root->root_key.offset)
6565 continue;
6566
Yan, Zheng94fcca92009-10-09 09:25:16 -04006567 /* We don't lock the tree block, it's OK to be racy here */
6568 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6569 &refs, &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006570 /* We don't care about errors in readahead. */
6571 if (ret < 0)
6572 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006573 BUG_ON(refs == 0);
6574
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006575 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006576 if (refs == 1)
6577 goto reada;
6578
Yan, Zheng94fcca92009-10-09 09:25:16 -04006579 if (wc->level == 1 &&
6580 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6581 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006582 if (!wc->update_ref ||
6583 generation <= root->root_key.offset)
6584 continue;
6585 btrfs_node_key_to_cpu(eb, &key, slot);
6586 ret = btrfs_comp_cpu_keys(&key,
6587 &wc->update_progress);
6588 if (ret < 0)
6589 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006590 } else {
6591 if (wc->level == 1 &&
6592 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6593 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006594 }
6595reada:
6596 ret = readahead_tree_block(root, bytenr, blocksize,
6597 generation);
6598 if (ret)
6599 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006600 nread++;
6601 }
6602 wc->reada_slot = slot;
6603}
6604
Chris Mason9aca1d52007-03-13 11:09:37 -04006605/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006606 * hepler to process tree block while walking down the tree.
6607 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006608 * when wc->stage == UPDATE_BACKREF, this function updates
6609 * back refs for pointers in the block.
6610 *
6611 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006612 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006613static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6614 struct btrfs_root *root,
6615 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006616 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006617{
6618 int level = wc->level;
6619 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006620 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6621 int ret;
6622
6623 if (wc->stage == UPDATE_BACKREF &&
6624 btrfs_header_owner(eb) != root->root_key.objectid)
6625 return 1;
6626
6627 /*
6628 * when reference count of tree block is 1, it won't increase
6629 * again. once full backref flag is set, we never clear it.
6630 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006631 if (lookup_info &&
6632 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6633 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006634 BUG_ON(!path->locks[level]);
6635 ret = btrfs_lookup_extent_info(trans, root,
6636 eb->start, eb->len,
6637 &wc->refs[level],
6638 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006639 BUG_ON(ret == -ENOMEM);
6640 if (ret)
6641 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006642 BUG_ON(wc->refs[level] == 0);
6643 }
6644
Yan Zheng2c47e6052009-06-27 21:07:35 -04006645 if (wc->stage == DROP_REFERENCE) {
6646 if (wc->refs[level] > 1)
6647 return 1;
6648
6649 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006650 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006651 path->locks[level] = 0;
6652 }
6653 return 0;
6654 }
6655
6656 /* wc->stage == UPDATE_BACKREF */
6657 if (!(wc->flags[level] & flag)) {
6658 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006659 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006660 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006661 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006662 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006663 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6664 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006665 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006666 wc->flags[level] |= flag;
6667 }
6668
6669 /*
6670 * the block is shared by multiple trees, so it's not good to
6671 * keep the tree lock
6672 */
6673 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006674 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006675 path->locks[level] = 0;
6676 }
6677 return 0;
6678}
6679
6680/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006681 * hepler to process tree block pointer.
6682 *
6683 * when wc->stage == DROP_REFERENCE, this function checks
6684 * reference count of the block pointed to. if the block
6685 * is shared and we need update back refs for the subtree
6686 * rooted at the block, this function changes wc->stage to
6687 * UPDATE_BACKREF. if the block is shared and there is no
6688 * need to update back, this function drops the reference
6689 * to the block.
6690 *
6691 * NOTE: return value 1 means we should stop walking down.
6692 */
6693static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6694 struct btrfs_root *root,
6695 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006696 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006697{
6698 u64 bytenr;
6699 u64 generation;
6700 u64 parent;
6701 u32 blocksize;
6702 struct btrfs_key key;
6703 struct extent_buffer *next;
6704 int level = wc->level;
6705 int reada = 0;
6706 int ret = 0;
6707
6708 generation = btrfs_node_ptr_generation(path->nodes[level],
6709 path->slots[level]);
6710 /*
6711 * if the lower level block was created before the snapshot
6712 * was created, we know there is no need to update back refs
6713 * for the subtree
6714 */
6715 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006716 generation <= root->root_key.offset) {
6717 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006718 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006719 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006720
6721 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6722 blocksize = btrfs_level_size(root, level - 1);
6723
6724 next = btrfs_find_tree_block(root, bytenr, blocksize);
6725 if (!next) {
6726 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006727 if (!next)
6728 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006729 reada = 1;
6730 }
6731 btrfs_tree_lock(next);
6732 btrfs_set_lock_blocking(next);
6733
Yan, Zheng94fcca92009-10-09 09:25:16 -04006734 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6735 &wc->refs[level - 1],
6736 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006737 if (ret < 0) {
6738 btrfs_tree_unlock(next);
6739 return ret;
6740 }
6741
Yan, Zheng94fcca92009-10-09 09:25:16 -04006742 BUG_ON(wc->refs[level - 1] == 0);
6743 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006744
Yan, Zheng94fcca92009-10-09 09:25:16 -04006745 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006746 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006747 if (level == 1 &&
6748 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6749 goto skip;
6750
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006751 if (!wc->update_ref ||
6752 generation <= root->root_key.offset)
6753 goto skip;
6754
6755 btrfs_node_key_to_cpu(path->nodes[level], &key,
6756 path->slots[level]);
6757 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6758 if (ret < 0)
6759 goto skip;
6760
6761 wc->stage = UPDATE_BACKREF;
6762 wc->shared_level = level - 1;
6763 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006764 } else {
6765 if (level == 1 &&
6766 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6767 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006768 }
6769
Chris Masonb9fab912012-05-06 07:23:47 -04006770 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006771 btrfs_tree_unlock(next);
6772 free_extent_buffer(next);
6773 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006774 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006775 }
6776
6777 if (!next) {
6778 if (reada && level == 1)
6779 reada_walk_down(trans, root, wc, path);
6780 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006781 if (!next)
6782 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006783 btrfs_tree_lock(next);
6784 btrfs_set_lock_blocking(next);
6785 }
6786
6787 level--;
6788 BUG_ON(level != btrfs_header_level(next));
6789 path->nodes[level] = next;
6790 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006791 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006792 wc->level = level;
6793 if (wc->level == 1)
6794 wc->reada_slot = 0;
6795 return 0;
6796skip:
6797 wc->refs[level - 1] = 0;
6798 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006799 if (wc->stage == DROP_REFERENCE) {
6800 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6801 parent = path->nodes[level]->start;
6802 } else {
6803 BUG_ON(root->root_key.objectid !=
6804 btrfs_header_owner(path->nodes[level]));
6805 parent = 0;
6806 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006807
Yan, Zheng94fcca92009-10-09 09:25:16 -04006808 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006809 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006810 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006811 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006812 btrfs_tree_unlock(next);
6813 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006814 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006815 return 1;
6816}
6817
6818/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006819 * hepler to process tree block while walking up the tree.
6820 *
6821 * when wc->stage == DROP_REFERENCE, this function drops
6822 * reference count on the block.
6823 *
6824 * when wc->stage == UPDATE_BACKREF, this function changes
6825 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6826 * to UPDATE_BACKREF previously while processing the block.
6827 *
6828 * NOTE: return value 1 means we should stop walking up.
6829 */
6830static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6831 struct btrfs_root *root,
6832 struct btrfs_path *path,
6833 struct walk_control *wc)
6834{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006835 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006836 int level = wc->level;
6837 struct extent_buffer *eb = path->nodes[level];
6838 u64 parent = 0;
6839
6840 if (wc->stage == UPDATE_BACKREF) {
6841 BUG_ON(wc->shared_level < level);
6842 if (level < wc->shared_level)
6843 goto out;
6844
Yan Zheng2c47e6052009-06-27 21:07:35 -04006845 ret = find_next_key(path, level + 1, &wc->update_progress);
6846 if (ret > 0)
6847 wc->update_ref = 0;
6848
6849 wc->stage = DROP_REFERENCE;
6850 wc->shared_level = -1;
6851 path->slots[level] = 0;
6852
6853 /*
6854 * check reference count again if the block isn't locked.
6855 * we should start walking down the tree again if reference
6856 * count is one.
6857 */
6858 if (!path->locks[level]) {
6859 BUG_ON(level == 0);
6860 btrfs_tree_lock(eb);
6861 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006862 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006863
6864 ret = btrfs_lookup_extent_info(trans, root,
6865 eb->start, eb->len,
6866 &wc->refs[level],
6867 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006868 if (ret < 0) {
6869 btrfs_tree_unlock_rw(eb, path->locks[level]);
6870 return ret;
6871 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006872 BUG_ON(wc->refs[level] == 0);
6873 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006874 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006875 return 1;
6876 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006877 }
6878 }
6879
6880 /* wc->stage == DROP_REFERENCE */
6881 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6882
6883 if (wc->refs[level] == 1) {
6884 if (level == 0) {
6885 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006886 ret = btrfs_dec_ref(trans, root, eb, 1,
6887 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006888 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006889 ret = btrfs_dec_ref(trans, root, eb, 0,
6890 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006891 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006892 }
6893 /* make block locked assertion in clean_tree_block happy */
6894 if (!path->locks[level] &&
6895 btrfs_header_generation(eb) == trans->transid) {
6896 btrfs_tree_lock(eb);
6897 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006898 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006899 }
6900 clean_tree_block(trans, root, eb);
6901 }
6902
6903 if (eb == root->node) {
6904 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6905 parent = eb->start;
6906 else
6907 BUG_ON(root->root_key.objectid !=
6908 btrfs_header_owner(eb));
6909 } else {
6910 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6911 parent = path->nodes[level + 1]->start;
6912 else
6913 BUG_ON(root->root_key.objectid !=
6914 btrfs_header_owner(path->nodes[level + 1]));
6915 }
6916
Jan Schmidt5581a512012-05-16 17:04:52 +02006917 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006918out:
6919 wc->refs[level] = 0;
6920 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006921 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006922}
6923
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006924static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6925 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006926 struct btrfs_path *path,
6927 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006928{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006929 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006930 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006931 int ret;
6932
Yan Zheng2c47e6052009-06-27 21:07:35 -04006933 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006934 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006935 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006936 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006937
Yan Zheng2c47e6052009-06-27 21:07:35 -04006938 if (level == 0)
6939 break;
6940
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006941 if (path->slots[level] >=
6942 btrfs_header_nritems(path->nodes[level]))
6943 break;
6944
Yan, Zheng94fcca92009-10-09 09:25:16 -04006945 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006946 if (ret > 0) {
6947 path->slots[level]++;
6948 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006949 } else if (ret < 0)
6950 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006951 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006952 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006953 return 0;
6954}
6955
Chris Masond3977122009-01-05 21:25:51 -05006956static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006957 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006958 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006959 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006960{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006961 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006962 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006963
Yan Zheng2c47e6052009-06-27 21:07:35 -04006964 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6965 while (level < max_level && path->nodes[level]) {
6966 wc->level = level;
6967 if (path->slots[level] + 1 <
6968 btrfs_header_nritems(path->nodes[level])) {
6969 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006970 return 0;
6971 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006972 ret = walk_up_proc(trans, root, path, wc);
6973 if (ret > 0)
6974 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006975
Yan Zheng2c47e6052009-06-27 21:07:35 -04006976 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006977 btrfs_tree_unlock_rw(path->nodes[level],
6978 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006979 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006980 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006981 free_extent_buffer(path->nodes[level]);
6982 path->nodes[level] = NULL;
6983 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006984 }
6985 }
6986 return 1;
6987}
6988
Chris Mason9aca1d52007-03-13 11:09:37 -04006989/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006990 * drop a subvolume tree.
6991 *
6992 * this function traverses the tree freeing any blocks that only
6993 * referenced by the tree.
6994 *
6995 * when a shared tree block is found. this function decreases its
6996 * reference count by one. if update_ref is true, this function
6997 * also make sure backrefs for the shared block and all lower level
6998 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006999 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007000int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007001 struct btrfs_block_rsv *block_rsv, int update_ref,
7002 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007003{
Chris Mason5caf2a02007-04-02 11:20:42 -04007004 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007005 struct btrfs_trans_handle *trans;
7006 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007007 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007008 struct walk_control *wc;
7009 struct btrfs_key key;
7010 int err = 0;
7011 int ret;
7012 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007013
Chris Mason5caf2a02007-04-02 11:20:42 -04007014 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007015 if (!path) {
7016 err = -ENOMEM;
7017 goto out;
7018 }
Chris Mason20524f02007-03-10 06:35:47 -05007019
Yan Zheng2c47e6052009-06-27 21:07:35 -04007020 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007021 if (!wc) {
7022 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007023 err = -ENOMEM;
7024 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007025 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007026
Yan, Zhenga22285a2010-05-16 10:48:46 -04007027 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007028 if (IS_ERR(trans)) {
7029 err = PTR_ERR(trans);
7030 goto out_free;
7031 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007032
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007033 if (block_rsv)
7034 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007035
Chris Mason9f3a7422007-08-07 15:52:19 -04007036 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007037 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007038 path->nodes[level] = btrfs_lock_root_node(root);
7039 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007040 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007041 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007042 memset(&wc->update_progress, 0,
7043 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007044 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007045 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007046 memcpy(&wc->update_progress, &key,
7047 sizeof(wc->update_progress));
7048
Chris Mason6702ed42007-08-07 16:15:09 -04007049 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007050 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007051 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007052 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7053 path->lowest_level = 0;
7054 if (ret < 0) {
7055 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007056 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007057 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007058 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007059
Chris Mason7d9eb122008-07-08 14:19:17 -04007060 /*
7061 * unlock our path, this is safe because only this
7062 * function is allowed to delete this snapshot
7063 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007064 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007065
Yan Zheng2c47e6052009-06-27 21:07:35 -04007066 level = btrfs_header_level(root->node);
7067 while (1) {
7068 btrfs_tree_lock(path->nodes[level]);
7069 btrfs_set_lock_blocking(path->nodes[level]);
7070
7071 ret = btrfs_lookup_extent_info(trans, root,
7072 path->nodes[level]->start,
7073 path->nodes[level]->len,
7074 &wc->refs[level],
7075 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007076 if (ret < 0) {
7077 err = ret;
7078 goto out_end_trans;
7079 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007080 BUG_ON(wc->refs[level] == 0);
7081
7082 if (level == root_item->drop_level)
7083 break;
7084
7085 btrfs_tree_unlock(path->nodes[level]);
7086 WARN_ON(wc->refs[level] != 1);
7087 level--;
7088 }
7089 }
7090
7091 wc->level = level;
7092 wc->shared_level = -1;
7093 wc->stage = DROP_REFERENCE;
7094 wc->update_ref = update_ref;
7095 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007096 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007097 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007098
7099 while (1) {
7100 ret = walk_down_tree(trans, root, path, wc);
7101 if (ret < 0) {
7102 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007103 break;
7104 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007105
7106 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7107 if (ret < 0) {
7108 err = ret;
7109 break;
7110 }
7111
7112 if (ret > 0) {
7113 BUG_ON(wc->stage != DROP_REFERENCE);
7114 break;
7115 }
7116
7117 if (wc->stage == DROP_REFERENCE) {
7118 level = wc->level;
7119 btrfs_node_key(path->nodes[level],
7120 &root_item->drop_progress,
7121 path->slots[level]);
7122 root_item->drop_level = level;
7123 }
7124
7125 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007126 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007127 ret = btrfs_update_root(trans, tree_root,
7128 &root->root_key,
7129 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007130 if (ret) {
7131 btrfs_abort_transaction(trans, tree_root, ret);
7132 err = ret;
7133 goto out_end_trans;
7134 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007135
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007136 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007137 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007138 if (IS_ERR(trans)) {
7139 err = PTR_ERR(trans);
7140 goto out_free;
7141 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007142 if (block_rsv)
7143 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007144 }
Chris Mason20524f02007-03-10 06:35:47 -05007145 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007146 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007147 if (err)
7148 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007149
7150 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007151 if (ret) {
7152 btrfs_abort_transaction(trans, tree_root, ret);
7153 goto out_end_trans;
7154 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007155
Yan, Zheng76dda932009-09-21 16:00:26 -04007156 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7157 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7158 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007159 if (ret < 0) {
7160 btrfs_abort_transaction(trans, tree_root, ret);
7161 err = ret;
7162 goto out_end_trans;
7163 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007164 /* if we fail to delete the orphan item this time
7165 * around, it'll get picked up the next time.
7166 *
7167 * The most common failure here is just -ENOENT.
7168 */
7169 btrfs_del_orphan_item(trans, tree_root,
7170 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007171 }
7172 }
7173
7174 if (root->in_radix) {
7175 btrfs_free_fs_root(tree_root->fs_info, root);
7176 } else {
7177 free_extent_buffer(root->node);
7178 free_extent_buffer(root->commit_root);
7179 kfree(root);
7180 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007181out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007182 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007183out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007184 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007185 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007186out:
7187 if (err)
7188 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007189 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007190}
Chris Mason9078a3e2007-04-26 16:46:15 -04007191
Yan Zheng2c47e6052009-06-27 21:07:35 -04007192/*
7193 * drop subtree rooted at tree block 'node'.
7194 *
7195 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007196 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007197 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007198int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7199 struct btrfs_root *root,
7200 struct extent_buffer *node,
7201 struct extent_buffer *parent)
7202{
7203 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007204 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007205 int level;
7206 int parent_level;
7207 int ret = 0;
7208 int wret;
7209
Yan Zheng2c47e6052009-06-27 21:07:35 -04007210 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7211
Yan Zhengf82d02d2008-10-29 14:49:05 -04007212 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007213 if (!path)
7214 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007215
Yan Zheng2c47e6052009-06-27 21:07:35 -04007216 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007217 if (!wc) {
7218 btrfs_free_path(path);
7219 return -ENOMEM;
7220 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007221
Chris Masonb9447ef2009-03-09 11:45:38 -04007222 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007223 parent_level = btrfs_header_level(parent);
7224 extent_buffer_get(parent);
7225 path->nodes[parent_level] = parent;
7226 path->slots[parent_level] = btrfs_header_nritems(parent);
7227
Chris Masonb9447ef2009-03-09 11:45:38 -04007228 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007229 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007230 path->nodes[level] = node;
7231 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007232 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007233
7234 wc->refs[parent_level] = 1;
7235 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7236 wc->level = level;
7237 wc->shared_level = -1;
7238 wc->stage = DROP_REFERENCE;
7239 wc->update_ref = 0;
7240 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007241 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007242 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007243
7244 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007245 wret = walk_down_tree(trans, root, path, wc);
7246 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007247 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007248 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007249 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007250
Yan Zheng2c47e6052009-06-27 21:07:35 -04007251 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007252 if (wret < 0)
7253 ret = wret;
7254 if (wret != 0)
7255 break;
7256 }
7257
Yan Zheng2c47e6052009-06-27 21:07:35 -04007258 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007259 btrfs_free_path(path);
7260 return ret;
7261}
7262
Chris Masonec44a352008-04-28 15:29:52 -04007263static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7264{
7265 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007266 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007267
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007268 /*
7269 * if restripe for this chunk_type is on pick target profile and
7270 * return, otherwise do the usual balance
7271 */
7272 stripped = get_restripe_target(root->fs_info, flags);
7273 if (stripped)
7274 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007275
Chris Masoncd02dca2010-12-13 14:56:23 -05007276 /*
7277 * we add in the count of missing devices because we want
7278 * to make sure that any RAID levels on a degraded FS
7279 * continue to be honored.
7280 */
7281 num_devices = root->fs_info->fs_devices->rw_devices +
7282 root->fs_info->fs_devices->missing_devices;
7283
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007284 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007285 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007286 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7287
Chris Masonec44a352008-04-28 15:29:52 -04007288 if (num_devices == 1) {
7289 stripped |= BTRFS_BLOCK_GROUP_DUP;
7290 stripped = flags & ~stripped;
7291
7292 /* turn raid0 into single device chunks */
7293 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7294 return stripped;
7295
7296 /* turn mirroring into duplication */
7297 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7298 BTRFS_BLOCK_GROUP_RAID10))
7299 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007300 } else {
7301 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007302 if (flags & stripped)
7303 return flags;
7304
7305 stripped |= BTRFS_BLOCK_GROUP_DUP;
7306 stripped = flags & ~stripped;
7307
7308 /* switch duplicated blocks with raid1 */
7309 if (flags & BTRFS_BLOCK_GROUP_DUP)
7310 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7311
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007312 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007313 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007314
Chris Masonec44a352008-04-28 15:29:52 -04007315 return flags;
7316}
7317
Miao Xie199c36e2011-07-15 10:34:36 +00007318static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007319{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007320 struct btrfs_space_info *sinfo = cache->space_info;
7321 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007322 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007323 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007324
Chris Masonc286ac42008-07-22 23:06:41 -04007325
Miao Xie199c36e2011-07-15 10:34:36 +00007326 /*
7327 * We need some metadata space and system metadata space for
7328 * allocating chunks in some corner cases until we force to set
7329 * it to be readonly.
7330 */
7331 if ((sinfo->flags &
7332 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7333 !force)
7334 min_allocable_bytes = 1 * 1024 * 1024;
7335 else
7336 min_allocable_bytes = 0;
7337
Yan, Zhengf0486c62010-05-16 10:46:25 -04007338 spin_lock(&sinfo->lock);
7339 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007340
7341 if (cache->ro) {
7342 ret = 0;
7343 goto out;
7344 }
7345
Yan, Zhengf0486c62010-05-16 10:46:25 -04007346 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7347 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007348
Yan, Zhengf0486c62010-05-16 10:46:25 -04007349 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007350 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7351 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007352 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007353 cache->ro = 1;
7354 ret = 0;
7355 }
WuBo61cfea92011-07-26 03:30:11 +00007356out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007357 spin_unlock(&cache->lock);
7358 spin_unlock(&sinfo->lock);
7359 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007360}
7361
Yan, Zhengf0486c62010-05-16 10:46:25 -04007362int btrfs_set_block_group_ro(struct btrfs_root *root,
7363 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007364
7365{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007366 struct btrfs_trans_handle *trans;
7367 u64 alloc_flags;
7368 int ret;
7369
7370 BUG_ON(cache->ro);
7371
Josef Bacik7a7eaa402011-04-13 12:54:33 -04007372 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007373 if (IS_ERR(trans))
7374 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007375
7376 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007377 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007378 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007379 CHUNK_ALLOC_FORCE);
7380 if (ret < 0)
7381 goto out;
7382 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007383
Miao Xie199c36e2011-07-15 10:34:36 +00007384 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007385 if (!ret)
7386 goto out;
7387 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007388 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007389 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007390 if (ret < 0)
7391 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007392 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007393out:
7394 btrfs_end_transaction(trans, root);
7395 return ret;
7396}
7397
Chris Masonc87f08c2011-02-16 13:57:04 -05007398int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7399 struct btrfs_root *root, u64 type)
7400{
7401 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007402 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007403 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007404}
7405
Miao Xie6d07bce2011-01-05 10:07:31 +00007406/*
7407 * helper to account the unused space of all the readonly block group in the
7408 * list. takes mirrors into account.
7409 */
7410static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7411{
7412 struct btrfs_block_group_cache *block_group;
7413 u64 free_bytes = 0;
7414 int factor;
7415
7416 list_for_each_entry(block_group, groups_list, list) {
7417 spin_lock(&block_group->lock);
7418
7419 if (!block_group->ro) {
7420 spin_unlock(&block_group->lock);
7421 continue;
7422 }
7423
7424 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7425 BTRFS_BLOCK_GROUP_RAID10 |
7426 BTRFS_BLOCK_GROUP_DUP))
7427 factor = 2;
7428 else
7429 factor = 1;
7430
7431 free_bytes += (block_group->key.offset -
7432 btrfs_block_group_used(&block_group->item)) *
7433 factor;
7434
7435 spin_unlock(&block_group->lock);
7436 }
7437
7438 return free_bytes;
7439}
7440
7441/*
7442 * helper to account the unused space of all the readonly block group in the
7443 * space_info. takes mirrors into account.
7444 */
7445u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7446{
7447 int i;
7448 u64 free_bytes = 0;
7449
7450 spin_lock(&sinfo->lock);
7451
7452 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7453 if (!list_empty(&sinfo->block_groups[i]))
7454 free_bytes += __btrfs_get_ro_block_group_free_space(
7455 &sinfo->block_groups[i]);
7456
7457 spin_unlock(&sinfo->lock);
7458
7459 return free_bytes;
7460}
7461
Jeff Mahoney143bede2012-03-01 14:56:26 +01007462void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007463 struct btrfs_block_group_cache *cache)
7464{
7465 struct btrfs_space_info *sinfo = cache->space_info;
7466 u64 num_bytes;
7467
7468 BUG_ON(!cache->ro);
7469
7470 spin_lock(&sinfo->lock);
7471 spin_lock(&cache->lock);
7472 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7473 cache->bytes_super - btrfs_block_group_used(&cache->item);
7474 sinfo->bytes_readonly -= num_bytes;
7475 cache->ro = 0;
7476 spin_unlock(&cache->lock);
7477 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007478}
7479
Josef Bacikba1bf482009-09-11 16:11:19 -04007480/*
7481 * checks to see if its even possible to relocate this block group.
7482 *
7483 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7484 * ok to go ahead and try.
7485 */
7486int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007487{
Zheng Yan1a40e232008-09-26 10:09:34 -04007488 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007489 struct btrfs_space_info *space_info;
7490 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7491 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007492 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007493 u64 dev_min = 1;
7494 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007495 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007496 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007497 int full = 0;
7498 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007499
Josef Bacikba1bf482009-09-11 16:11:19 -04007500 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007501
Josef Bacikba1bf482009-09-11 16:11:19 -04007502 /* odd, couldn't find the block group, leave it alone */
7503 if (!block_group)
7504 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007505
liubocdcb7252011-08-03 10:15:25 +00007506 min_free = btrfs_block_group_used(&block_group->item);
7507
Josef Bacikba1bf482009-09-11 16:11:19 -04007508 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007509 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007510 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007511
Josef Bacikba1bf482009-09-11 16:11:19 -04007512 space_info = block_group->space_info;
7513 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007514
Josef Bacikba1bf482009-09-11 16:11:19 -04007515 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007516
Josef Bacikba1bf482009-09-11 16:11:19 -04007517 /*
7518 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007519 * relocate it unless we're able to allocate a new chunk below.
7520 *
7521 * Otherwise, we need to make sure we have room in the space to handle
7522 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007523 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007524 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007525 (space_info->bytes_used + space_info->bytes_reserved +
7526 space_info->bytes_pinned + space_info->bytes_readonly +
7527 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007528 spin_unlock(&space_info->lock);
7529 goto out;
7530 }
7531 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007532
Josef Bacikba1bf482009-09-11 16:11:19 -04007533 /*
7534 * ok we don't have enough space, but maybe we have free space on our
7535 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007536 * alloc devices and guess if we have enough space. if this block
7537 * group is going to be restriped, run checks against the target
7538 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007539 */
7540 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007541
liubocdcb7252011-08-03 10:15:25 +00007542 /*
7543 * index:
7544 * 0: raid10
7545 * 1: raid1
7546 * 2: dup
7547 * 3: raid0
7548 * 4: single
7549 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007550 target = get_restripe_target(root->fs_info, block_group->flags);
7551 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007552 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007553 } else {
7554 /*
7555 * this is just a balance, so if we were marked as full
7556 * we know there is no space for a new chunk
7557 */
7558 if (full)
7559 goto out;
7560
7561 index = get_block_group_index(block_group);
7562 }
7563
liubocdcb7252011-08-03 10:15:25 +00007564 if (index == 0) {
7565 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007566 /* Divide by 2 */
7567 min_free >>= 1;
liubocdcb7252011-08-03 10:15:25 +00007568 } else if (index == 1) {
7569 dev_min = 2;
7570 } else if (index == 2) {
Josef Bacik6719db62011-08-20 08:29:51 -04007571 /* Multiply by 2 */
7572 min_free <<= 1;
liubocdcb7252011-08-03 10:15:25 +00007573 } else if (index == 3) {
7574 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007575 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007576 }
7577
Josef Bacikba1bf482009-09-11 16:11:19 -04007578 mutex_lock(&root->fs_info->chunk_mutex);
7579 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007580 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007581
Josef Bacikba1bf482009-09-11 16:11:19 -04007582 /*
7583 * check to make sure we can actually find a chunk with enough
7584 * space to fit our block group in.
7585 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007586 if (device->total_bytes > device->bytes_used + min_free &&
7587 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007588 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007589 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007590 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007591 dev_nr++;
7592
7593 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007594 break;
liubocdcb7252011-08-03 10:15:25 +00007595
Josef Bacikba1bf482009-09-11 16:11:19 -04007596 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007597 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007598 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007599 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007600out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007601 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007602 return ret;
7603}
7604
Christoph Hellwigb2950862008-12-02 09:54:17 -05007605static int find_first_block_group(struct btrfs_root *root,
7606 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007607{
Chris Mason925baed2008-06-25 16:01:30 -04007608 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007609 struct btrfs_key found_key;
7610 struct extent_buffer *leaf;
7611 int slot;
7612
7613 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7614 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007615 goto out;
7616
Chris Masond3977122009-01-05 21:25:51 -05007617 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007618 slot = path->slots[0];
7619 leaf = path->nodes[0];
7620 if (slot >= btrfs_header_nritems(leaf)) {
7621 ret = btrfs_next_leaf(root, path);
7622 if (ret == 0)
7623 continue;
7624 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007625 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007626 break;
7627 }
7628 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7629
7630 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007631 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7632 ret = 0;
7633 goto out;
7634 }
Chris Mason0b86a832008-03-24 15:01:56 -04007635 path->slots[0]++;
7636 }
Chris Mason925baed2008-06-25 16:01:30 -04007637out:
Chris Mason0b86a832008-03-24 15:01:56 -04007638 return ret;
7639}
7640
Josef Bacik0af3d002010-06-21 14:48:16 -04007641void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7642{
7643 struct btrfs_block_group_cache *block_group;
7644 u64 last = 0;
7645
7646 while (1) {
7647 struct inode *inode;
7648
7649 block_group = btrfs_lookup_first_block_group(info, last);
7650 while (block_group) {
7651 spin_lock(&block_group->lock);
7652 if (block_group->iref)
7653 break;
7654 spin_unlock(&block_group->lock);
7655 block_group = next_block_group(info->tree_root,
7656 block_group);
7657 }
7658 if (!block_group) {
7659 if (last == 0)
7660 break;
7661 last = 0;
7662 continue;
7663 }
7664
7665 inode = block_group->inode;
7666 block_group->iref = 0;
7667 block_group->inode = NULL;
7668 spin_unlock(&block_group->lock);
7669 iput(inode);
7670 last = block_group->key.objectid + block_group->key.offset;
7671 btrfs_put_block_group(block_group);
7672 }
7673}
7674
Zheng Yan1a40e232008-09-26 10:09:34 -04007675int btrfs_free_block_groups(struct btrfs_fs_info *info)
7676{
7677 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007678 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007679 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007680 struct rb_node *n;
7681
Yan Zheng11833d62009-09-11 16:11:19 -04007682 down_write(&info->extent_commit_sem);
7683 while (!list_empty(&info->caching_block_groups)) {
7684 caching_ctl = list_entry(info->caching_block_groups.next,
7685 struct btrfs_caching_control, list);
7686 list_del(&caching_ctl->list);
7687 put_caching_control(caching_ctl);
7688 }
7689 up_write(&info->extent_commit_sem);
7690
Zheng Yan1a40e232008-09-26 10:09:34 -04007691 spin_lock(&info->block_group_cache_lock);
7692 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7693 block_group = rb_entry(n, struct btrfs_block_group_cache,
7694 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007695 rb_erase(&block_group->cache_node,
7696 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007697 spin_unlock(&info->block_group_cache_lock);
7698
Josef Bacik80eb2342008-10-29 14:49:05 -04007699 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007700 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007701 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007702
Josef Bacik817d52f2009-07-13 21:29:25 -04007703 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007704 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007705
Josef Bacik3c148742011-02-02 15:53:47 +00007706 /*
7707 * We haven't cached this block group, which means we could
7708 * possibly have excluded extents on this block group.
7709 */
7710 if (block_group->cached == BTRFS_CACHE_NO)
7711 free_excluded_extents(info->extent_root, block_group);
7712
Josef Bacik817d52f2009-07-13 21:29:25 -04007713 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007714 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007715
7716 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007717 }
7718 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007719
7720 /* now that all the block groups are freed, go through and
7721 * free all the space_info structs. This is only called during
7722 * the final stages of unmount, and so we know nobody is
7723 * using them. We call synchronize_rcu() once before we start,
7724 * just to be on the safe side.
7725 */
7726 synchronize_rcu();
7727
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007728 release_global_block_rsv(info);
7729
Chris Mason4184ea72009-03-10 12:39:20 -04007730 while(!list_empty(&info->space_info)) {
7731 space_info = list_entry(info->space_info.next,
7732 struct btrfs_space_info,
7733 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007734 if (space_info->bytes_pinned > 0 ||
Josef Bacikfb25e912011-07-26 17:00:46 -04007735 space_info->bytes_reserved > 0 ||
7736 space_info->bytes_may_use > 0) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007737 WARN_ON(1);
7738 dump_space_info(space_info, 0, 0);
7739 }
Chris Mason4184ea72009-03-10 12:39:20 -04007740 list_del(&space_info->list);
7741 kfree(space_info);
7742 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007743 return 0;
7744}
7745
Yan, Zhengb742bb82010-05-16 10:46:24 -04007746static void __link_block_group(struct btrfs_space_info *space_info,
7747 struct btrfs_block_group_cache *cache)
7748{
7749 int index = get_block_group_index(cache);
7750
7751 down_write(&space_info->groups_sem);
7752 list_add_tail(&cache->list, &space_info->block_groups[index]);
7753 up_write(&space_info->groups_sem);
7754}
7755
Chris Mason9078a3e2007-04-26 16:46:15 -04007756int btrfs_read_block_groups(struct btrfs_root *root)
7757{
7758 struct btrfs_path *path;
7759 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007760 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007761 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007762 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007763 struct btrfs_key key;
7764 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007765 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007766 int need_clear = 0;
7767 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007768
Chris Masonbe744172007-05-06 10:15:01 -04007769 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007770 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007771 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007772 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007773 path = btrfs_alloc_path();
7774 if (!path)
7775 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007776 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007777
David Sterba6c417612011-04-13 15:41:04 +02007778 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007779 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02007780 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04007781 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007782 if (btrfs_test_opt(root, CLEAR_CACHE))
7783 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007784
Chris Masond3977122009-01-05 21:25:51 -05007785 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007786 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007787 if (ret > 0)
7788 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007789 if (ret != 0)
7790 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007791 leaf = path->nodes[0];
7792 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007793 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007794 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007795 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007796 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007797 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007798 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7799 GFP_NOFS);
7800 if (!cache->free_space_ctl) {
7801 kfree(cache);
7802 ret = -ENOMEM;
7803 goto error;
7804 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007805
Yan Zhengd2fb3432008-12-11 16:30:39 -05007806 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007807 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007808 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007809 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007810 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007811
Liu Bocf7c1ef2012-07-06 03:31:34 -06007812 if (need_clear) {
7813 /*
7814 * When we mount with old space cache, we need to
7815 * set BTRFS_DC_CLEAR and set dirty flag.
7816 *
7817 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7818 * truncate the old free space cache inode and
7819 * setup a new one.
7820 * b) Setting 'dirty flag' makes sure that we flush
7821 * the new space cache info onto disk.
7822 */
Josef Bacik0af3d002010-06-21 14:48:16 -04007823 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007824 if (btrfs_test_opt(root, SPACE_CACHE))
7825 cache->dirty = 1;
7826 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007827
Chris Mason5f39d392007-10-15 16:14:19 -04007828 read_extent_buffer(leaf, &cache->item,
7829 btrfs_item_ptr_offset(leaf, path->slots[0]),
7830 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007831 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007832
Chris Mason9078a3e2007-04-26 16:46:15 -04007833 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007834 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007835 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007836 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05007837 cache->full_stripe_len = btrfs_full_stripe_len(root,
7838 &root->fs_info->mapping_tree,
7839 found_key.objectid);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007840 btrfs_init_free_space_ctl(cache);
7841
Josef Bacik817d52f2009-07-13 21:29:25 -04007842 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007843 * We need to exclude the super stripes now so that the space
7844 * info has super bytes accounted for, otherwise we'll think
7845 * we have more space than we actually do.
7846 */
7847 exclude_super_stripes(root, cache);
7848
7849 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007850 * check for two cases, either we are full, and therefore
7851 * don't need to bother with the caching work since we won't
7852 * find any space, or we are empty, and we can just add all
7853 * the space in and be done with it. This saves us _alot_ of
7854 * time, particularly in the full case.
7855 */
7856 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007857 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007858 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007859 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007860 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007861 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007862 cache->cached = BTRFS_CACHE_FINISHED;
7863 add_new_free_space(cache, root->fs_info,
7864 found_key.objectid,
7865 found_key.objectid +
7866 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007867 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007868 }
Chris Mason96b51792007-10-15 16:15:19 -04007869
Chris Mason6324fbf2008-03-24 15:01:59 -04007870 ret = update_space_info(info, cache->flags, found_key.offset,
7871 btrfs_block_group_used(&cache->item),
7872 &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007873 BUG_ON(ret); /* -ENOMEM */
Chris Mason6324fbf2008-03-24 15:01:59 -04007874 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007875 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007876 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007877 spin_unlock(&cache->space_info->lock);
7878
Yan, Zhengb742bb82010-05-16 10:46:24 -04007879 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007880
Josef Bacik0f9dd462008-09-23 13:14:11 -04007881 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007882 BUG_ON(ret); /* Logic error */
Chris Mason75ccf472008-09-30 19:24:06 -04007883
7884 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007885 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007886 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007887 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007888
7889 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7890 if (!(get_alloc_profile(root, space_info->flags) &
7891 (BTRFS_BLOCK_GROUP_RAID10 |
7892 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007893 BTRFS_BLOCK_GROUP_RAID5 |
7894 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04007895 BTRFS_BLOCK_GROUP_DUP)))
7896 continue;
7897 /*
7898 * avoid allocating from un-mirrored block group if there are
7899 * mirrored block groups.
7900 */
7901 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007902 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007903 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007904 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007905 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007906
7907 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007908 ret = 0;
7909error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007910 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007911 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007912}
Chris Mason6324fbf2008-03-24 15:01:59 -04007913
Josef Bacikea658ba2012-09-11 16:57:25 -04007914void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
7915 struct btrfs_root *root)
7916{
7917 struct btrfs_block_group_cache *block_group, *tmp;
7918 struct btrfs_root *extent_root = root->fs_info->extent_root;
7919 struct btrfs_block_group_item item;
7920 struct btrfs_key key;
7921 int ret = 0;
7922
7923 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
7924 new_bg_list) {
7925 list_del_init(&block_group->new_bg_list);
7926
7927 if (ret)
7928 continue;
7929
7930 spin_lock(&block_group->lock);
7931 memcpy(&item, &block_group->item, sizeof(item));
7932 memcpy(&key, &block_group->key, sizeof(key));
7933 spin_unlock(&block_group->lock);
7934
7935 ret = btrfs_insert_item(trans, extent_root, &key, &item,
7936 sizeof(item));
7937 if (ret)
7938 btrfs_abort_transaction(trans, extent_root, ret);
7939 }
7940}
7941
Chris Mason6324fbf2008-03-24 15:01:59 -04007942int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7943 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007944 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007945 u64 size)
7946{
7947 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007948 struct btrfs_root *extent_root;
7949 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007950
7951 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007952
Chris Mason12fcfd22009-03-24 10:24:20 -04007953 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007954
Chris Mason8f18cf12008-04-25 16:53:30 -04007955 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007956 if (!cache)
7957 return -ENOMEM;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007958 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7959 GFP_NOFS);
7960 if (!cache->free_space_ctl) {
7961 kfree(cache);
7962 return -ENOMEM;
7963 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007964
Chris Masone17cade2008-04-15 15:41:47 -04007965 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007966 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007967 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007968 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007969 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05007970 cache->full_stripe_len = btrfs_full_stripe_len(root,
7971 &root->fs_info->mapping_tree,
7972 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04007973
Yan Zhengd2fb3432008-12-11 16:30:39 -05007974 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007975 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007976 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007977 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04007978 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007979
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007980 btrfs_init_free_space_ctl(cache);
7981
Chris Mason6324fbf2008-03-24 15:01:59 -04007982 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007983 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7984 cache->flags = type;
7985 btrfs_set_block_group_flags(&cache->item, type);
7986
Yan Zheng11833d62009-09-11 16:11:19 -04007987 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007988 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007989 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007990
Josef Bacik817d52f2009-07-13 21:29:25 -04007991 add_new_free_space(cache, root->fs_info, chunk_offset,
7992 chunk_offset + size);
7993
Yan Zheng11833d62009-09-11 16:11:19 -04007994 free_excluded_extents(root, cache);
7995
Chris Mason6324fbf2008-03-24 15:01:59 -04007996 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7997 &cache->space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007998 BUG_ON(ret); /* -ENOMEM */
Li Zefanc7c144d2011-12-07 10:39:22 +08007999 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008000
8001 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008002 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008003 spin_unlock(&cache->space_info->lock);
8004
Yan, Zhengb742bb82010-05-16 10:46:24 -04008005 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008006
Josef Bacik0f9dd462008-09-23 13:14:11 -04008007 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008008 BUG_ON(ret); /* Logic error */
Chris Masonc286ac42008-07-22 23:06:41 -04008009
Josef Bacikea658ba2012-09-11 16:57:25 -04008010 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008011
Chris Masond18a2c42008-04-04 15:40:00 -04008012 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008013
Chris Mason6324fbf2008-03-24 15:01:59 -04008014 return 0;
8015}
Zheng Yan1a40e232008-09-26 10:09:34 -04008016
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008017static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8018{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008019 u64 extra_flags = chunk_to_extended(flags) &
8020 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008021
8022 if (flags & BTRFS_BLOCK_GROUP_DATA)
8023 fs_info->avail_data_alloc_bits &= ~extra_flags;
8024 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8025 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8026 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8027 fs_info->avail_system_alloc_bits &= ~extra_flags;
8028}
8029
Zheng Yan1a40e232008-09-26 10:09:34 -04008030int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8031 struct btrfs_root *root, u64 group_start)
8032{
8033 struct btrfs_path *path;
8034 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008035 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008036 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008037 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008038 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008039 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008040 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008041 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008042
Zheng Yan1a40e232008-09-26 10:09:34 -04008043 root = root->fs_info->extent_root;
8044
8045 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8046 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008047 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008048
liubo9f7c43c2011-03-07 02:13:33 +00008049 /*
8050 * Free the reserved super bytes from this block group before
8051 * remove it.
8052 */
8053 free_excluded_extents(root, block_group);
8054
Zheng Yan1a40e232008-09-26 10:09:34 -04008055 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008056 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008057 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8058 BTRFS_BLOCK_GROUP_RAID1 |
8059 BTRFS_BLOCK_GROUP_RAID10))
8060 factor = 2;
8061 else
8062 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008063
Chris Mason44fb5512009-06-04 15:34:51 -04008064 /* make sure this block group isn't part of an allocation cluster */
8065 cluster = &root->fs_info->data_alloc_cluster;
8066 spin_lock(&cluster->refill_lock);
8067 btrfs_return_cluster_to_free_space(block_group, cluster);
8068 spin_unlock(&cluster->refill_lock);
8069
8070 /*
8071 * make sure this block group isn't part of a metadata
8072 * allocation cluster
8073 */
8074 cluster = &root->fs_info->meta_alloc_cluster;
8075 spin_lock(&cluster->refill_lock);
8076 btrfs_return_cluster_to_free_space(block_group, cluster);
8077 spin_unlock(&cluster->refill_lock);
8078
Zheng Yan1a40e232008-09-26 10:09:34 -04008079 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008080 if (!path) {
8081 ret = -ENOMEM;
8082 goto out;
8083 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008084
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008085 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008086 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008087 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008088 if (ret) {
8089 btrfs_add_delayed_iput(inode);
8090 goto out;
8091 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008092 clear_nlink(inode);
8093 /* One for the block groups ref */
8094 spin_lock(&block_group->lock);
8095 if (block_group->iref) {
8096 block_group->iref = 0;
8097 block_group->inode = NULL;
8098 spin_unlock(&block_group->lock);
8099 iput(inode);
8100 } else {
8101 spin_unlock(&block_group->lock);
8102 }
8103 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008104 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008105 }
8106
8107 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8108 key.offset = block_group->key.objectid;
8109 key.type = 0;
8110
8111 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8112 if (ret < 0)
8113 goto out;
8114 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008115 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008116 if (ret == 0) {
8117 ret = btrfs_del_item(trans, tree_root, path);
8118 if (ret)
8119 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008120 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008121 }
8122
Yan Zheng3dfdb932009-01-21 10:49:16 -05008123 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008124 rb_erase(&block_group->cache_node,
8125 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008126 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008127
Josef Bacik80eb2342008-10-29 14:49:05 -04008128 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008129 /*
8130 * we must use list_del_init so people can check to see if they
8131 * are still on the list after taking the semaphore
8132 */
8133 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008134 if (list_empty(&block_group->space_info->block_groups[index]))
8135 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008136 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008137
Josef Bacik817d52f2009-07-13 21:29:25 -04008138 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008139 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008140
8141 btrfs_remove_free_space_cache(block_group);
8142
Yan Zhengc146afa2008-11-12 14:34:12 -05008143 spin_lock(&block_group->space_info->lock);
8144 block_group->space_info->total_bytes -= block_group->key.offset;
8145 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008146 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008147 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008148
Josef Bacik0af3d002010-06-21 14:48:16 -04008149 memcpy(&key, &block_group->key, sizeof(key));
8150
Chris Mason283bb192009-07-24 16:30:55 -04008151 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008152
Chris Masonfa9c0d72009-04-03 09:47:43 -04008153 btrfs_put_block_group(block_group);
8154 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008155
8156 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8157 if (ret > 0)
8158 ret = -EIO;
8159 if (ret < 0)
8160 goto out;
8161
8162 ret = btrfs_del_item(trans, root, path);
8163out:
8164 btrfs_free_path(path);
8165 return ret;
8166}
liuboacce9522011-01-06 19:30:25 +08008167
liuboc59021f2011-03-07 02:13:14 +00008168int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8169{
8170 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008171 struct btrfs_super_block *disk_super;
8172 u64 features;
8173 u64 flags;
8174 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008175 int ret;
8176
David Sterba6c417612011-04-13 15:41:04 +02008177 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008178 if (!btrfs_super_root(disk_super))
8179 return 1;
liuboc59021f2011-03-07 02:13:14 +00008180
liubo1aba86d2011-04-08 08:44:37 +00008181 features = btrfs_super_incompat_flags(disk_super);
8182 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8183 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008184
liubo1aba86d2011-04-08 08:44:37 +00008185 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8186 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008187 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008188 goto out;
liuboc59021f2011-03-07 02:13:14 +00008189
liubo1aba86d2011-04-08 08:44:37 +00008190 if (mixed) {
8191 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8192 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8193 } else {
8194 flags = BTRFS_BLOCK_GROUP_METADATA;
8195 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8196 if (ret)
8197 goto out;
8198
8199 flags = BTRFS_BLOCK_GROUP_DATA;
8200 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8201 }
8202out:
liuboc59021f2011-03-07 02:13:14 +00008203 return ret;
8204}
8205
liuboacce9522011-01-06 19:30:25 +08008206int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8207{
8208 return unpin_extent_range(root, start, end);
8209}
8210
8211int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008212 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008213{
Li Dongyang5378e602011-03-24 10:24:27 +00008214 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008215}
Li Dongyangf7039b12011-03-24 10:24:28 +00008216
8217int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8218{
8219 struct btrfs_fs_info *fs_info = root->fs_info;
8220 struct btrfs_block_group_cache *cache = NULL;
8221 u64 group_trimmed;
8222 u64 start;
8223 u64 end;
8224 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008225 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008226 int ret = 0;
8227
Liu Bo2cac13e2012-02-09 18:17:41 +08008228 /*
8229 * try to trim all FS space, our block group may start from non-zero.
8230 */
8231 if (range->len == total_bytes)
8232 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8233 else
8234 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008235
8236 while (cache) {
8237 if (cache->key.objectid >= (range->start + range->len)) {
8238 btrfs_put_block_group(cache);
8239 break;
8240 }
8241
8242 start = max(range->start, cache->key.objectid);
8243 end = min(range->start + range->len,
8244 cache->key.objectid + cache->key.offset);
8245
8246 if (end - start >= range->minlen) {
8247 if (!block_group_cache_done(cache)) {
8248 ret = cache_block_group(cache, NULL, root, 0);
8249 if (!ret)
8250 wait_block_group_cache_done(cache);
8251 }
8252 ret = btrfs_trim_block_group(cache,
8253 &group_trimmed,
8254 start,
8255 end,
8256 range->minlen);
8257
8258 trimmed += group_trimmed;
8259 if (ret) {
8260 btrfs_put_block_group(cache);
8261 break;
8262 }
8263 }
8264
8265 cache = next_block_group(fs_info->tree_root, cache);
8266 }
8267
8268 range->len = trimmed;
8269 return ret;
8270}