blob: 7cb9d734f6e51cc0266f1170810cf61330c7eda3 [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
Liu Boc53d6132012-12-27 09:01:19 +000076static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040077 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040078static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
79 struct btrfs_root *root,
80 u64 bytenr, u64 num_bytes, u64 parent,
81 u64 root_objectid, u64 owner_objectid,
82 u64 owner_offset, int refs_to_drop,
83 struct btrfs_delayed_extent_op *extra_op);
84static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
85 struct extent_buffer *leaf,
86 struct btrfs_extent_item *ei);
87static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
88 struct btrfs_root *root,
89 u64 parent, u64 root_objectid,
90 u64 flags, u64 owner, u64 offset,
91 struct btrfs_key *ins, int ref_mod);
92static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
93 struct btrfs_root *root,
94 u64 parent, u64 root_objectid,
95 u64 flags, struct btrfs_disk_key *key,
96 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050097static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -040098 struct btrfs_root *extent_root, u64 flags,
99 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400100static int find_next_key(struct btrfs_path *path, int level,
101 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400102static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
103 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400104static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
105 u64 num_bytes, int reserve);
Josef Bacik5d803662013-02-07 16:06:02 -0500106static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
107 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500108
Josef Bacik817d52f2009-07-13 21:29:25 -0400109static noinline int
110block_group_cache_done(struct btrfs_block_group_cache *cache)
111{
112 smp_mb();
113 return cache->cached == BTRFS_CACHE_FINISHED;
114}
115
Josef Bacik0f9dd462008-09-23 13:14:11 -0400116static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
117{
118 return (cache->flags & bits) == bits;
119}
120
David Sterba62a45b62011-04-20 15:52:26 +0200121static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000122{
123 atomic_inc(&cache->count);
124}
125
126void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
127{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400128 if (atomic_dec_and_test(&cache->count)) {
129 WARN_ON(cache->pinned > 0);
130 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800131 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000132 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400133 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000134}
135
Josef Bacik0f9dd462008-09-23 13:14:11 -0400136/*
137 * this adds the block group to the fs_info rb tree for the block group
138 * cache
139 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500140static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400141 struct btrfs_block_group_cache *block_group)
142{
143 struct rb_node **p;
144 struct rb_node *parent = NULL;
145 struct btrfs_block_group_cache *cache;
146
147 spin_lock(&info->block_group_cache_lock);
148 p = &info->block_group_cache_tree.rb_node;
149
150 while (*p) {
151 parent = *p;
152 cache = rb_entry(parent, struct btrfs_block_group_cache,
153 cache_node);
154 if (block_group->key.objectid < cache->key.objectid) {
155 p = &(*p)->rb_left;
156 } else if (block_group->key.objectid > cache->key.objectid) {
157 p = &(*p)->rb_right;
158 } else {
159 spin_unlock(&info->block_group_cache_lock);
160 return -EEXIST;
161 }
162 }
163
164 rb_link_node(&block_group->cache_node, parent, p);
165 rb_insert_color(&block_group->cache_node,
166 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000167
168 if (info->first_logical_byte > block_group->key.objectid)
169 info->first_logical_byte = block_group->key.objectid;
170
Josef Bacik0f9dd462008-09-23 13:14:11 -0400171 spin_unlock(&info->block_group_cache_lock);
172
173 return 0;
174}
175
176/*
177 * This will return the block group at or after bytenr if contains is 0, else
178 * it will return the block group that contains the bytenr
179 */
180static struct btrfs_block_group_cache *
181block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
182 int contains)
183{
184 struct btrfs_block_group_cache *cache, *ret = NULL;
185 struct rb_node *n;
186 u64 end, start;
187
188 spin_lock(&info->block_group_cache_lock);
189 n = info->block_group_cache_tree.rb_node;
190
191 while (n) {
192 cache = rb_entry(n, struct btrfs_block_group_cache,
193 cache_node);
194 end = cache->key.objectid + cache->key.offset - 1;
195 start = cache->key.objectid;
196
197 if (bytenr < start) {
198 if (!contains && (!ret || start < ret->key.objectid))
199 ret = cache;
200 n = n->rb_left;
201 } else if (bytenr > start) {
202 if (contains && bytenr <= end) {
203 ret = cache;
204 break;
205 }
206 n = n->rb_right;
207 } else {
208 ret = cache;
209 break;
210 }
211 }
Liu Boa1897fd2012-12-27 09:01:23 +0000212 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000213 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000214 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
215 info->first_logical_byte = ret->key.objectid;
216 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400217 spin_unlock(&info->block_group_cache_lock);
218
219 return ret;
220}
221
Yan Zheng11833d62009-09-11 16:11:19 -0400222static int add_excluded_extent(struct btrfs_root *root,
223 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400224{
Yan Zheng11833d62009-09-11 16:11:19 -0400225 u64 end = start + num_bytes - 1;
226 set_extent_bits(&root->fs_info->freed_extents[0],
227 start, end, EXTENT_UPTODATE, GFP_NOFS);
228 set_extent_bits(&root->fs_info->freed_extents[1],
229 start, end, EXTENT_UPTODATE, GFP_NOFS);
230 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400231}
232
Yan Zheng11833d62009-09-11 16:11:19 -0400233static void free_excluded_extents(struct btrfs_root *root,
234 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400235{
Yan Zheng11833d62009-09-11 16:11:19 -0400236 u64 start, end;
237
238 start = cache->key.objectid;
239 end = start + cache->key.offset - 1;
240
241 clear_extent_bits(&root->fs_info->freed_extents[0],
242 start, end, EXTENT_UPTODATE, GFP_NOFS);
243 clear_extent_bits(&root->fs_info->freed_extents[1],
244 start, end, EXTENT_UPTODATE, GFP_NOFS);
245}
246
247static int exclude_super_stripes(struct btrfs_root *root,
248 struct btrfs_block_group_cache *cache)
249{
Josef Bacik817d52f2009-07-13 21:29:25 -0400250 u64 bytenr;
251 u64 *logical;
252 int stripe_len;
253 int i, nr, ret;
254
Yan, Zheng06b23312009-11-26 09:31:11 +0000255 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
256 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
257 cache->bytes_super += stripe_len;
258 ret = add_excluded_extent(root, cache->key.objectid,
259 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100260 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng06b23312009-11-26 09:31:11 +0000261 }
262
Josef Bacik817d52f2009-07-13 21:29:25 -0400263 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
264 bytenr = btrfs_sb_offset(i);
265 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
266 cache->key.objectid, bytenr,
267 0, &logical, &nr, &stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100268 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -0400269
Josef Bacik817d52f2009-07-13 21:29:25 -0400270 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400271 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400272 ret = add_excluded_extent(root, logical[nr],
273 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100274 BUG_ON(ret); /* -ENOMEM */
Josef Bacik817d52f2009-07-13 21:29:25 -0400275 }
Yan Zheng11833d62009-09-11 16:11:19 -0400276
Josef Bacik817d52f2009-07-13 21:29:25 -0400277 kfree(logical);
278 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 return 0;
280}
281
Yan Zheng11833d62009-09-11 16:11:19 -0400282static struct btrfs_caching_control *
283get_caching_control(struct btrfs_block_group_cache *cache)
284{
285 struct btrfs_caching_control *ctl;
286
287 spin_lock(&cache->lock);
288 if (cache->cached != BTRFS_CACHE_STARTED) {
289 spin_unlock(&cache->lock);
290 return NULL;
291 }
292
Josef Bacikdde5abe2010-09-16 16:17:03 -0400293 /* We're loading it the fast way, so we don't have a caching_ctl. */
294 if (!cache->caching_ctl) {
295 spin_unlock(&cache->lock);
296 return NULL;
297 }
298
Yan Zheng11833d62009-09-11 16:11:19 -0400299 ctl = cache->caching_ctl;
300 atomic_inc(&ctl->count);
301 spin_unlock(&cache->lock);
302 return ctl;
303}
304
305static void put_caching_control(struct btrfs_caching_control *ctl)
306{
307 if (atomic_dec_and_test(&ctl->count))
308 kfree(ctl);
309}
310
Josef Bacik0f9dd462008-09-23 13:14:11 -0400311/*
312 * this is only called by cache_block_group, since we could have freed extents
313 * we need to check the pinned_extents for any extents that can't be used yet
314 * since their free space will be released as soon as the transaction commits.
315 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400316static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 struct btrfs_fs_info *info, u64 start, u64 end)
318{
Josef Bacik817d52f2009-07-13 21:29:25 -0400319 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320 int ret;
321
322 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400323 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400324 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400325 EXTENT_DIRTY | EXTENT_UPTODATE,
326 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400327 if (ret)
328 break;
329
Yan, Zheng06b23312009-11-26 09:31:11 +0000330 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400331 start = extent_end + 1;
332 } else if (extent_start > start && extent_start < end) {
333 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400334 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500335 ret = btrfs_add_free_space(block_group, start,
336 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100337 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400338 start = extent_end + 1;
339 } else {
340 break;
341 }
342 }
343
344 if (start < end) {
345 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400346 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500347 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400349 }
350
Josef Bacik817d52f2009-07-13 21:29:25 -0400351 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400352}
353
Josef Bacikbab39bf2011-06-30 14:42:28 -0400354static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400355{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400356 struct btrfs_block_group_cache *block_group;
357 struct btrfs_fs_info *fs_info;
358 struct btrfs_caching_control *caching_ctl;
359 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400360 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400361 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400362 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400363 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400364 u64 last = 0;
365 u32 nritems;
366 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400367
Josef Bacikbab39bf2011-06-30 14:42:28 -0400368 caching_ctl = container_of(work, struct btrfs_caching_control, work);
369 block_group = caching_ctl->block_group;
370 fs_info = block_group->fs_info;
371 extent_root = fs_info->extent_root;
372
Chris Masone37c9e62007-05-09 20:13:14 -0400373 path = btrfs_alloc_path();
374 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400375 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400376
Josef Bacik817d52f2009-07-13 21:29:25 -0400377 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400378
Chris Mason5cd57b22008-06-25 16:01:30 -0400379 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400380 * We don't want to deadlock with somebody trying to allocate a new
381 * extent for the extent root while also trying to search the extent
382 * root to add free space. So we skip locking and search the commit
383 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400384 */
385 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400386 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400387 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400388
Yan Zhenge4404d62008-12-12 10:03:26 -0500389 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400390 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400391 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400392again:
Yan Zheng11833d62009-09-11 16:11:19 -0400393 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400394 /* need to make sure the commit_root doesn't disappear */
395 down_read(&fs_info->extent_commit_sem);
396
Yan Zheng11833d62009-09-11 16:11:19 -0400397 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400398 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400399 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500400
Yan Zheng11833d62009-09-11 16:11:19 -0400401 leaf = path->nodes[0];
402 nritems = btrfs_header_nritems(leaf);
403
Chris Masond3977122009-01-05 21:25:51 -0500404 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200405 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400406 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400407 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400408 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400409
Yan Zheng11833d62009-09-11 16:11:19 -0400410 if (path->slots[0] < nritems) {
411 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
412 } else {
413 ret = find_next_key(path, 0, &key);
414 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400415 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400416
Josef Bacik589d8ad2011-05-11 17:30:53 -0400417 if (need_resched() ||
418 btrfs_next_leaf(extent_root, path)) {
419 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400420 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400421 up_read(&fs_info->extent_commit_sem);
422 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400423 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400424 goto again;
425 }
426 leaf = path->nodes[0];
427 nritems = btrfs_header_nritems(leaf);
428 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400429 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 if (key.objectid < block_group->key.objectid) {
432 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400433 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400434 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400435
Chris Masone37c9e62007-05-09 20:13:14 -0400436 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400437 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400438 break;
Yan7d7d6062007-09-14 16:15:28 -0400439
Yan Zheng11833d62009-09-11 16:11:19 -0400440 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400441 total_found += add_new_free_space(block_group,
442 fs_info, last,
443 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400444 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400445
Yan Zheng11833d62009-09-11 16:11:19 -0400446 if (total_found > (1024 * 1024 * 2)) {
447 total_found = 0;
448 wake_up(&caching_ctl->wait);
449 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400450 }
Chris Masone37c9e62007-05-09 20:13:14 -0400451 path->slots[0]++;
452 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400453 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400454
455 total_found += add_new_free_space(block_group, fs_info, last,
456 block_group->key.objectid +
457 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400458 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400459
460 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400461 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400462 block_group->cached = BTRFS_CACHE_FINISHED;
463 spin_unlock(&block_group->lock);
464
Chris Mason54aa1f42007-06-22 14:16:25 -0400465err:
Chris Masone37c9e62007-05-09 20:13:14 -0400466 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400467 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400468
Yan Zheng11833d62009-09-11 16:11:19 -0400469 free_excluded_extents(extent_root, block_group);
470
471 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400472out:
Yan Zheng11833d62009-09-11 16:11:19 -0400473 wake_up(&caching_ctl->wait);
474
475 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000476 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400477}
478
Josef Bacik9d66e232010-08-25 16:54:15 -0400479static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400480 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400481{
Josef Bacik291c7d22011-11-14 13:52:14 -0500482 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400483 struct btrfs_fs_info *fs_info = cache->fs_info;
484 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400485 int ret = 0;
486
Josef Bacik291c7d22011-11-14 13:52:14 -0500487 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100488 if (!caching_ctl)
489 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500490
491 INIT_LIST_HEAD(&caching_ctl->list);
492 mutex_init(&caching_ctl->mutex);
493 init_waitqueue_head(&caching_ctl->wait);
494 caching_ctl->block_group = cache;
495 caching_ctl->progress = cache->key.objectid;
496 atomic_set(&caching_ctl->count, 1);
497 caching_ctl->work.func = caching_thread;
498
499 spin_lock(&cache->lock);
500 /*
501 * This should be a rare occasion, but this could happen I think in the
502 * case where one thread starts to load the space cache info, and then
503 * some other thread starts a transaction commit which tries to do an
504 * allocation while the other thread is still loading the space cache
505 * info. The previous loop should have kept us from choosing this block
506 * group, but if we've moved to the state where we will wait on caching
507 * block groups we need to first check if we're doing a fast load here,
508 * so we can wait for it to finish, otherwise we could end up allocating
509 * from a block group who's cache gets evicted for one reason or
510 * another.
511 */
512 while (cache->cached == BTRFS_CACHE_FAST) {
513 struct btrfs_caching_control *ctl;
514
515 ctl = cache->caching_ctl;
516 atomic_inc(&ctl->count);
517 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
518 spin_unlock(&cache->lock);
519
520 schedule();
521
522 finish_wait(&ctl->wait, &wait);
523 put_caching_control(ctl);
524 spin_lock(&cache->lock);
525 }
526
527 if (cache->cached != BTRFS_CACHE_NO) {
528 spin_unlock(&cache->lock);
529 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400530 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500531 }
532 WARN_ON(cache->caching_ctl);
533 cache->caching_ctl = caching_ctl;
534 cache->cached = BTRFS_CACHE_FAST;
535 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -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 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002149 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002150 }
Josef Bacikeb099672009-02-12 09:27:38 -05002151
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002152 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2153 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2154 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2155 insert_reserved);
2156 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2157 node->type == BTRFS_SHARED_DATA_REF_KEY)
2158 ret = run_delayed_data_ref(trans, root, node, extent_op,
2159 insert_reserved);
2160 else
2161 BUG();
2162 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002163}
2164
Chris Mason56bec292009-03-13 10:10:06 -04002165static noinline struct btrfs_delayed_ref_node *
2166select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002167{
Chris Mason56bec292009-03-13 10:10:06 -04002168 struct rb_node *node;
2169 struct btrfs_delayed_ref_node *ref;
2170 int action = BTRFS_ADD_DELAYED_REF;
2171again:
2172 /*
2173 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2174 * this prevents ref count from going down to zero when
2175 * there still are pending delayed ref.
2176 */
2177 node = rb_prev(&head->node.rb_node);
2178 while (1) {
2179 if (!node)
2180 break;
2181 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2182 rb_node);
2183 if (ref->bytenr != head->node.bytenr)
2184 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002185 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002186 return ref;
2187 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002188 }
Chris Mason56bec292009-03-13 10:10:06 -04002189 if (action == BTRFS_ADD_DELAYED_REF) {
2190 action = BTRFS_DROP_DELAYED_REF;
2191 goto again;
2192 }
2193 return NULL;
2194}
2195
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002196/*
2197 * Returns 0 on success or if called with an already aborted transaction.
2198 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2199 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002200static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2201 struct btrfs_root *root,
2202 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002203{
Chris Mason56bec292009-03-13 10:10:06 -04002204 struct btrfs_delayed_ref_root *delayed_refs;
2205 struct btrfs_delayed_ref_node *ref;
2206 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002207 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002208 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002209 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002210 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002211 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002212
2213 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002214 while (1) {
2215 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002216 /* pick a new head ref from the cluster list */
2217 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002218 break;
Chris Mason56bec292009-03-13 10:10:06 -04002219
Chris Masonc3e69d52009-03-13 10:17:05 -04002220 locked_ref = list_entry(cluster->next,
2221 struct btrfs_delayed_ref_head, cluster);
2222
2223 /* grab the lock that says we are going to process
2224 * all the refs for this head */
2225 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2226
2227 /*
2228 * we may have dropped the spin lock to get the head
2229 * mutex lock, and that might have given someone else
2230 * time to free the head. If that's true, it has been
2231 * removed from our list and we can move on.
2232 */
2233 if (ret == -EAGAIN) {
2234 locked_ref = NULL;
2235 count++;
2236 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002237 }
2238 }
2239
2240 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002241 * We need to try and merge add/drops of the same ref since we
2242 * can run into issues with relocate dropping the implicit ref
2243 * and then it being added back again before the drop can
2244 * finish. If we merged anything we need to re-loop so we can
2245 * get a good ref.
2246 */
2247 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2248 locked_ref);
2249
2250 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002251 * locked_ref is the head node, so we have to go one
2252 * node back for any delayed ref updates
2253 */
2254 ref = select_delayed_ref(locked_ref);
2255
2256 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002257 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002258 /*
2259 * there are still refs with lower seq numbers in the
2260 * process of being added. Don't run this ref yet.
2261 */
2262 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002263 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002264 locked_ref = NULL;
2265 delayed_refs->num_heads_ready++;
2266 spin_unlock(&delayed_refs->lock);
2267 cond_resched();
2268 spin_lock(&delayed_refs->lock);
2269 continue;
2270 }
2271
2272 /*
Chris Mason56bec292009-03-13 10:10:06 -04002273 * record the must insert reserved flag before we
2274 * drop the spin lock.
2275 */
2276 must_insert_reserved = locked_ref->must_insert_reserved;
2277 locked_ref->must_insert_reserved = 0;
2278
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002279 extent_op = locked_ref->extent_op;
2280 locked_ref->extent_op = NULL;
2281
Chris Mason56bec292009-03-13 10:10:06 -04002282 if (!ref) {
2283 /* All delayed refs have been processed, Go ahead
2284 * and send the head node to run_one_delayed_ref,
2285 * so that any accounting fixes can happen
2286 */
2287 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288
2289 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002290 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002291 extent_op = NULL;
2292 }
2293
2294 if (extent_op) {
2295 spin_unlock(&delayed_refs->lock);
2296
2297 ret = run_delayed_extent_op(trans, root,
2298 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002299 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002300
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002301 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002302 printk(KERN_DEBUG
2303 "btrfs: run_delayed_extent_op "
2304 "returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002305 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002306 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002307 return ret;
2308 }
2309
Chris Mason203bf282012-01-06 15:23:57 -05002310 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002311 }
Chris Mason56bec292009-03-13 10:10:06 -04002312 }
2313
2314 ref->in_tree = 0;
2315 rb_erase(&ref->rb_node, &delayed_refs->root);
2316 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002317 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002318 /*
2319 * when we play the delayed ref, also correct the
2320 * ref_mod on head
2321 */
2322 switch (ref->action) {
2323 case BTRFS_ADD_DELAYED_REF:
2324 case BTRFS_ADD_DELAYED_EXTENT:
2325 locked_ref->node.ref_mod -= ref->ref_mod;
2326 break;
2327 case BTRFS_DROP_DELAYED_REF:
2328 locked_ref->node.ref_mod += ref->ref_mod;
2329 break;
2330 default:
2331 WARN_ON(1);
2332 }
2333 }
Chris Mason56bec292009-03-13 10:10:06 -04002334 spin_unlock(&delayed_refs->lock);
2335
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002336 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002337 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002338
Miao Xie78a61842012-11-21 02:21:28 +00002339 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002340 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002341 btrfs_delayed_ref_unlock(locked_ref);
2342 btrfs_put_delayed_ref(ref);
2343 printk(KERN_DEBUG
2344 "btrfs: run_one_delayed_ref returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002345 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002346 return ret;
2347 }
2348
Miao Xie093486c2012-12-19 08:10:10 +00002349 /*
2350 * If this node is a head, that means all the refs in this head
2351 * have been dealt with, and we will pick the next head to deal
2352 * with, so we must unlock the head and drop it from the cluster
2353 * list before we release it.
2354 */
2355 if (btrfs_delayed_ref_is_head(ref)) {
2356 list_del_init(&locked_ref->cluster);
2357 btrfs_delayed_ref_unlock(locked_ref);
2358 locked_ref = NULL;
2359 }
2360 btrfs_put_delayed_ref(ref);
2361 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002362next:
Chris Mason1887be62009-03-13 10:11:24 -04002363 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002364 spin_lock(&delayed_refs->lock);
2365 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002366 return count;
2367}
2368
Arne Jansen709c0482011-09-12 12:22:57 +02002369#ifdef SCRAMBLE_DELAYED_REFS
2370/*
2371 * Normally delayed refs get processed in ascending bytenr order. This
2372 * correlates in most cases to the order added. To expose dependencies on this
2373 * order, we start to process the tree in the middle instead of the beginning
2374 */
2375static u64 find_middle(struct rb_root *root)
2376{
2377 struct rb_node *n = root->rb_node;
2378 struct btrfs_delayed_ref_node *entry;
2379 int alt = 1;
2380 u64 middle;
2381 u64 first = 0, last = 0;
2382
2383 n = rb_first(root);
2384 if (n) {
2385 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2386 first = entry->bytenr;
2387 }
2388 n = rb_last(root);
2389 if (n) {
2390 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2391 last = entry->bytenr;
2392 }
2393 n = root->rb_node;
2394
2395 while (n) {
2396 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2397 WARN_ON(!entry->in_tree);
2398
2399 middle = entry->bytenr;
2400
2401 if (alt)
2402 n = n->rb_left;
2403 else
2404 n = n->rb_right;
2405
2406 alt = 1 - alt;
2407 }
2408 return middle;
2409}
2410#endif
2411
Arne Jansenbed92ea2012-06-28 18:03:02 +02002412int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2413 struct btrfs_fs_info *fs_info)
2414{
2415 struct qgroup_update *qgroup_update;
2416 int ret = 0;
2417
2418 if (list_empty(&trans->qgroup_ref_list) !=
2419 !trans->delayed_ref_elem.seq) {
2420 /* list without seq or seq without list */
2421 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2422 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2423 trans->delayed_ref_elem.seq);
2424 BUG();
2425 }
2426
2427 if (!trans->delayed_ref_elem.seq)
2428 return 0;
2429
2430 while (!list_empty(&trans->qgroup_ref_list)) {
2431 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2432 struct qgroup_update, list);
2433 list_del(&qgroup_update->list);
2434 if (!ret)
2435 ret = btrfs_qgroup_account_ref(
2436 trans, fs_info, qgroup_update->node,
2437 qgroup_update->extent_op);
2438 kfree(qgroup_update);
2439 }
2440
2441 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2442
2443 return ret;
2444}
2445
Chris Masonbb721702013-01-29 18:44:12 -05002446static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2447 int count)
2448{
2449 int val = atomic_read(&delayed_refs->ref_seq);
2450
2451 if (val < seq || val >= seq + count)
2452 return 1;
2453 return 0;
2454}
2455
Chris Masonc3e69d52009-03-13 10:17:05 -04002456/*
2457 * this starts processing the delayed reference count updates and
2458 * extent insertions we have queued up so far. count can be
2459 * 0, which means to process everything in the tree at the start
2460 * of the run (but not newly added entries), or it can be some target
2461 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002462 *
2463 * Returns 0 on success or if called with an aborted transaction
2464 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002465 */
2466int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2467 struct btrfs_root *root, unsigned long count)
2468{
2469 struct rb_node *node;
2470 struct btrfs_delayed_ref_root *delayed_refs;
2471 struct btrfs_delayed_ref_node *ref;
2472 struct list_head cluster;
2473 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002474 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002475 int run_all = count == (unsigned long)-1;
2476 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002477 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002478
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002479 /* We'll clean this up in btrfs_cleanup_transaction */
2480 if (trans->aborted)
2481 return 0;
2482
Chris Masonc3e69d52009-03-13 10:17:05 -04002483 if (root == root->fs_info->extent_root)
2484 root = root->fs_info->tree_root;
2485
Jan Schmidtedf39272012-06-28 18:04:55 +02002486 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2487
Chris Masonc3e69d52009-03-13 10:17:05 -04002488 delayed_refs = &trans->transaction->delayed_refs;
2489 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002490 if (count == 0) {
2491 count = delayed_refs->num_entries * 2;
2492 run_most = 1;
2493 }
2494
2495 if (!run_all && !run_most) {
2496 int old;
2497 int seq = atomic_read(&delayed_refs->ref_seq);
2498
2499progress:
2500 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2501 if (old) {
2502 DEFINE_WAIT(__wait);
2503 if (delayed_refs->num_entries < 16348)
2504 return 0;
2505
2506 prepare_to_wait(&delayed_refs->wait, &__wait,
2507 TASK_UNINTERRUPTIBLE);
2508
2509 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2510 if (old) {
2511 schedule();
2512 finish_wait(&delayed_refs->wait, &__wait);
2513
2514 if (!refs_newer(delayed_refs, seq, 256))
2515 goto progress;
2516 else
2517 return 0;
2518 } else {
2519 finish_wait(&delayed_refs->wait, &__wait);
2520 goto again;
2521 }
2522 }
2523
2524 } else {
2525 atomic_inc(&delayed_refs->procs_running_refs);
2526 }
2527
Chris Masonc3e69d52009-03-13 10:17:05 -04002528again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002529 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002530 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002531
Arne Jansen709c0482011-09-12 12:22:57 +02002532#ifdef SCRAMBLE_DELAYED_REFS
2533 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2534#endif
2535
Chris Masonc3e69d52009-03-13 10:17:05 -04002536 while (1) {
2537 if (!(run_all || run_most) &&
2538 delayed_refs->num_heads_ready < 64)
2539 break;
2540
2541 /*
2542 * go find something we can process in the rbtree. We start at
2543 * the beginning of the tree, and then build a cluster
2544 * of refs to process starting at the first one we are able to
2545 * lock
2546 */
Jan Schmidta1686502011-12-12 16:10:07 +01002547 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002548 ret = btrfs_find_ref_cluster(trans, &cluster,
2549 delayed_refs->run_delayed_start);
2550 if (ret)
2551 break;
2552
2553 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002554 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002555 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002556 spin_unlock(&delayed_refs->lock);
2557 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002558 atomic_dec(&delayed_refs->procs_running_refs);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002559 return ret;
2560 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002561
Chris Masonbb721702013-01-29 18:44:12 -05002562 atomic_add(ret, &delayed_refs->ref_seq);
2563
Chris Masonc3e69d52009-03-13 10:17:05 -04002564 count -= min_t(unsigned long, ret, count);
2565
2566 if (count == 0)
2567 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002568
Arne Jansen1fa11e22012-08-06 14:18:51 -06002569 if (delayed_start >= delayed_refs->run_delayed_start) {
2570 if (loops == 0) {
2571 /*
2572 * btrfs_find_ref_cluster looped. let's do one
2573 * more cycle. if we don't run any delayed ref
2574 * during that cycle (because we can't because
2575 * all of them are blocked), bail out.
2576 */
2577 loops = 1;
2578 } else {
2579 /*
2580 * no runnable refs left, stop trying
2581 */
2582 BUG_ON(run_all);
2583 break;
2584 }
2585 }
2586 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002587 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002588 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002589 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002590 }
2591
Chris Mason56bec292009-03-13 10:10:06 -04002592 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002593 if (!list_empty(&trans->new_bgs)) {
2594 spin_unlock(&delayed_refs->lock);
2595 btrfs_create_pending_block_groups(trans, root);
2596 spin_lock(&delayed_refs->lock);
2597 }
2598
Chris Mason56bec292009-03-13 10:10:06 -04002599 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002600 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002601 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002602 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002603
2604 while (node) {
2605 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2606 rb_node);
2607 if (btrfs_delayed_ref_is_head(ref)) {
2608 struct btrfs_delayed_ref_head *head;
2609
2610 head = btrfs_delayed_node_to_head(ref);
2611 atomic_inc(&ref->refs);
2612
2613 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002614 /*
2615 * Mutex was contended, block until it's
2616 * released and try again
2617 */
Chris Mason56bec292009-03-13 10:10:06 -04002618 mutex_lock(&head->mutex);
2619 mutex_unlock(&head->mutex);
2620
2621 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002622 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002623 goto again;
2624 }
2625 node = rb_next(node);
2626 }
2627 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002628 schedule_timeout(1);
2629 goto again;
2630 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002631out:
Chris Masonbb721702013-01-29 18:44:12 -05002632 atomic_dec(&delayed_refs->procs_running_refs);
2633 smp_mb();
2634 if (waitqueue_active(&delayed_refs->wait))
2635 wake_up(&delayed_refs->wait);
2636
Chris Masonc3e69d52009-03-13 10:17:05 -04002637 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002638 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002639 return 0;
2640}
2641
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002642int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *root,
2644 u64 bytenr, u64 num_bytes, u64 flags,
2645 int is_data)
2646{
2647 struct btrfs_delayed_extent_op *extent_op;
2648 int ret;
2649
Miao Xie78a61842012-11-21 02:21:28 +00002650 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002651 if (!extent_op)
2652 return -ENOMEM;
2653
2654 extent_op->flags_to_set = flags;
2655 extent_op->update_flags = 1;
2656 extent_op->update_key = 0;
2657 extent_op->is_data = is_data ? 1 : 0;
2658
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002659 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2660 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002661 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002662 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002663 return ret;
2664}
2665
2666static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2667 struct btrfs_root *root,
2668 struct btrfs_path *path,
2669 u64 objectid, u64 offset, u64 bytenr)
2670{
2671 struct btrfs_delayed_ref_head *head;
2672 struct btrfs_delayed_ref_node *ref;
2673 struct btrfs_delayed_data_ref *data_ref;
2674 struct btrfs_delayed_ref_root *delayed_refs;
2675 struct rb_node *node;
2676 int ret = 0;
2677
2678 ret = -ENOENT;
2679 delayed_refs = &trans->transaction->delayed_refs;
2680 spin_lock(&delayed_refs->lock);
2681 head = btrfs_find_delayed_ref_head(trans, bytenr);
2682 if (!head)
2683 goto out;
2684
2685 if (!mutex_trylock(&head->mutex)) {
2686 atomic_inc(&head->node.refs);
2687 spin_unlock(&delayed_refs->lock);
2688
David Sterbab3b4aa72011-04-21 01:20:15 +02002689 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002690
David Sterba8cc33e52011-05-02 15:29:25 +02002691 /*
2692 * Mutex was contended, block until it's released and let
2693 * caller try again
2694 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002695 mutex_lock(&head->mutex);
2696 mutex_unlock(&head->mutex);
2697 btrfs_put_delayed_ref(&head->node);
2698 return -EAGAIN;
2699 }
2700
2701 node = rb_prev(&head->node.rb_node);
2702 if (!node)
2703 goto out_unlock;
2704
2705 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2706
2707 if (ref->bytenr != bytenr)
2708 goto out_unlock;
2709
2710 ret = 1;
2711 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2712 goto out_unlock;
2713
2714 data_ref = btrfs_delayed_node_to_data_ref(ref);
2715
2716 node = rb_prev(node);
2717 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002718 int seq = ref->seq;
2719
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002720 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002721 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002722 goto out_unlock;
2723 }
2724
2725 if (data_ref->root != root->root_key.objectid ||
2726 data_ref->objectid != objectid || data_ref->offset != offset)
2727 goto out_unlock;
2728
2729 ret = 0;
2730out_unlock:
2731 mutex_unlock(&head->mutex);
2732out:
2733 spin_unlock(&delayed_refs->lock);
2734 return ret;
2735}
2736
2737static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2738 struct btrfs_root *root,
2739 struct btrfs_path *path,
2740 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002741{
2742 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002743 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002744 struct btrfs_extent_data_ref *ref;
2745 struct btrfs_extent_inline_ref *iref;
2746 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002747 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002748 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002749 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002750
Chris Masonbe20aa92007-12-17 20:14:01 -05002751 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002752 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002753 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002754
Chris Masonbe20aa92007-12-17 20:14:01 -05002755 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2756 if (ret < 0)
2757 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002758 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002759
2760 ret = -ENOENT;
2761 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002762 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002763
Zheng Yan31840ae2008-09-23 13:14:14 -04002764 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002765 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002766 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002767
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002768 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002769 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002770
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002771 ret = 1;
2772 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2773#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2774 if (item_size < sizeof(*ei)) {
2775 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2776 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002777 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002778#endif
2779 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2780
2781 if (item_size != sizeof(*ei) +
2782 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2783 goto out;
2784
2785 if (btrfs_extent_generation(leaf, ei) <=
2786 btrfs_root_last_snapshot(&root->root_item))
2787 goto out;
2788
2789 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2790 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2791 BTRFS_EXTENT_DATA_REF_KEY)
2792 goto out;
2793
2794 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2795 if (btrfs_extent_refs(leaf, ei) !=
2796 btrfs_extent_data_ref_count(leaf, ref) ||
2797 btrfs_extent_data_ref_root(leaf, ref) !=
2798 root->root_key.objectid ||
2799 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2800 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2801 goto out;
2802
Yan Zhengf321e492008-07-30 09:26:11 -04002803 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002804out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002805 return ret;
2806}
2807
2808int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2809 struct btrfs_root *root,
2810 u64 objectid, u64 offset, u64 bytenr)
2811{
2812 struct btrfs_path *path;
2813 int ret;
2814 int ret2;
2815
2816 path = btrfs_alloc_path();
2817 if (!path)
2818 return -ENOENT;
2819
2820 do {
2821 ret = check_committed_ref(trans, root, path, objectid,
2822 offset, bytenr);
2823 if (ret && ret != -ENOENT)
2824 goto out;
2825
2826 ret2 = check_delayed_ref(trans, root, path, objectid,
2827 offset, bytenr);
2828 } while (ret2 == -EAGAIN);
2829
2830 if (ret2 && ret2 != -ENOENT) {
2831 ret = ret2;
2832 goto out;
2833 }
2834
2835 if (ret != -ENOENT || ret2 != -ENOENT)
2836 ret = 0;
2837out:
Yan Zhengf321e492008-07-30 09:26:11 -04002838 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002839 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2840 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002841 return ret;
2842}
2843
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002844static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002845 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002846 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002847 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002848{
2849 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002850 u64 num_bytes;
2851 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002852 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002853 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002854 struct btrfs_key key;
2855 struct btrfs_file_extent_item *fi;
2856 int i;
2857 int level;
2858 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002859 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002860 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002861
2862 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002863 nritems = btrfs_header_nritems(buf);
2864 level = btrfs_header_level(buf);
2865
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002866 if (!root->ref_cows && level == 0)
2867 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002868
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002869 if (inc)
2870 process_func = btrfs_inc_extent_ref;
2871 else
2872 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002873
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002874 if (full_backref)
2875 parent = buf->start;
2876 else
2877 parent = 0;
2878
Zheng Yan31840ae2008-09-23 13:14:14 -04002879 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002880 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002881 btrfs_item_key_to_cpu(buf, &key, i);
2882 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002883 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002884 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002885 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002886 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002887 BTRFS_FILE_EXTENT_INLINE)
2888 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002889 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2890 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002891 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002893 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2894 key.offset -= btrfs_file_extent_offset(buf, fi);
2895 ret = process_func(trans, root, bytenr, num_bytes,
2896 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002897 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002898 if (ret)
2899 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002900 } else {
2901 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002902 num_bytes = btrfs_level_size(root, level - 1);
2903 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002904 parent, ref_root, level - 1, 0,
2905 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002906 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002907 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002908 }
2909 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002910 return 0;
2911fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002912 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002913}
2914
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002915int btrfs_inc_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)
Zheng Yan31840ae2008-09-23 13:14:14 -04002917{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002918 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002919}
Zheng Yan31840ae2008-09-23 13:14:14 -04002920
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002921int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002922 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002923{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002924 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002925}
2926
Chris Mason9078a3e2007-04-26 16:46:15 -04002927static int write_one_cache_group(struct btrfs_trans_handle *trans,
2928 struct btrfs_root *root,
2929 struct btrfs_path *path,
2930 struct btrfs_block_group_cache *cache)
2931{
2932 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002933 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002934 unsigned long bi;
2935 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002936
Chris Mason9078a3e2007-04-26 16:46:15 -04002937 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002938 if (ret < 0)
2939 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002940 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04002941
2942 leaf = path->nodes[0];
2943 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2944 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2945 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002946 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002947fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002948 if (ret) {
2949 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04002950 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002951 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002952 return 0;
2953
2954}
2955
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002956static struct btrfs_block_group_cache *
2957next_block_group(struct btrfs_root *root,
2958 struct btrfs_block_group_cache *cache)
2959{
2960 struct rb_node *node;
2961 spin_lock(&root->fs_info->block_group_cache_lock);
2962 node = rb_next(&cache->cache_node);
2963 btrfs_put_block_group(cache);
2964 if (node) {
2965 cache = rb_entry(node, struct btrfs_block_group_cache,
2966 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002967 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002968 } else
2969 cache = NULL;
2970 spin_unlock(&root->fs_info->block_group_cache_lock);
2971 return cache;
2972}
2973
Josef Bacik0af3d002010-06-21 14:48:16 -04002974static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2975 struct btrfs_trans_handle *trans,
2976 struct btrfs_path *path)
2977{
2978 struct btrfs_root *root = block_group->fs_info->tree_root;
2979 struct inode *inode = NULL;
2980 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002981 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002982 int num_pages = 0;
2983 int retries = 0;
2984 int ret = 0;
2985
2986 /*
2987 * If this block group is smaller than 100 megs don't bother caching the
2988 * block group.
2989 */
2990 if (block_group->key.offset < (100 * 1024 * 1024)) {
2991 spin_lock(&block_group->lock);
2992 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2993 spin_unlock(&block_group->lock);
2994 return 0;
2995 }
2996
2997again:
2998 inode = lookup_free_space_inode(root, block_group, path);
2999 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3000 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003001 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003002 goto out;
3003 }
3004
3005 if (IS_ERR(inode)) {
3006 BUG_ON(retries);
3007 retries++;
3008
3009 if (block_group->ro)
3010 goto out_free;
3011
3012 ret = create_free_space_inode(root, trans, block_group, path);
3013 if (ret)
3014 goto out_free;
3015 goto again;
3016 }
3017
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003018 /* We've already setup this transaction, go ahead and exit */
3019 if (block_group->cache_generation == trans->transid &&
3020 i_size_read(inode)) {
3021 dcs = BTRFS_DC_SETUP;
3022 goto out_put;
3023 }
3024
Josef Bacik0af3d002010-06-21 14:48:16 -04003025 /*
3026 * We want to set the generation to 0, that way if anything goes wrong
3027 * from here on out we know not to trust this cache when we load up next
3028 * time.
3029 */
3030 BTRFS_I(inode)->generation = 0;
3031 ret = btrfs_update_inode(trans, root, inode);
3032 WARN_ON(ret);
3033
3034 if (i_size_read(inode) > 0) {
3035 ret = btrfs_truncate_free_space_cache(root, trans, path,
3036 inode);
3037 if (ret)
3038 goto out_put;
3039 }
3040
3041 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003042 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3043 !btrfs_test_opt(root, SPACE_CACHE)) {
3044 /*
3045 * don't bother trying to write stuff out _if_
3046 * a) we're not cached,
3047 * b) we're with nospace_cache mount option.
3048 */
Josef Bacik2b209822010-12-03 13:17:53 -05003049 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003050 spin_unlock(&block_group->lock);
3051 goto out_put;
3052 }
3053 spin_unlock(&block_group->lock);
3054
Josef Bacik6fc823b2012-08-06 13:46:38 -06003055 /*
3056 * Try to preallocate enough space based on how big the block group is.
3057 * Keep in mind this has to include any pinned space which could end up
3058 * taking up quite a bit since it's not folded into the other space
3059 * cache.
3060 */
3061 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003062 if (!num_pages)
3063 num_pages = 1;
3064
Josef Bacik0af3d002010-06-21 14:48:16 -04003065 num_pages *= 16;
3066 num_pages *= PAGE_CACHE_SIZE;
3067
3068 ret = btrfs_check_data_free_space(inode, num_pages);
3069 if (ret)
3070 goto out_put;
3071
3072 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3073 num_pages, num_pages,
3074 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003075 if (!ret)
3076 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003077 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003078
Josef Bacik0af3d002010-06-21 14:48:16 -04003079out_put:
3080 iput(inode);
3081out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003082 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003083out:
3084 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003085 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003086 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003087 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003088 spin_unlock(&block_group->lock);
3089
3090 return ret;
3091}
3092
Chris Mason96b51792007-10-15 16:15:19 -04003093int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3094 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003095{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003096 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003097 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003098 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003099 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003100
3101 path = btrfs_alloc_path();
3102 if (!path)
3103 return -ENOMEM;
3104
Josef Bacik0af3d002010-06-21 14:48:16 -04003105again:
3106 while (1) {
3107 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3108 while (cache) {
3109 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3110 break;
3111 cache = next_block_group(root, cache);
3112 }
3113 if (!cache) {
3114 if (last == 0)
3115 break;
3116 last = 0;
3117 continue;
3118 }
3119 err = cache_save_setup(cache, trans, path);
3120 last = cache->key.objectid + cache->key.offset;
3121 btrfs_put_block_group(cache);
3122 }
3123
Chris Masond3977122009-01-05 21:25:51 -05003124 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003125 if (last == 0) {
3126 err = btrfs_run_delayed_refs(trans, root,
3127 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003128 if (err) /* File system offline */
3129 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003130 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003131
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003132 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3133 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003134 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3135 btrfs_put_block_group(cache);
3136 goto again;
3137 }
3138
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003139 if (cache->dirty)
3140 break;
3141 cache = next_block_group(root, cache);
3142 }
3143 if (!cache) {
3144 if (last == 0)
3145 break;
3146 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003147 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003148 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003149
Josef Bacik0cb59c92010-07-02 12:14:14 -04003150 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3151 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003152 cache->dirty = 0;
3153 last = cache->key.objectid + cache->key.offset;
3154
3155 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003156 if (err) /* File system offline */
3157 goto out;
3158
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003159 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003160 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003161
Josef Bacik0cb59c92010-07-02 12:14:14 -04003162 while (1) {
3163 /*
3164 * I don't think this is needed since we're just marking our
3165 * preallocated extent as written, but just in case it can't
3166 * hurt.
3167 */
3168 if (last == 0) {
3169 err = btrfs_run_delayed_refs(trans, root,
3170 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003171 if (err) /* File system offline */
3172 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003173 }
3174
3175 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3176 while (cache) {
3177 /*
3178 * Really this shouldn't happen, but it could if we
3179 * couldn't write the entire preallocated extent and
3180 * splitting the extent resulted in a new block.
3181 */
3182 if (cache->dirty) {
3183 btrfs_put_block_group(cache);
3184 goto again;
3185 }
3186 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3187 break;
3188 cache = next_block_group(root, cache);
3189 }
3190 if (!cache) {
3191 if (last == 0)
3192 break;
3193 last = 0;
3194 continue;
3195 }
3196
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003197 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003198
3199 /*
3200 * If we didn't have an error then the cache state is still
3201 * NEED_WRITE, so we can set it to WRITTEN.
3202 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003203 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003204 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3205 last = cache->key.objectid + cache->key.offset;
3206 btrfs_put_block_group(cache);
3207 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003208out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003209
Chris Mason9078a3e2007-04-26 16:46:15 -04003210 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003211 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003212}
3213
Yan Zhengd2fb3432008-12-11 16:30:39 -05003214int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3215{
3216 struct btrfs_block_group_cache *block_group;
3217 int readonly = 0;
3218
3219 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3220 if (!block_group || block_group->ro)
3221 readonly = 1;
3222 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003223 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003224 return readonly;
3225}
3226
Chris Mason593060d2008-03-25 16:50:33 -04003227static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3228 u64 total_bytes, u64 bytes_used,
3229 struct btrfs_space_info **space_info)
3230{
3231 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003232 int i;
3233 int factor;
3234
3235 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3236 BTRFS_BLOCK_GROUP_RAID10))
3237 factor = 2;
3238 else
3239 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003240
3241 found = __find_space_info(info, flags);
3242 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003243 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003244 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003245 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003246 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003247 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003248 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003249 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003250 *space_info = found;
3251 return 0;
3252 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003253 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003254 if (!found)
3255 return -ENOMEM;
3256
Yan, Zhengb742bb82010-05-16 10:46:24 -04003257 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3258 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003259 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003260 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003261 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003262 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003263 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003264 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003265 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003266 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003267 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003268 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003269 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003270 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003271 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003272 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003273 found->flush = 0;
3274 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003275 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003276 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003277 if (flags & BTRFS_BLOCK_GROUP_DATA)
3278 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003279 return 0;
3280}
3281
Chris Mason8790d502008-04-03 16:29:03 -04003282static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3283{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003284 u64 extra_flags = chunk_to_extended(flags) &
3285 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003286
Miao Xiede98ced2013-01-29 10:13:12 +00003287 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003288 if (flags & BTRFS_BLOCK_GROUP_DATA)
3289 fs_info->avail_data_alloc_bits |= extra_flags;
3290 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3291 fs_info->avail_metadata_alloc_bits |= extra_flags;
3292 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3293 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003294 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003295}
Chris Mason593060d2008-03-25 16:50:33 -04003296
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003297/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003298 * returns target flags in extended format or 0 if restripe for this
3299 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003300 *
3301 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003302 */
3303static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3304{
3305 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3306 u64 target = 0;
3307
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003308 if (!bctl)
3309 return 0;
3310
3311 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3312 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3313 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3314 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3315 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3316 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3317 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3318 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3319 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3320 }
3321
3322 return target;
3323}
3324
3325/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003326 * @flags: available profiles in extended format (see ctree.h)
3327 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003328 * Returns reduced profile in chunk format. If profile changing is in
3329 * progress (either running or paused) picks the target profile (if it's
3330 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003331 */
Yan Zheng2b820322008-11-17 21:11:30 -05003332u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003333{
Chris Masoncd02dca2010-12-13 14:56:23 -05003334 /*
3335 * we add in the count of missing devices because we want
3336 * to make sure that any RAID levels on a degraded FS
3337 * continue to be honored.
3338 */
3339 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3340 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003341 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003342 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003343
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003344 /*
3345 * see if restripe for this chunk_type is in progress, if so
3346 * try to reduce to the target profile
3347 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003348 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003349 target = get_restripe_target(root->fs_info, flags);
3350 if (target) {
3351 /* pick target profile only if it's already available */
3352 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003353 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003354 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003355 }
3356 }
3357 spin_unlock(&root->fs_info->balance_lock);
3358
David Woodhouse53b381b2013-01-29 18:40:14 -05003359 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003360 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003361 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3362 BTRFS_BLOCK_GROUP_RAID5);
3363 if (num_devices < 3)
3364 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003365 if (num_devices < 4)
3366 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3367
David Woodhouse53b381b2013-01-29 18:40:14 -05003368 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3369 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3370 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3371 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003372
David Woodhouse53b381b2013-01-29 18:40:14 -05003373 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3374 tmp = BTRFS_BLOCK_GROUP_RAID6;
3375 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3376 tmp = BTRFS_BLOCK_GROUP_RAID5;
3377 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3378 tmp = BTRFS_BLOCK_GROUP_RAID10;
3379 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3380 tmp = BTRFS_BLOCK_GROUP_RAID1;
3381 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3382 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003383
David Woodhouse53b381b2013-01-29 18:40:14 -05003384 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003385}
3386
Yan, Zhengb742bb82010-05-16 10:46:24 -04003387static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003388{
Miao Xiede98ced2013-01-29 10:13:12 +00003389 unsigned seq;
3390
3391 do {
3392 seq = read_seqbegin(&root->fs_info->profiles_lock);
3393
3394 if (flags & BTRFS_BLOCK_GROUP_DATA)
3395 flags |= root->fs_info->avail_data_alloc_bits;
3396 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3397 flags |= root->fs_info->avail_system_alloc_bits;
3398 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3399 flags |= root->fs_info->avail_metadata_alloc_bits;
3400 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003401
Yan, Zhengb742bb82010-05-16 10:46:24 -04003402 return btrfs_reduce_alloc_profile(root, flags);
3403}
Josef Bacik6a632092009-02-20 11:00:09 -05003404
Miao Xie6d07bce2011-01-05 10:07:31 +00003405u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003406{
3407 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003408 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003409
Yan, Zhengb742bb82010-05-16 10:46:24 -04003410 if (data)
3411 flags = BTRFS_BLOCK_GROUP_DATA;
3412 else if (root == root->fs_info->chunk_root)
3413 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3414 else
3415 flags = BTRFS_BLOCK_GROUP_METADATA;
3416
David Woodhouse53b381b2013-01-29 18:40:14 -05003417 ret = get_alloc_profile(root, flags);
3418 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003419}
3420
Josef Bacik6a632092009-02-20 11:00:09 -05003421/*
3422 * This will check the space that the inode allocates from to make sure we have
3423 * enough space for bytes.
3424 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003425int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003426{
3427 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003428 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003429 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003430 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003431 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003432
3433 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003434 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003435
Li Zefan82d59022011-04-20 10:33:24 +08003436 if (root == root->fs_info->tree_root ||
3437 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003438 alloc_chunk = 0;
3439 committed = 1;
3440 }
3441
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003442 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003443 if (!data_sinfo)
3444 goto alloc;
3445
Josef Bacik6a632092009-02-20 11:00:09 -05003446again:
3447 /* make sure we have enough space to handle the data first */
3448 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003449 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3450 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3451 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003452
3453 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003454 struct btrfs_trans_handle *trans;
3455
Josef Bacik6a632092009-02-20 11:00:09 -05003456 /*
3457 * if we don't have enough free bytes in this space then we need
3458 * to alloc a new chunk.
3459 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003460 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003461 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003462
Chris Mason0e4f8f82011-04-15 16:05:44 -04003463 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003464 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003465alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003466 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003467 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003468 if (IS_ERR(trans))
3469 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003470
3471 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003472 alloc_target,
3473 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003474 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003475 if (ret < 0) {
3476 if (ret != -ENOSPC)
3477 return ret;
3478 else
3479 goto commit_trans;
3480 }
Chris Mason33b4d472009-09-22 14:45:50 -04003481
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003482 if (!data_sinfo)
3483 data_sinfo = fs_info->data_sinfo;
3484
Josef Bacik6a632092009-02-20 11:00:09 -05003485 goto again;
3486 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003487
3488 /*
3489 * If we have less pinned bytes than we want to allocate then
3490 * don't bother committing the transaction, it won't help us.
3491 */
3492 if (data_sinfo->bytes_pinned < bytes)
3493 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003494 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003495
3496 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003497commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003498 if (!committed &&
3499 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003500 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003501 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003502 if (IS_ERR(trans))
3503 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003504 ret = btrfs_commit_transaction(trans, root);
3505 if (ret)
3506 return ret;
3507 goto again;
3508 }
3509
Josef Bacik6a632092009-02-20 11:00:09 -05003510 return -ENOSPC;
3511 }
3512 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003513 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003514 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003515 spin_unlock(&data_sinfo->lock);
3516
Josef Bacik9ed74f22009-09-11 16:12:44 -04003517 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003518}
3519
3520/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003521 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003522 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003523void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003524{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003525 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003526 struct btrfs_space_info *data_sinfo;
3527
3528 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003529 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003530
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003531 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003532 spin_lock(&data_sinfo->lock);
3533 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003534 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003535 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003536 spin_unlock(&data_sinfo->lock);
3537}
3538
Josef Bacik97e728d2009-04-21 17:40:57 -04003539static void force_metadata_allocation(struct btrfs_fs_info *info)
3540{
3541 struct list_head *head = &info->space_info;
3542 struct btrfs_space_info *found;
3543
3544 rcu_read_lock();
3545 list_for_each_entry_rcu(found, head, list) {
3546 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003547 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003548 }
3549 rcu_read_unlock();
3550}
3551
Chris Masone5bc2452010-10-26 13:37:56 -04003552static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003553 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003554{
Josef Bacikfb25e912011-07-26 17:00:46 -04003555 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003556 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003557 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003558 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003559
Chris Mason0e4f8f82011-04-15 16:05:44 -04003560 if (force == CHUNK_ALLOC_FORCE)
3561 return 1;
3562
3563 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003564 * We need to take into account the global rsv because for all intents
3565 * and purposes it's used space. Don't worry about locking the
3566 * global_rsv, it doesn't change except when the transaction commits.
3567 */
Josef Bacik54338b52012-08-14 16:20:52 -04003568 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3569 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003570
3571 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003572 * in limited mode, we want to have some free space up to
3573 * about 1% of the FS size.
3574 */
3575 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003576 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003577 thresh = max_t(u64, 64 * 1024 * 1024,
3578 div_factor_fine(thresh, 1));
3579
3580 if (num_bytes - num_allocated < thresh)
3581 return 1;
3582 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003583
Josef Bacik698d0082012-09-12 14:08:47 -04003584 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003585 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003586 return 1;
3587}
3588
Liu Bo15d1ff82012-03-29 09:57:44 -04003589static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3590{
3591 u64 num_dev;
3592
David Woodhouse53b381b2013-01-29 18:40:14 -05003593 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3594 BTRFS_BLOCK_GROUP_RAID0 |
3595 BTRFS_BLOCK_GROUP_RAID5 |
3596 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003597 num_dev = root->fs_info->fs_devices->rw_devices;
3598 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3599 num_dev = 2;
3600 else
3601 num_dev = 1; /* DUP or single */
3602
3603 /* metadata for updaing devices and chunk tree */
3604 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3605}
3606
3607static void check_system_chunk(struct btrfs_trans_handle *trans,
3608 struct btrfs_root *root, u64 type)
3609{
3610 struct btrfs_space_info *info;
3611 u64 left;
3612 u64 thresh;
3613
3614 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3615 spin_lock(&info->lock);
3616 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3617 info->bytes_reserved - info->bytes_readonly;
3618 spin_unlock(&info->lock);
3619
3620 thresh = get_system_chunk_thresh(root, type);
3621 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3622 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3623 left, thresh, type);
3624 dump_space_info(info, 0, 0);
3625 }
3626
3627 if (left < thresh) {
3628 u64 flags;
3629
3630 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3631 btrfs_alloc_chunk(trans, root, flags);
3632 }
3633}
3634
Chris Mason6324fbf2008-03-24 15:01:59 -04003635static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003636 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003637{
3638 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003639 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003640 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003641 int ret = 0;
3642
Josef Bacikc6b305a2012-12-18 09:16:16 -05003643 /* Don't re-enter if we're already allocating a chunk */
3644 if (trans->allocating_chunk)
3645 return -ENOSPC;
3646
Chris Mason6324fbf2008-03-24 15:01:59 -04003647 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003648 if (!space_info) {
3649 ret = update_space_info(extent_root->fs_info, flags,
3650 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003651 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003652 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003653 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003654
Josef Bacik6d741192011-04-11 20:20:11 -04003655again:
Josef Bacik25179202008-10-29 14:49:05 -04003656 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003657 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003658 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003659 if (space_info->full) {
3660 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003661 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003662 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003663
Josef Bacik698d0082012-09-12 14:08:47 -04003664 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003665 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003666 return 0;
3667 } else if (space_info->chunk_alloc) {
3668 wait_for_alloc = 1;
3669 } else {
3670 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003671 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003672
Josef Bacik25179202008-10-29 14:49:05 -04003673 spin_unlock(&space_info->lock);
3674
Josef Bacik6d741192011-04-11 20:20:11 -04003675 mutex_lock(&fs_info->chunk_mutex);
3676
3677 /*
3678 * The chunk_mutex is held throughout the entirety of a chunk
3679 * allocation, so once we've acquired the chunk_mutex we know that the
3680 * other guy is done and we need to recheck and see if we should
3681 * allocate.
3682 */
3683 if (wait_for_alloc) {
3684 mutex_unlock(&fs_info->chunk_mutex);
3685 wait_for_alloc = 0;
3686 goto again;
3687 }
3688
Josef Bacikc6b305a2012-12-18 09:16:16 -05003689 trans->allocating_chunk = true;
3690
Josef Bacik97e728d2009-04-21 17:40:57 -04003691 /*
Josef Bacik67377732010-09-16 16:19:09 -04003692 * If we have mixed data/metadata chunks we want to make sure we keep
3693 * allocating mixed chunks instead of individual chunks.
3694 */
3695 if (btrfs_mixed_space_info(space_info))
3696 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3697
3698 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003699 * if we're doing a data chunk, go ahead and make sure that
3700 * we keep a reasonable number of metadata chunks allocated in the
3701 * FS as well.
3702 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003703 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003704 fs_info->data_chunk_allocations++;
3705 if (!(fs_info->data_chunk_allocations %
3706 fs_info->metadata_ratio))
3707 force_metadata_allocation(fs_info);
3708 }
3709
Liu Bo15d1ff82012-03-29 09:57:44 -04003710 /*
3711 * Check if we have enough space in SYSTEM chunk because we may need
3712 * to update devices.
3713 */
3714 check_system_chunk(trans, extent_root, flags);
3715
Yan Zheng2b820322008-11-17 21:11:30 -05003716 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003717 trans->allocating_chunk = false;
Mark Fasheh92b8e892011-07-12 10:57:59 -07003718
Josef Bacik9ed74f22009-09-11 16:12:44 -04003719 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003720 if (ret < 0 && ret != -ENOSPC)
3721 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003722 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003723 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003724 else
3725 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003726
Chris Mason0e4f8f82011-04-15 16:05:44 -04003727 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003728out:
Josef Bacik6d741192011-04-11 20:20:11 -04003729 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003730 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003731 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003732 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003733}
3734
Josef Bacika80c8dc2012-09-06 16:59:33 -04003735static int can_overcommit(struct btrfs_root *root,
3736 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003737 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003738{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003739 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003740 u64 profile = btrfs_get_alloc_profile(root, 0);
Josef Bacik96f1bb52013-01-30 17:02:51 -05003741 u64 rsv_size = 0;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003742 u64 avail;
3743 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003744 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003745
3746 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003747 space_info->bytes_pinned + space_info->bytes_readonly;
3748
3749 spin_lock(&global_rsv->lock);
3750 rsv_size = global_rsv->size;
3751 spin_unlock(&global_rsv->lock);
3752
3753 /*
3754 * We only want to allow over committing if we have lots of actual space
3755 * free, but if we don't have enough space to handle the global reserve
3756 * space then we could end up having a real enospc problem when trying
3757 * to allocate a chunk or some other such important allocation.
3758 */
3759 rsv_size <<= 1;
3760 if (used + rsv_size >= space_info->total_bytes)
3761 return 0;
3762
3763 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003764
3765 spin_lock(&root->fs_info->free_chunk_lock);
3766 avail = root->fs_info->free_chunk_space;
3767 spin_unlock(&root->fs_info->free_chunk_lock);
3768
3769 /*
3770 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003771 * space is actually useable. For raid56, the space info used
3772 * doesn't include the parity drive, so we don't have to
3773 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003774 */
3775 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3776 BTRFS_BLOCK_GROUP_RAID1 |
3777 BTRFS_BLOCK_GROUP_RAID10))
3778 avail >>= 1;
3779
Josef Bacik70afa392013-02-06 13:53:19 -05003780 to_add = space_info->total_bytes;
3781
Josef Bacika80c8dc2012-09-06 16:59:33 -04003782 /*
Miao Xie561c2942012-10-16 11:32:18 +00003783 * If we aren't flushing all things, let us overcommit up to
3784 * 1/2th of the space. If we can flush, don't let us overcommit
3785 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003786 */
Miao Xie08e007d2012-10-16 11:33:38 +00003787 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003788 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003789 else
Josef Bacik70afa392013-02-06 13:53:19 -05003790 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003791
Josef Bacik70afa392013-02-06 13:53:19 -05003792 /*
3793 * Limit the overcommit to the amount of free space we could possibly
3794 * allocate for chunks.
3795 */
3796 to_add = min(avail, to_add);
3797
3798 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003799 return 1;
3800 return 0;
3801}
3802
Miao Xieda633a42012-12-20 11:19:09 +00003803static inline int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
3804 unsigned long nr_pages,
3805 enum wb_reason reason)
Josef Bacik1135d6d2012-12-14 13:46:43 -05003806{
Miao Xieda633a42012-12-20 11:19:09 +00003807 /* the flusher is dealing with the dirty inodes now. */
3808 if (writeback_in_progress(sb->s_bdi))
3809 return 1;
3810
3811 if (down_read_trylock(&sb->s_umount)) {
Josef Bacik1135d6d2012-12-14 13:46:43 -05003812 writeback_inodes_sb_nr(sb, nr_pages, reason);
3813 up_read(&sb->s_umount);
3814 return 1;
3815 }
3816
3817 return 0;
3818}
3819
Miao Xieda633a42012-12-20 11:19:09 +00003820void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3821 unsigned long nr_pages)
3822{
3823 struct super_block *sb = root->fs_info->sb;
3824 int started;
3825
3826 /* If we can not start writeback, just sync all the delalloc file. */
3827 started = writeback_inodes_sb_nr_if_idle_safe(sb, nr_pages,
3828 WB_REASON_FS_FREE_SPACE);
3829 if (!started) {
3830 /*
3831 * We needn't worry the filesystem going from r/w to r/o though
3832 * we don't acquire ->s_umount mutex, because the filesystem
3833 * should guarantee the delalloc inodes list be empty after
3834 * the filesystem is readonly(all dirty pages are written to
3835 * the disk).
3836 */
3837 btrfs_start_delalloc_inodes(root, 0);
3838 btrfs_wait_ordered_extents(root, 0);
3839 }
3840}
3841
Yan, Zheng5da9d012010-05-16 10:46:25 -04003842/*
3843 * shrink metadata reservation for delalloc
3844 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003845static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3846 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003847{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003848 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003849 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003850 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003851 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003852 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003853 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003854 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003855 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003856 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003857
Josef Bacik663350a2011-11-03 22:54:25 -04003858 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003859 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003860 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003861
3862 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003863 delalloc_bytes = percpu_counter_sum_positive(
3864 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003865 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003866 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003867 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003868 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003869 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003870 }
3871
Josef Bacikf4c738c2012-07-02 17:10:51 -04003872 while (delalloc_bytes && loops < 3) {
3873 max_reclaim = min(delalloc_bytes, to_reclaim);
3874 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003875 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003876 /*
3877 * We need to wait for the async pages to actually start before
3878 * we do anything.
3879 */
3880 wait_event(root->fs_info->async_submit_wait,
3881 !atomic_read(&root->fs_info->async_delalloc_pages));
3882
Miao Xie08e007d2012-10-16 11:33:38 +00003883 if (!trans)
3884 flush = BTRFS_RESERVE_FLUSH_ALL;
3885 else
3886 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003887 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003888 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003889 spin_unlock(&space_info->lock);
3890 break;
3891 }
Josef Bacik0019f102010-10-15 15:18:40 -04003892 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003893
Chris Mason36e39c42011-03-12 07:08:42 -05003894 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003895 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003896 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003897 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003898 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003899 if (time_left)
3900 break;
3901 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003902 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003903 delalloc_bytes = percpu_counter_sum_positive(
3904 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003905 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003906}
3907
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003908/**
Josef Bacik663350a2011-11-03 22:54:25 -04003909 * maybe_commit_transaction - possibly commit the transaction if its ok to
3910 * @root - the root we're allocating for
3911 * @bytes - the number of bytes we want to reserve
3912 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003913 *
Josef Bacik663350a2011-11-03 22:54:25 -04003914 * This will check to make sure that committing the transaction will actually
3915 * get us somewhere and then commit the transaction if it does. Otherwise it
3916 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003917 */
Josef Bacik663350a2011-11-03 22:54:25 -04003918static int may_commit_transaction(struct btrfs_root *root,
3919 struct btrfs_space_info *space_info,
3920 u64 bytes, int force)
3921{
3922 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3923 struct btrfs_trans_handle *trans;
3924
3925 trans = (struct btrfs_trans_handle *)current->journal_info;
3926 if (trans)
3927 return -EAGAIN;
3928
3929 if (force)
3930 goto commit;
3931
3932 /* See if there is enough pinned space to make this reservation */
3933 spin_lock(&space_info->lock);
3934 if (space_info->bytes_pinned >= bytes) {
3935 spin_unlock(&space_info->lock);
3936 goto commit;
3937 }
3938 spin_unlock(&space_info->lock);
3939
3940 /*
3941 * See if there is some space in the delayed insertion reservation for
3942 * this reservation.
3943 */
3944 if (space_info != delayed_rsv->space_info)
3945 return -ENOSPC;
3946
Liu Bod9b02182012-02-16 18:34:39 +08003947 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003948 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003949 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04003950 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003951 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003952 return -ENOSPC;
3953 }
3954 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003955 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003956
3957commit:
3958 trans = btrfs_join_transaction(root);
3959 if (IS_ERR(trans))
3960 return -ENOSPC;
3961
3962 return btrfs_commit_transaction(trans, root);
3963}
3964
Josef Bacik96c3f432012-06-21 14:05:49 -04003965enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04003966 FLUSH_DELAYED_ITEMS_NR = 1,
3967 FLUSH_DELAYED_ITEMS = 2,
3968 FLUSH_DELALLOC = 3,
3969 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04003970 ALLOC_CHUNK = 5,
3971 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04003972};
3973
3974static int flush_space(struct btrfs_root *root,
3975 struct btrfs_space_info *space_info, u64 num_bytes,
3976 u64 orig_bytes, int state)
3977{
3978 struct btrfs_trans_handle *trans;
3979 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003980 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04003981
3982 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04003983 case FLUSH_DELAYED_ITEMS_NR:
3984 case FLUSH_DELAYED_ITEMS:
3985 if (state == FLUSH_DELAYED_ITEMS_NR) {
3986 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3987
3988 nr = (int)div64_u64(num_bytes, bytes);
3989 if (!nr)
3990 nr = 1;
3991 nr *= 2;
3992 } else {
3993 nr = -1;
3994 }
3995 trans = btrfs_join_transaction(root);
3996 if (IS_ERR(trans)) {
3997 ret = PTR_ERR(trans);
3998 break;
3999 }
4000 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4001 btrfs_end_transaction(trans, root);
4002 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004003 case FLUSH_DELALLOC:
4004 case FLUSH_DELALLOC_WAIT:
4005 shrink_delalloc(root, num_bytes, orig_bytes,
4006 state == FLUSH_DELALLOC_WAIT);
4007 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004008 case ALLOC_CHUNK:
4009 trans = btrfs_join_transaction(root);
4010 if (IS_ERR(trans)) {
4011 ret = PTR_ERR(trans);
4012 break;
4013 }
4014 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004015 btrfs_get_alloc_profile(root, 0),
4016 CHUNK_ALLOC_NO_FORCE);
4017 btrfs_end_transaction(trans, root);
4018 if (ret == -ENOSPC)
4019 ret = 0;
4020 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004021 case COMMIT_TRANS:
4022 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4023 break;
4024 default:
4025 ret = -ENOSPC;
4026 break;
4027 }
4028
4029 return ret;
4030}
Josef Bacik663350a2011-11-03 22:54:25 -04004031/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004032 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4033 * @root - the root we're allocating for
4034 * @block_rsv - the block_rsv we're allocating for
4035 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004036 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004037 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004038 * This will reserve orgi_bytes number of bytes from the space info associated
4039 * with the block_rsv. If there is not enough space it will make an attempt to
4040 * flush out space to make room. It will do this by flushing delalloc if
4041 * possible or committing the transaction. If flush is 0 then no attempts to
4042 * regain reservations will be made and this will fail if there is not enough
4043 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004044 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004045static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004046 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004047 u64 orig_bytes,
4048 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004049{
4050 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004051 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004052 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004053 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004054 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004055 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004056
4057again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004058 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004059 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004060 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004061 * We only want to wait if somebody other than us is flushing and we
4062 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004063 */
Miao Xie08e007d2012-10-16 11:33:38 +00004064 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4065 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004066 spin_unlock(&space_info->lock);
4067 /*
4068 * If we have a trans handle we can't wait because the flusher
4069 * may have to commit the transaction, which would mean we would
4070 * deadlock since we are waiting for the flusher to finish, but
4071 * hold the current transaction open.
4072 */
Josef Bacik663350a2011-11-03 22:54:25 -04004073 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004074 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004075 ret = wait_event_killable(space_info->wait, !space_info->flush);
4076 /* Must have been killed, return */
4077 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004078 return -EINTR;
4079
4080 spin_lock(&space_info->lock);
4081 }
4082
4083 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004084 used = space_info->bytes_used + space_info->bytes_reserved +
4085 space_info->bytes_pinned + space_info->bytes_readonly +
4086 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004087
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004088 /*
4089 * The idea here is that we've not already over-reserved the block group
4090 * then we can go ahead and save our reservation first and then start
4091 * flushing if we need to. Otherwise if we've already overcommitted
4092 * lets start flushing stuff first and then come back and try to make
4093 * our reservation.
4094 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004095 if (used <= space_info->total_bytes) {
4096 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004097 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004098 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004099 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004100 ret = 0;
4101 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004102 /*
4103 * Ok set num_bytes to orig_bytes since we aren't
4104 * overocmmitted, this way we only try and reclaim what
4105 * we need.
4106 */
4107 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004108 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004109 } else {
4110 /*
4111 * Ok we're over committed, set num_bytes to the overcommitted
4112 * amount plus the amount of bytes that we need for this
4113 * reservation.
4114 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004115 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004116 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004117 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004118
Josef Bacik44734ed2012-09-28 16:04:19 -04004119 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4120 space_info->bytes_may_use += orig_bytes;
4121 trace_btrfs_space_reservation(root->fs_info, "space_info",
4122 space_info->flags, orig_bytes,
4123 1);
4124 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004125 }
4126
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004127 /*
4128 * Couldn't make our reservation, save our place so while we're trying
4129 * to reclaim space we can actually use it instead of somebody else
4130 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004131 *
4132 * We make the other tasks wait for the flush only when we can flush
4133 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004134 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004135 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004136 flushing = true;
4137 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004138 }
4139
Yan, Zhengf0486c62010-05-16 10:46:25 -04004140 spin_unlock(&space_info->lock);
4141
Miao Xie08e007d2012-10-16 11:33:38 +00004142 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004143 goto out;
4144
Josef Bacik96c3f432012-06-21 14:05:49 -04004145 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4146 flush_state);
4147 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004148
4149 /*
4150 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4151 * would happen. So skip delalloc flush.
4152 */
4153 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4154 (flush_state == FLUSH_DELALLOC ||
4155 flush_state == FLUSH_DELALLOC_WAIT))
4156 flush_state = ALLOC_CHUNK;
4157
Josef Bacik96c3f432012-06-21 14:05:49 -04004158 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004159 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004160 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4161 flush_state < COMMIT_TRANS)
4162 goto again;
4163 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4164 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004165 goto again;
4166
4167out:
Josef Bacik5d803662013-02-07 16:06:02 -05004168 if (ret == -ENOSPC &&
4169 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4170 struct btrfs_block_rsv *global_rsv =
4171 &root->fs_info->global_block_rsv;
4172
4173 if (block_rsv != global_rsv &&
4174 !block_rsv_use_bytes(global_rsv, orig_bytes))
4175 ret = 0;
4176 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004177 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004178 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004179 space_info->flush = 0;
4180 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004181 spin_unlock(&space_info->lock);
4182 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004183 return ret;
4184}
4185
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004186static struct btrfs_block_rsv *get_block_rsv(
4187 const struct btrfs_trans_handle *trans,
4188 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004189{
Josef Bacik4c13d752011-08-30 11:31:29 -04004190 struct btrfs_block_rsv *block_rsv = NULL;
4191
Josef Bacik0e721102012-06-26 16:13:18 -04004192 if (root->ref_cows)
4193 block_rsv = trans->block_rsv;
4194
4195 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004196 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004197
4198 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004199 block_rsv = root->block_rsv;
4200
4201 if (!block_rsv)
4202 block_rsv = &root->fs_info->empty_block_rsv;
4203
4204 return block_rsv;
4205}
4206
4207static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4208 u64 num_bytes)
4209{
4210 int ret = -ENOSPC;
4211 spin_lock(&block_rsv->lock);
4212 if (block_rsv->reserved >= num_bytes) {
4213 block_rsv->reserved -= num_bytes;
4214 if (block_rsv->reserved < block_rsv->size)
4215 block_rsv->full = 0;
4216 ret = 0;
4217 }
4218 spin_unlock(&block_rsv->lock);
4219 return ret;
4220}
4221
4222static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4223 u64 num_bytes, int update_size)
4224{
4225 spin_lock(&block_rsv->lock);
4226 block_rsv->reserved += num_bytes;
4227 if (update_size)
4228 block_rsv->size += num_bytes;
4229 else if (block_rsv->reserved >= block_rsv->size)
4230 block_rsv->full = 1;
4231 spin_unlock(&block_rsv->lock);
4232}
4233
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004234static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4235 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004236 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004237{
4238 struct btrfs_space_info *space_info = block_rsv->space_info;
4239
4240 spin_lock(&block_rsv->lock);
4241 if (num_bytes == (u64)-1)
4242 num_bytes = block_rsv->size;
4243 block_rsv->size -= num_bytes;
4244 if (block_rsv->reserved >= block_rsv->size) {
4245 num_bytes = block_rsv->reserved - block_rsv->size;
4246 block_rsv->reserved = block_rsv->size;
4247 block_rsv->full = 1;
4248 } else {
4249 num_bytes = 0;
4250 }
4251 spin_unlock(&block_rsv->lock);
4252
4253 if (num_bytes > 0) {
4254 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004255 spin_lock(&dest->lock);
4256 if (!dest->full) {
4257 u64 bytes_to_add;
4258
4259 bytes_to_add = dest->size - dest->reserved;
4260 bytes_to_add = min(num_bytes, bytes_to_add);
4261 dest->reserved += bytes_to_add;
4262 if (dest->reserved >= dest->size)
4263 dest->full = 1;
4264 num_bytes -= bytes_to_add;
4265 }
4266 spin_unlock(&dest->lock);
4267 }
4268 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004269 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004270 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004271 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004272 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004273 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004274 spin_unlock(&space_info->lock);
4275 }
4276 }
4277}
4278
4279static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4280 struct btrfs_block_rsv *dst, u64 num_bytes)
4281{
4282 int ret;
4283
4284 ret = block_rsv_use_bytes(src, num_bytes);
4285 if (ret)
4286 return ret;
4287
4288 block_rsv_add_bytes(dst, num_bytes, 1);
4289 return 0;
4290}
4291
Miao Xie66d8f3d2012-09-06 04:02:28 -06004292void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004293{
4294 memset(rsv, 0, sizeof(*rsv));
4295 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004296 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004297}
4298
Miao Xie66d8f3d2012-09-06 04:02:28 -06004299struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4300 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004301{
4302 struct btrfs_block_rsv *block_rsv;
4303 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004304
4305 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4306 if (!block_rsv)
4307 return NULL;
4308
Miao Xie66d8f3d2012-09-06 04:02:28 -06004309 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004310 block_rsv->space_info = __find_space_info(fs_info,
4311 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004312 return block_rsv;
4313}
4314
4315void btrfs_free_block_rsv(struct btrfs_root *root,
4316 struct btrfs_block_rsv *rsv)
4317{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004318 if (!rsv)
4319 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004320 btrfs_block_rsv_release(root, rsv, (u64)-1);
4321 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004322}
4323
Miao Xie08e007d2012-10-16 11:33:38 +00004324int btrfs_block_rsv_add(struct btrfs_root *root,
4325 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4326 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004327{
4328 int ret;
4329
4330 if (num_bytes == 0)
4331 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004332
Miao Xie61b520a2011-11-10 20:45:05 -05004333 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004334 if (!ret) {
4335 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4336 return 0;
4337 }
4338
Yan, Zhengf0486c62010-05-16 10:46:25 -04004339 return ret;
4340}
4341
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004342int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004343 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004344{
4345 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004346 int ret = -ENOSPC;
4347
4348 if (!block_rsv)
4349 return 0;
4350
4351 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004352 num_bytes = div_factor(block_rsv->size, min_factor);
4353 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004354 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004355 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004356
Josef Bacik36ba0222011-10-18 12:15:48 -04004357 return ret;
4358}
4359
Miao Xie08e007d2012-10-16 11:33:38 +00004360int btrfs_block_rsv_refill(struct btrfs_root *root,
4361 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4362 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004363{
4364 u64 num_bytes = 0;
4365 int ret = -ENOSPC;
4366
4367 if (!block_rsv)
4368 return 0;
4369
4370 spin_lock(&block_rsv->lock);
4371 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004372 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004373 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004374 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004375 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004376 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004377
Yan, Zhengf0486c62010-05-16 10:46:25 -04004378 if (!ret)
4379 return 0;
4380
Miao Xieaa38a712011-11-18 17:43:00 +08004381 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004382 if (!ret) {
4383 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004384 return 0;
4385 }
4386
Josef Bacik13553e52011-08-08 13:33:21 -04004387 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004388}
4389
4390int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4391 struct btrfs_block_rsv *dst_rsv,
4392 u64 num_bytes)
4393{
4394 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4395}
4396
4397void btrfs_block_rsv_release(struct btrfs_root *root,
4398 struct btrfs_block_rsv *block_rsv,
4399 u64 num_bytes)
4400{
4401 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4402 if (global_rsv->full || global_rsv == block_rsv ||
4403 block_rsv->space_info != global_rsv->space_info)
4404 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004405 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4406 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004407}
4408
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004409/*
4410 * helper to calculate size of global block reservation.
4411 * the desired value is sum of space used by extent tree,
4412 * checksum tree and root tree
4413 */
4414static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4415{
4416 struct btrfs_space_info *sinfo;
4417 u64 num_bytes;
4418 u64 meta_used;
4419 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004420 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004421
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004422 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4423 spin_lock(&sinfo->lock);
4424 data_used = sinfo->bytes_used;
4425 spin_unlock(&sinfo->lock);
4426
4427 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4428 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004429 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4430 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004431 meta_used = sinfo->bytes_used;
4432 spin_unlock(&sinfo->lock);
4433
4434 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4435 csum_size * 2;
4436 num_bytes += div64_u64(data_used + meta_used, 50);
4437
4438 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004439 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004440
4441 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4442}
4443
4444static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4445{
4446 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4447 struct btrfs_space_info *sinfo = block_rsv->space_info;
4448 u64 num_bytes;
4449
4450 num_bytes = calc_global_metadata_size(fs_info);
4451
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004452 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004453 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004454
4455 block_rsv->size = num_bytes;
4456
4457 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004458 sinfo->bytes_reserved + sinfo->bytes_readonly +
4459 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004460
4461 if (sinfo->total_bytes > num_bytes) {
4462 num_bytes = sinfo->total_bytes - num_bytes;
4463 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004464 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004465 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004466 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004467 }
4468
4469 if (block_rsv->reserved >= block_rsv->size) {
4470 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004471 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004472 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004473 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004474 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004475 block_rsv->reserved = block_rsv->size;
4476 block_rsv->full = 1;
4477 }
David Sterba182608c2011-05-05 13:13:16 +02004478
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004479 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004480 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004481}
4482
Yan, Zhengf0486c62010-05-16 10:46:25 -04004483static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4484{
4485 struct btrfs_space_info *space_info;
4486
4487 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4488 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004489
4490 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004491 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004492 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004493 fs_info->trans_block_rsv.space_info = space_info;
4494 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004495 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004496
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004497 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4498 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4499 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4500 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004501 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004502
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004503 update_global_block_rsv(fs_info);
4504}
4505
4506static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4507{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004508 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4509 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004510 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4511 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4512 WARN_ON(fs_info->trans_block_rsv.size > 0);
4513 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4514 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4515 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004516 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4517 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004518}
4519
Yan, Zhenga22285a2010-05-16 10:48:46 -04004520void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4521 struct btrfs_root *root)
4522{
Josef Bacik0e721102012-06-26 16:13:18 -04004523 if (!trans->block_rsv)
4524 return;
4525
Yan, Zhenga22285a2010-05-16 10:48:46 -04004526 if (!trans->bytes_reserved)
4527 return;
4528
Chris Masone77266e2012-02-24 10:39:05 -05004529 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004530 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004531 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004532 trans->bytes_reserved = 0;
4533}
4534
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004535/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004536int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4537 struct inode *inode)
4538{
4539 struct btrfs_root *root = BTRFS_I(inode)->root;
4540 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4541 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4542
4543 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004544 * We need to hold space in order to delete our orphan item once we've
4545 * added it, so this takes the reservation so we can release it later
4546 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004547 */
Chris Masonff5714c2011-05-28 07:00:39 -04004548 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004549 trace_btrfs_space_reservation(root->fs_info, "orphan",
4550 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004551 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4552}
4553
4554void btrfs_orphan_release_metadata(struct inode *inode)
4555{
4556 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004557 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004558 trace_btrfs_space_reservation(root->fs_info, "orphan",
4559 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004560 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4561}
4562
Miao Xied5c12072013-02-28 10:04:33 +00004563/*
4564 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4565 * root: the root of the parent directory
4566 * rsv: block reservation
4567 * items: the number of items that we need do reservation
4568 * qgroup_reserved: used to return the reserved size in qgroup
4569 *
4570 * This function is used to reserve the space for snapshot/subvolume
4571 * creation and deletion. Those operations are different with the
4572 * common file/directory operations, they change two fs/file trees
4573 * and root tree, the number of items that the qgroup reserves is
4574 * different with the free space reservation. So we can not use
4575 * the space reseravtion mechanism in start_transaction().
4576 */
4577int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4578 struct btrfs_block_rsv *rsv,
4579 int items,
4580 u64 *qgroup_reserved)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004581{
Miao Xied5c12072013-02-28 10:04:33 +00004582 u64 num_bytes;
4583 int ret;
4584
4585 if (root->fs_info->quota_enabled) {
4586 /* One for parent inode, two for dir entries */
4587 num_bytes = 3 * root->leafsize;
4588 ret = btrfs_qgroup_reserve(root, num_bytes);
4589 if (ret)
4590 return ret;
4591 } else {
4592 num_bytes = 0;
4593 }
4594
4595 *qgroup_reserved = num_bytes;
4596
4597 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4598 rsv->space_info = __find_space_info(root->fs_info,
4599 BTRFS_BLOCK_GROUP_METADATA);
4600 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4601 BTRFS_RESERVE_FLUSH_ALL);
4602 if (ret) {
4603 if (*qgroup_reserved)
4604 btrfs_qgroup_free(root, *qgroup_reserved);
4605 }
4606
4607 return ret;
4608}
4609
4610void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4611 struct btrfs_block_rsv *rsv,
4612 u64 qgroup_reserved)
4613{
4614 btrfs_block_rsv_release(root, rsv, (u64)-1);
4615 if (qgroup_reserved)
4616 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004617}
4618
Josef Bacik7709cde2011-08-04 10:25:02 -04004619/**
4620 * drop_outstanding_extent - drop an outstanding extent
4621 * @inode: the inode we're dropping the extent for
4622 *
4623 * This is called when we are freeing up an outstanding extent, either called
4624 * after an error or after an extent is written. This will return the number of
4625 * reserved extents that need to be freed. This must be called with
4626 * BTRFS_I(inode)->lock held.
4627 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004628static unsigned drop_outstanding_extent(struct inode *inode)
4629{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004630 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004631 unsigned dropped_extents = 0;
4632
Josef Bacik9e0baf62011-07-15 15:16:44 +00004633 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4634 BTRFS_I(inode)->outstanding_extents--;
4635
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004636 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004637 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4638 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004639 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004640
Josef Bacik9e0baf62011-07-15 15:16:44 +00004641 /*
4642 * If we have more or the same amount of outsanding extents than we have
4643 * reserved then we need to leave the reserved extents count alone.
4644 */
4645 if (BTRFS_I(inode)->outstanding_extents >=
4646 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004647 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004648
4649 dropped_extents = BTRFS_I(inode)->reserved_extents -
4650 BTRFS_I(inode)->outstanding_extents;
4651 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004652 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004653}
4654
Josef Bacik7709cde2011-08-04 10:25:02 -04004655/**
4656 * calc_csum_metadata_size - return the amount of metada space that must be
4657 * reserved/free'd for the given bytes.
4658 * @inode: the inode we're manipulating
4659 * @num_bytes: the number of bytes in question
4660 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4661 *
4662 * This adjusts the number of csum_bytes in the inode and then returns the
4663 * correct amount of metadata that must either be reserved or freed. We
4664 * calculate how many checksums we can fit into one leaf and then divide the
4665 * number of bytes that will need to be checksumed by this value to figure out
4666 * how many checksums will be required. If we are adding bytes then the number
4667 * may go up and we will return the number of additional bytes that must be
4668 * reserved. If it is going down we will return the number of bytes that must
4669 * be freed.
4670 *
4671 * This must be called with BTRFS_I(inode)->lock held.
4672 */
4673static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4674 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004675{
Josef Bacik7709cde2011-08-04 10:25:02 -04004676 struct btrfs_root *root = BTRFS_I(inode)->root;
4677 u64 csum_size;
4678 int num_csums_per_leaf;
4679 int num_csums;
4680 int old_csums;
4681
4682 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4683 BTRFS_I(inode)->csum_bytes == 0)
4684 return 0;
4685
4686 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4687 if (reserve)
4688 BTRFS_I(inode)->csum_bytes += num_bytes;
4689 else
4690 BTRFS_I(inode)->csum_bytes -= num_bytes;
4691 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4692 num_csums_per_leaf = (int)div64_u64(csum_size,
4693 sizeof(struct btrfs_csum_item) +
4694 sizeof(struct btrfs_disk_key));
4695 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4696 num_csums = num_csums + num_csums_per_leaf - 1;
4697 num_csums = num_csums / num_csums_per_leaf;
4698
4699 old_csums = old_csums + num_csums_per_leaf - 1;
4700 old_csums = old_csums / num_csums_per_leaf;
4701
4702 /* No change, no need to reserve more */
4703 if (old_csums == num_csums)
4704 return 0;
4705
4706 if (reserve)
4707 return btrfs_calc_trans_metadata_size(root,
4708 num_csums - old_csums);
4709
4710 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004711}
4712
4713int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4714{
4715 struct btrfs_root *root = BTRFS_I(inode)->root;
4716 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004717 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004718 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004719 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004720 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004721 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004722 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004723 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004724
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004725 /* If we are a free space inode we need to not flush since we will be in
4726 * the middle of a transaction commit. We also don't need the delalloc
4727 * mutex since we won't race with anybody. We need this mostly to make
4728 * lockdep shut its filthy mouth.
4729 */
4730 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004731 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004732 delalloc_lock = false;
4733 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004734
Miao Xie08e007d2012-10-16 11:33:38 +00004735 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4736 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004737 schedule_timeout(1);
4738
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004739 if (delalloc_lock)
4740 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4741
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004742 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004743
Josef Bacik9e0baf62011-07-15 15:16:44 +00004744 spin_lock(&BTRFS_I(inode)->lock);
4745 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004746
Josef Bacik9e0baf62011-07-15 15:16:44 +00004747 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004748 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004749 nr_extents = BTRFS_I(inode)->outstanding_extents -
4750 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004751
4752 /*
4753 * Add an item to reserve for updating the inode when we complete the
4754 * delalloc io.
4755 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004756 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4757 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004758 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004759 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004760 }
4761
4762 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004763 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004764 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004765 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004766
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004767 if (root->fs_info->quota_enabled)
Arne Jansenc5567232011-09-14 15:44:05 +02004768 ret = btrfs_qgroup_reserve(root, num_bytes +
4769 nr_extents * root->leafsize);
Arne Jansenc5567232011-09-14 15:44:05 +02004770
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004771 /*
4772 * ret != 0 here means the qgroup reservation failed, we go straight to
4773 * the shared error handling then.
4774 */
4775 if (ret == 0)
4776 ret = reserve_metadata_bytes(root, block_rsv,
4777 to_reserve, flush);
4778
Josef Bacik9e0baf62011-07-15 15:16:44 +00004779 if (ret) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004780 u64 to_free = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004781 unsigned dropped;
Josef Bacik7ed49f12011-08-19 15:45:52 -04004782
Josef Bacik7709cde2011-08-04 10:25:02 -04004783 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004784 dropped = drop_outstanding_extent(inode);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004785 /*
Josef Bacik660d3f62011-12-09 11:18:51 -05004786 * If the inodes csum_bytes is the same as the original
4787 * csum_bytes then we know we haven't raced with any free()ers
4788 * so we can just reduce our inodes csum bytes and carry on.
4789 * Otherwise we have to do the normal free thing to account for
4790 * the case that the free side didn't free up its reserve
4791 * because of this outstanding reservation.
Josef Bacik7ed49f12011-08-19 15:45:52 -04004792 */
Josef Bacik660d3f62011-12-09 11:18:51 -05004793 if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4794 calc_csum_metadata_size(inode, num_bytes, 0);
4795 else
4796 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4797 spin_unlock(&BTRFS_I(inode)->lock);
4798 if (dropped)
4799 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4800
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004801 if (to_free) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004802 btrfs_block_rsv_release(root, block_rsv, to_free);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004803 trace_btrfs_space_reservation(root->fs_info,
4804 "delalloc",
4805 btrfs_ino(inode),
4806 to_free, 0);
4807 }
Miao Xie4b5829a2012-12-05 10:53:25 +00004808 if (root->fs_info->quota_enabled) {
4809 btrfs_qgroup_free(root, num_bytes +
4810 nr_extents * root->leafsize);
4811 }
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004812 if (delalloc_lock)
4813 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004814 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004815 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004816
Josef Bacik660d3f62011-12-09 11:18:51 -05004817 spin_lock(&BTRFS_I(inode)->lock);
4818 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004819 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4820 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004821 nr_extents--;
4822 }
4823 BTRFS_I(inode)->reserved_extents += nr_extents;
4824 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004825
4826 if (delalloc_lock)
4827 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004828
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004829 if (to_reserve)
4830 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4831 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004832 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4833
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004834 return 0;
4835}
4836
Josef Bacik7709cde2011-08-04 10:25:02 -04004837/**
4838 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4839 * @inode: the inode to release the reservation for
4840 * @num_bytes: the number of bytes we're releasing
4841 *
4842 * This will release the metadata reservation for an inode. This can be called
4843 * once we complete IO for a given set of bytes to release their metadata
4844 * reservations.
4845 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004846void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4847{
4848 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004849 u64 to_free = 0;
4850 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004851
4852 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004853 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004854 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004855
Miao Xie09348562013-02-07 10:12:07 +00004856 if (num_bytes)
4857 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04004858 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004859 if (dropped > 0)
4860 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004861
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004862 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4863 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004864 if (root->fs_info->quota_enabled) {
4865 btrfs_qgroup_free(root, num_bytes +
4866 dropped * root->leafsize);
4867 }
4868
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004869 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4870 to_free);
4871}
4872
Josef Bacik7709cde2011-08-04 10:25:02 -04004873/**
4874 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4875 * @inode: inode we're writing to
4876 * @num_bytes: the number of bytes we want to allocate
4877 *
4878 * This will do the following things
4879 *
4880 * o reserve space in the data space info for num_bytes
4881 * o reserve space in the metadata space info based on number of outstanding
4882 * extents and how much csums will be needed
4883 * o add to the inodes ->delalloc_bytes
4884 * o add it to the fs_info's delalloc inodes list.
4885 *
4886 * This will return 0 for success and -ENOSPC if there is no space left.
4887 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004888int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4889{
4890 int ret;
4891
4892 ret = btrfs_check_data_free_space(inode, num_bytes);
4893 if (ret)
4894 return ret;
4895
4896 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4897 if (ret) {
4898 btrfs_free_reserved_data_space(inode, num_bytes);
4899 return ret;
4900 }
4901
4902 return 0;
4903}
4904
Josef Bacik7709cde2011-08-04 10:25:02 -04004905/**
4906 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4907 * @inode: inode we're releasing space for
4908 * @num_bytes: the number of bytes we want to free up
4909 *
4910 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4911 * called in the case that we don't need the metadata AND data reservations
4912 * anymore. So if there is an error or we insert an inline extent.
4913 *
4914 * This function will release the metadata space that was not used and will
4915 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4916 * list if there are no delalloc bytes left.
4917 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004918void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4919{
4920 btrfs_delalloc_release_metadata(inode, num_bytes);
4921 btrfs_free_reserved_data_space(inode, num_bytes);
4922}
4923
Liu Boc53d6132012-12-27 09:01:19 +00004924static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004925 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004926{
Josef Bacik0af3d002010-06-21 14:48:16 -04004927 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004928 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004929 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004930 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004931 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004932 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004933
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004934 /* block accounting for super block */
4935 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02004936 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004937 if (alloc)
4938 old_val += num_bytes;
4939 else
4940 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02004941 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004942 spin_unlock(&info->delalloc_lock);
4943
Chris Masond3977122009-01-05 21:25:51 -05004944 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004945 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004946 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004947 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004948 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4949 BTRFS_BLOCK_GROUP_RAID1 |
4950 BTRFS_BLOCK_GROUP_RAID10))
4951 factor = 2;
4952 else
4953 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004954 /*
4955 * If this block group has free space cache written out, we
4956 * need to make sure to load it if we are removing space. This
4957 * is because we need the unpinning stage to actually add the
4958 * space back to the block group, otherwise we will leak space.
4959 */
4960 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00004961 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004962
Chris Masondb945352007-10-15 16:15:53 -04004963 byte_in_group = bytenr - cache->key.objectid;
4964 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004965
Josef Bacik25179202008-10-29 14:49:05 -04004966 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004967 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004968
Josef Bacik73bc1872011-10-03 14:07:49 -04004969 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04004970 cache->disk_cache_state < BTRFS_DC_CLEAR)
4971 cache->disk_cache_state = BTRFS_DC_CLEAR;
4972
Josef Bacik0f9dd462008-09-23 13:14:11 -04004973 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004974 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004975 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004976 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004977 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004978 btrfs_set_block_group_used(&cache->item, old_val);
4979 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004980 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004981 cache->space_info->bytes_used += num_bytes;
4982 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004983 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004984 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004985 } else {
Chris Masondb945352007-10-15 16:15:53 -04004986 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004987 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004988 cache->pinned += num_bytes;
4989 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004990 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004991 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004992 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004993 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004994
Yan, Zhengf0486c62010-05-16 10:46:25 -04004995 set_extent_dirty(info->pinned_extents,
4996 bytenr, bytenr + num_bytes - 1,
4997 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004998 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004999 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005000 total -= num_bytes;
5001 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005002 }
5003 return 0;
5004}
Chris Mason6324fbf2008-03-24 15:01:59 -04005005
Chris Masona061fc82008-05-07 11:43:44 -04005006static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5007{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005008 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005009 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005010
Liu Boa1897fd2012-12-27 09:01:23 +00005011 spin_lock(&root->fs_info->block_group_cache_lock);
5012 bytenr = root->fs_info->first_logical_byte;
5013 spin_unlock(&root->fs_info->block_group_cache_lock);
5014
5015 if (bytenr < (u64)-1)
5016 return bytenr;
5017
Josef Bacik0f9dd462008-09-23 13:14:11 -04005018 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5019 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005020 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005021
Yan Zhengd2fb3432008-12-11 16:30:39 -05005022 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005023 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005024
5025 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005026}
5027
Yan, Zhengf0486c62010-05-16 10:46:25 -04005028static int pin_down_extent(struct btrfs_root *root,
5029 struct btrfs_block_group_cache *cache,
5030 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005031{
Yan Zheng11833d62009-09-11 16:11:19 -04005032 spin_lock(&cache->space_info->lock);
5033 spin_lock(&cache->lock);
5034 cache->pinned += num_bytes;
5035 cache->space_info->bytes_pinned += num_bytes;
5036 if (reserved) {
5037 cache->reserved -= num_bytes;
5038 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005039 }
Yan Zheng11833d62009-09-11 16:11:19 -04005040 spin_unlock(&cache->lock);
5041 spin_unlock(&cache->space_info->lock);
5042
Yan, Zhengf0486c62010-05-16 10:46:25 -04005043 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5044 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005045 return 0;
5046}
Chris Mason9078a3e2007-04-26 16:46:15 -04005047
Yan, Zhengf0486c62010-05-16 10:46:25 -04005048/*
5049 * this function must be called within transaction
5050 */
5051int btrfs_pin_extent(struct btrfs_root *root,
5052 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005053{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005054 struct btrfs_block_group_cache *cache;
5055
5056 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005057 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005058
5059 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5060
5061 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005062 return 0;
5063}
Zheng Yane8569812008-09-26 10:05:48 -04005064
Yan, Zhengf0486c62010-05-16 10:46:25 -04005065/*
Chris Masone688b722011-10-31 20:52:39 -04005066 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005067 */
Liu Bodcfac412012-12-27 09:01:20 +00005068int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b722011-10-31 20:52:39 -04005069 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005070{
Chris Masone688b722011-10-31 20:52:39 -04005071 struct btrfs_block_group_cache *cache;
5072
5073 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005074 BUG_ON(!cache); /* Logic error */
Chris Masone688b722011-10-31 20:52:39 -04005075
5076 /*
5077 * pull in the free space cache (if any) so that our pin
5078 * removes the free space from the cache. We have load_only set
5079 * to one because the slow code to read in the free extents does check
5080 * the pinned extents.
5081 */
Liu Bof6373bf2012-12-27 09:01:18 +00005082 cache_block_group(cache, 1);
Chris Masone688b722011-10-31 20:52:39 -04005083
5084 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5085
5086 /* remove us from the free space cache (if we're there at all) */
5087 btrfs_remove_free_space(cache, bytenr, num_bytes);
5088 btrfs_put_block_group(cache);
5089 return 0;
5090}
5091
Josef Bacikfb25e912011-07-26 17:00:46 -04005092/**
5093 * btrfs_update_reserved_bytes - update the block_group and space info counters
5094 * @cache: The cache we are manipulating
5095 * @num_bytes: The number of bytes in question
5096 * @reserve: One of the reservation enums
5097 *
5098 * This is called by the allocator when it reserves space, or by somebody who is
5099 * freeing space that was never actually used on disk. For example if you
5100 * reserve some space for a new leaf in transaction A and before transaction A
5101 * commits you free that leaf, you call this with reserve set to 0 in order to
5102 * clear the reservation.
5103 *
5104 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5105 * ENOSPC accounting. For data we handle the reservation through clearing the
5106 * delalloc bits in the io_tree. We have to do this since we could end up
5107 * allocating less disk space for the amount of data we have reserved in the
5108 * case of compression.
5109 *
5110 * If this is a reservation and the block group has become read only we cannot
5111 * make the reservation and return -EAGAIN, otherwise this function always
5112 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005113 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005114static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5115 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005116{
Josef Bacikfb25e912011-07-26 17:00:46 -04005117 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005118 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005119
Josef Bacikfb25e912011-07-26 17:00:46 -04005120 spin_lock(&space_info->lock);
5121 spin_lock(&cache->lock);
5122 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005123 if (cache->ro) {
5124 ret = -EAGAIN;
5125 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005126 cache->reserved += num_bytes;
5127 space_info->bytes_reserved += num_bytes;
5128 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005129 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005130 "space_info", space_info->flags,
5131 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005132 space_info->bytes_may_use -= num_bytes;
5133 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005134 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005135 } else {
5136 if (cache->ro)
5137 space_info->bytes_readonly += num_bytes;
5138 cache->reserved -= num_bytes;
5139 space_info->bytes_reserved -= num_bytes;
5140 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005141 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005142 spin_unlock(&cache->lock);
5143 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005144 return ret;
5145}
5146
Jeff Mahoney143bede2012-03-01 14:56:26 +01005147void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005148 struct btrfs_root *root)
5149{
5150 struct btrfs_fs_info *fs_info = root->fs_info;
5151 struct btrfs_caching_control *next;
5152 struct btrfs_caching_control *caching_ctl;
5153 struct btrfs_block_group_cache *cache;
5154
5155 down_write(&fs_info->extent_commit_sem);
5156
5157 list_for_each_entry_safe(caching_ctl, next,
5158 &fs_info->caching_block_groups, list) {
5159 cache = caching_ctl->block_group;
5160 if (block_group_cache_done(cache)) {
5161 cache->last_byte_to_unpin = (u64)-1;
5162 list_del_init(&caching_ctl->list);
5163 put_caching_control(caching_ctl);
5164 } else {
5165 cache->last_byte_to_unpin = caching_ctl->progress;
5166 }
5167 }
5168
5169 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5170 fs_info->pinned_extents = &fs_info->freed_extents[1];
5171 else
5172 fs_info->pinned_extents = &fs_info->freed_extents[0];
5173
5174 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005175
5176 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005177}
5178
5179static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5180{
5181 struct btrfs_fs_info *fs_info = root->fs_info;
5182 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005183 struct btrfs_space_info *space_info;
5184 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005185 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005186 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005187
5188 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005189 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005190 if (!cache ||
5191 start >= cache->key.objectid + cache->key.offset) {
5192 if (cache)
5193 btrfs_put_block_group(cache);
5194 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005195 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005196 }
5197
5198 len = cache->key.objectid + cache->key.offset - start;
5199 len = min(len, end + 1 - start);
5200
5201 if (start < cache->last_byte_to_unpin) {
5202 len = min(len, cache->last_byte_to_unpin - start);
5203 btrfs_add_free_space(cache, start, len);
5204 }
Josef Bacik25179202008-10-29 14:49:05 -04005205
Yan, Zhengf0486c62010-05-16 10:46:25 -04005206 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005207 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005208
Josef Bacik7b398f82012-10-22 15:52:28 -04005209 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005210 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005211 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005212 space_info->bytes_pinned -= len;
5213 if (cache->ro) {
5214 space_info->bytes_readonly += len;
5215 readonly = true;
5216 }
Josef Bacik25179202008-10-29 14:49:05 -04005217 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005218 if (!readonly && global_rsv->space_info == space_info) {
5219 spin_lock(&global_rsv->lock);
5220 if (!global_rsv->full) {
5221 len = min(len, global_rsv->size -
5222 global_rsv->reserved);
5223 global_rsv->reserved += len;
5224 space_info->bytes_may_use += len;
5225 if (global_rsv->reserved >= global_rsv->size)
5226 global_rsv->full = 1;
5227 }
5228 spin_unlock(&global_rsv->lock);
5229 }
5230 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005231 }
5232
5233 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005234 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005235 return 0;
5236}
5237
5238int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005239 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005240{
Yan Zheng11833d62009-09-11 16:11:19 -04005241 struct btrfs_fs_info *fs_info = root->fs_info;
5242 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005243 u64 start;
5244 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005245 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005246
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005247 if (trans->aborted)
5248 return 0;
5249
Yan Zheng11833d62009-09-11 16:11:19 -04005250 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5251 unpin = &fs_info->freed_extents[1];
5252 else
5253 unpin = &fs_info->freed_extents[0];
5254
Chris Masond3977122009-01-05 21:25:51 -05005255 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005256 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005257 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005258 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005259 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005260
Li Dongyang5378e602011-03-24 10:24:27 +00005261 if (btrfs_test_opt(root, DISCARD))
5262 ret = btrfs_discard_extent(root, start,
5263 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005264
Chris Mason1a5bc162007-10-15 16:15:26 -04005265 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005266 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005267 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005268 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005269
Chris Masone20d96d2007-03-22 12:13:20 -04005270 return 0;
5271}
5272
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005273static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5274 struct btrfs_root *root,
5275 u64 bytenr, u64 num_bytes, u64 parent,
5276 u64 root_objectid, u64 owner_objectid,
5277 u64 owner_offset, int refs_to_drop,
5278 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005279{
Chris Masone2fa7222007-03-12 16:22:34 -04005280 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005282 struct btrfs_fs_info *info = root->fs_info;
5283 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005284 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005285 struct btrfs_extent_item *ei;
5286 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005287 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005288 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005289 int extent_slot = 0;
5290 int found_extent = 0;
5291 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005292 u32 item_size;
5293 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05005294
Chris Mason5caf2a02007-04-02 11:20:42 -04005295 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005296 if (!path)
5297 return -ENOMEM;
5298
Chris Mason3c12ac72008-04-21 12:01:38 -04005299 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005300 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005301
5302 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5303 BUG_ON(!is_data && refs_to_drop != 1);
5304
5305 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5306 bytenr, num_bytes, parent,
5307 root_objectid, owner_objectid,
5308 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005309 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005310 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005311 while (extent_slot >= 0) {
5312 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005313 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005314 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005315 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005316 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5317 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005318 found_extent = 1;
5319 break;
5320 }
5321 if (path->slots[0] - extent_slot > 5)
5322 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005323 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005324 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005325#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5326 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5327 if (found_extent && item_size < sizeof(*ei))
5328 found_extent = 0;
5329#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005330 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005331 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005332 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005333 NULL, refs_to_drop,
5334 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005335 if (ret) {
5336 btrfs_abort_transaction(trans, extent_root, ret);
5337 goto out;
5338 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005339 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005340 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005341
5342 key.objectid = bytenr;
5343 key.type = BTRFS_EXTENT_ITEM_KEY;
5344 key.offset = num_bytes;
5345
Zheng Yan31840ae2008-09-23 13:14:14 -04005346 ret = btrfs_search_slot(trans, extent_root,
5347 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005348 if (ret) {
5349 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05005350 ", was looking for %llu\n", ret,
5351 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005352 if (ret > 0)
5353 btrfs_print_leaf(extent_root,
5354 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005355 }
David Sterba005d6422012-09-18 07:52:32 -06005356 if (ret < 0) {
5357 btrfs_abort_transaction(trans, extent_root, ret);
5358 goto out;
5359 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005360 extent_slot = path->slots[0];
5361 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005362 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005363 btrfs_print_leaf(extent_root, path->nodes[0]);
5364 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05005365 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005366 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05005367 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04005368 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05005369 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005370 (unsigned long long)owner_objectid,
5371 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005372 } else {
David Sterba005d6422012-09-18 07:52:32 -06005373 btrfs_abort_transaction(trans, extent_root, ret);
5374 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005375 }
Chris Mason5f39d392007-10-15 16:14:19 -04005376
5377 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005378 item_size = btrfs_item_size_nr(leaf, extent_slot);
5379#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5380 if (item_size < sizeof(*ei)) {
5381 BUG_ON(found_extent || extent_slot != path->slots[0]);
5382 ret = convert_extent_item_v0(trans, extent_root, path,
5383 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005384 if (ret < 0) {
5385 btrfs_abort_transaction(trans, extent_root, ret);
5386 goto out;
5387 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005388
David Sterbab3b4aa72011-04-21 01:20:15 +02005389 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005390 path->leave_spinning = 1;
5391
5392 key.objectid = bytenr;
5393 key.type = BTRFS_EXTENT_ITEM_KEY;
5394 key.offset = num_bytes;
5395
5396 ret = btrfs_search_slot(trans, extent_root, &key, path,
5397 -1, 1);
5398 if (ret) {
5399 printk(KERN_ERR "umm, got %d back from search"
5400 ", was looking for %llu\n", ret,
5401 (unsigned long long)bytenr);
5402 btrfs_print_leaf(extent_root, path->nodes[0]);
5403 }
David Sterba005d6422012-09-18 07:52:32 -06005404 if (ret < 0) {
5405 btrfs_abort_transaction(trans, extent_root, ret);
5406 goto out;
5407 }
5408
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005409 extent_slot = path->slots[0];
5410 leaf = path->nodes[0];
5411 item_size = btrfs_item_size_nr(leaf, extent_slot);
5412 }
5413#endif
5414 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005415 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005416 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005417 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5418 struct btrfs_tree_block_info *bi;
5419 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5420 bi = (struct btrfs_tree_block_info *)(ei + 1);
5421 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005422 }
5423
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005424 refs = btrfs_extent_refs(leaf, ei);
5425 BUG_ON(refs < refs_to_drop);
5426 refs -= refs_to_drop;
5427
5428 if (refs > 0) {
5429 if (extent_op)
5430 __run_delayed_extent_op(extent_op, leaf, ei);
5431 /*
5432 * In the case of inline back ref, reference count will
5433 * be updated by remove_extent_backref
5434 */
5435 if (iref) {
5436 BUG_ON(!found_extent);
5437 } else {
5438 btrfs_set_extent_refs(leaf, ei, refs);
5439 btrfs_mark_buffer_dirty(leaf);
5440 }
5441 if (found_extent) {
5442 ret = remove_extent_backref(trans, extent_root, path,
5443 iref, refs_to_drop,
5444 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005445 if (ret) {
5446 btrfs_abort_transaction(trans, extent_root, ret);
5447 goto out;
5448 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005449 }
5450 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005451 if (found_extent) {
5452 BUG_ON(is_data && refs_to_drop !=
5453 extent_data_ref_count(root, path, iref));
5454 if (iref) {
5455 BUG_ON(path->slots[0] != extent_slot);
5456 } else {
5457 BUG_ON(path->slots[0] != extent_slot + 1);
5458 path->slots[0] = extent_slot;
5459 num_to_del = 2;
5460 }
Chris Mason78fae272007-03-25 11:35:08 -04005461 }
Chris Masonb9473432009-03-13 11:00:37 -04005462
Chris Mason952fcca2008-02-18 16:33:44 -05005463 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5464 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005465 if (ret) {
5466 btrfs_abort_transaction(trans, extent_root, ret);
5467 goto out;
5468 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005469 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005470
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005472 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005473 if (ret) {
5474 btrfs_abort_transaction(trans, extent_root, ret);
5475 goto out;
5476 }
Chris Mason459931e2008-12-10 09:10:46 -05005477 }
5478
Liu Boc53d6132012-12-27 09:01:19 +00005479 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005480 if (ret) {
5481 btrfs_abort_transaction(trans, extent_root, ret);
5482 goto out;
5483 }
Chris Masona28ec192007-03-06 20:08:01 -05005484 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005485out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005486 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005487 return ret;
5488}
5489
5490/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005491 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005492 * delayed ref for that extent as well. This searches the delayed ref tree for
5493 * a given extent, and if there are no other delayed refs to be processed, it
5494 * removes it from the tree.
5495 */
5496static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5497 struct btrfs_root *root, u64 bytenr)
5498{
5499 struct btrfs_delayed_ref_head *head;
5500 struct btrfs_delayed_ref_root *delayed_refs;
5501 struct btrfs_delayed_ref_node *ref;
5502 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005503 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005504
5505 delayed_refs = &trans->transaction->delayed_refs;
5506 spin_lock(&delayed_refs->lock);
5507 head = btrfs_find_delayed_ref_head(trans, bytenr);
5508 if (!head)
5509 goto out;
5510
5511 node = rb_prev(&head->node.rb_node);
5512 if (!node)
5513 goto out;
5514
5515 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5516
5517 /* there are still entries for this ref, we can't drop it */
5518 if (ref->bytenr == bytenr)
5519 goto out;
5520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005521 if (head->extent_op) {
5522 if (!head->must_insert_reserved)
5523 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005524 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005525 head->extent_op = NULL;
5526 }
5527
Chris Mason1887be62009-03-13 10:11:24 -04005528 /*
5529 * waiting for the lock here would deadlock. If someone else has it
5530 * locked they are already in the process of dropping it anyway
5531 */
5532 if (!mutex_trylock(&head->mutex))
5533 goto out;
5534
5535 /*
5536 * at this point we have a head with no other entries. Go
5537 * ahead and process it.
5538 */
5539 head->node.in_tree = 0;
5540 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005541
Chris Mason1887be62009-03-13 10:11:24 -04005542 delayed_refs->num_entries--;
5543
5544 /*
5545 * we don't take a ref on the node because we're removing it from the
5546 * tree, so we just steal the ref the tree was holding.
5547 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005548 delayed_refs->num_heads--;
5549 if (list_empty(&head->cluster))
5550 delayed_refs->num_heads_ready--;
5551
5552 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005553 spin_unlock(&delayed_refs->lock);
5554
Yan, Zhengf0486c62010-05-16 10:46:25 -04005555 BUG_ON(head->extent_op);
5556 if (head->must_insert_reserved)
5557 ret = 1;
5558
5559 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005560 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005561 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005562out:
5563 spin_unlock(&delayed_refs->lock);
5564 return 0;
5565}
5566
Yan, Zhengf0486c62010-05-16 10:46:25 -04005567void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5568 struct btrfs_root *root,
5569 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005570 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005571{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005572 struct btrfs_block_group_cache *cache = NULL;
5573 int ret;
5574
5575 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005576 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5577 buf->start, buf->len,
5578 parent, root->root_key.objectid,
5579 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005580 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005581 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005582 }
5583
5584 if (!last_ref)
5585 return;
5586
Yan, Zhengf0486c62010-05-16 10:46:25 -04005587 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005588
5589 if (btrfs_header_generation(buf) == trans->transid) {
5590 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5591 ret = check_ref_cleanup(trans, root, buf->start);
5592 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005593 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005594 }
5595
5596 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5597 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005598 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005599 }
5600
5601 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5602
5603 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005604 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005605 }
5606out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005607 /*
5608 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5609 * anymore.
5610 */
5611 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005612 btrfs_put_block_group(cache);
5613}
5614
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005615/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005616int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5617 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5618 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005619{
5620 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005621 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005622
Chris Mason56bec292009-03-13 10:10:06 -04005623 /*
5624 * tree log blocks never actually go into the extent allocation
5625 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005626 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005627 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5628 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005629 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005630 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005631 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005632 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005633 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5634 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005635 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005636 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005637 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005638 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5639 num_bytes,
5640 parent, root_objectid, owner,
5641 offset, BTRFS_DROP_DELAYED_REF,
5642 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005643 }
Chris Mason925baed2008-06-25 16:01:30 -04005644 return ret;
5645}
5646
David Woodhouse53b381b2013-01-29 18:40:14 -05005647static u64 stripe_align(struct btrfs_root *root,
5648 struct btrfs_block_group_cache *cache,
5649 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05005650{
Qu Wenruofda28322013-02-26 08:10:22 +00005651 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05005652 return ret;
5653}
5654
Chris Masonfec577f2007-02-26 10:40:21 -05005655/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005656 * when we wait for progress in the block group caching, its because
5657 * our allocation attempt failed at least once. So, we must sleep
5658 * and let some progress happen before we try again.
5659 *
5660 * This function will sleep at least once waiting for new free space to
5661 * show up, and then it will check the block group free space numbers
5662 * for our min num_bytes. Another option is to have it go ahead
5663 * and look in the rbtree for a free extent of a given size, but this
5664 * is a good start.
5665 */
5666static noinline int
5667wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5668 u64 num_bytes)
5669{
Yan Zheng11833d62009-09-11 16:11:19 -04005670 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005671
Yan Zheng11833d62009-09-11 16:11:19 -04005672 caching_ctl = get_caching_control(cache);
5673 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005674 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005675
Yan Zheng11833d62009-09-11 16:11:19 -04005676 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005677 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005678
5679 put_caching_control(caching_ctl);
5680 return 0;
5681}
5682
5683static noinline int
5684wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5685{
5686 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04005687
5688 caching_ctl = get_caching_control(cache);
5689 if (!caching_ctl)
5690 return 0;
5691
5692 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5693
5694 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005695 return 0;
5696}
5697
Liu Bo31e50222012-11-21 14:18:10 +00005698int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005699{
Ilya Dryomov7738a532012-03-27 17:09:17 +03005700 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00005701 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005702 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00005703 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005704 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00005705 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005706 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00005707 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005708 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05005709 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05005710 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05005711 return BTRFS_RAID_RAID6;
5712
5713 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04005714}
5715
Ilya Dryomov7738a532012-03-27 17:09:17 +03005716static int get_block_group_index(struct btrfs_block_group_cache *cache)
5717{
Liu Bo31e50222012-11-21 14:18:10 +00005718 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005719}
5720
Josef Bacik817d52f2009-07-13 21:29:25 -04005721enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005722 LOOP_CACHING_NOWAIT = 0,
5723 LOOP_CACHING_WAIT = 1,
5724 LOOP_ALLOC_CHUNK = 2,
5725 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005726};
5727
5728/*
Chris Masonfec577f2007-02-26 10:40:21 -05005729 * walks the btree of allocated extents and find a hole of a given size.
5730 * The key ins is changed to record the hole:
5731 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005732 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005733 * ins->offset == number of blocks
5734 * Any available blocks before search_start are skipped.
5735 */
Chris Masond3977122009-01-05 21:25:51 -05005736static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005737 struct btrfs_root *orig_root,
5738 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005739 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005740 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005741{
Josef Bacik80eb2342008-10-29 14:49:05 -04005742 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005743 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005744 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005745 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005746 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005747 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005748 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005749 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005750 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005751 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005752 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5753 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005754 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005755 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005756 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005757 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005758 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005759
Chris Masondb945352007-10-15 16:15:53 -04005760 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005761 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005762 ins->objectid = 0;
5763 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005764
Josef Bacik3f7de032011-11-10 08:29:20 -05005765 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5766
Josef Bacik2552d172009-04-03 10:14:19 -04005767 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005768 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00005769 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005770 return -ENOSPC;
5771 }
Josef Bacik2552d172009-04-03 10:14:19 -04005772
Josef Bacik67377732010-09-16 16:19:09 -04005773 /*
5774 * If the space info is for both data and metadata it means we have a
5775 * small filesystem and we can't use the clustering stuff.
5776 */
5777 if (btrfs_mixed_space_info(space_info))
5778 use_cluster = false;
5779
Josef Bacik67377732010-09-16 16:19:09 -04005780 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005781 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005782 if (!btrfs_test_opt(root, SSD))
5783 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005784 }
5785
Josef Bacik67377732010-09-16 16:19:09 -04005786 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5787 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005788 last_ptr = &root->fs_info->data_alloc_cluster;
5789 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005790
Chris Mason239b14b2008-03-24 15:02:07 -04005791 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005792 spin_lock(&last_ptr->lock);
5793 if (last_ptr->block_group)
5794 hint_byte = last_ptr->window_start;
5795 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005796 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005797
Chris Masona061fc82008-05-07 11:43:44 -04005798 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005799 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005800
Josef Bacik817d52f2009-07-13 21:29:25 -04005801 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005802 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005803
Josef Bacik2552d172009-04-03 10:14:19 -04005804 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005805 block_group = btrfs_lookup_block_group(root->fs_info,
5806 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005807 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005808 /*
5809 * we don't want to use the block group if it doesn't match our
5810 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005811 *
5812 * However if we are re-searching with an ideal block group
5813 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005814 */
5815 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005816 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005817 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005818 if (list_empty(&block_group->list) ||
5819 block_group->ro) {
5820 /*
5821 * someone is removing this block group,
5822 * we can't jump into the have_block_group
5823 * target because our list pointers are not
5824 * valid
5825 */
5826 btrfs_put_block_group(block_group);
5827 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005828 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005829 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005830 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005831 }
Josef Bacik2552d172009-04-03 10:14:19 -04005832 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005833 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005834 }
Chris Mason42e70e72008-11-07 18:17:11 -05005835 }
Josef Bacik2552d172009-04-03 10:14:19 -04005836search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005837 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005838 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005839 list_for_each_entry(block_group, &space_info->block_groups[index],
5840 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005841 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005842 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005843
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005844 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005845 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005846 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005847
Chris Mason83a50de2010-12-13 15:06:46 -05005848 /*
5849 * this can happen if we end up cycling through all the
5850 * raid types, but we want to make sure we only allocate
5851 * for the proper type.
5852 */
5853 if (!block_group_bits(block_group, data)) {
5854 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5855 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005856 BTRFS_BLOCK_GROUP_RAID5 |
5857 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05005858 BTRFS_BLOCK_GROUP_RAID10;
5859
5860 /*
5861 * if they asked for extra copies and this block group
5862 * doesn't provide them, bail. This does allow us to
5863 * fill raid0 from raid1.
5864 */
5865 if ((data & extra) && !(block_group->flags & extra))
5866 goto loop;
5867 }
5868
Josef Bacik2552d172009-04-03 10:14:19 -04005869have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05005870 cached = block_group_cache_done(block_group);
5871 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05005872 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00005873 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005874 BUG_ON(ret < 0);
5875 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005876 }
5877
Josef Bacikea6a4782008-11-20 12:16:16 -05005878 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005879 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005880
Josef Bacik0a243252009-09-11 16:11:20 -04005881 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005882 * Ok we want to try and use the cluster allocator, so
5883 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005884 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005885 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05005886 unsigned long aligned_cluster;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005887 /*
5888 * the refill lock keeps out other
5889 * people trying to start a new cluster
5890 */
5891 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005892 used_block_group = last_ptr->block_group;
5893 if (used_block_group != block_group &&
5894 (!used_block_group ||
5895 used_block_group->ro ||
5896 !block_group_bits(used_block_group, data))) {
5897 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04005898 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005899 }
Chris Mason44fb5512009-06-04 15:34:51 -04005900
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005901 if (used_block_group != block_group)
5902 btrfs_get_block_group(used_block_group);
5903
5904 offset = btrfs_alloc_from_cluster(used_block_group,
5905 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005906 if (offset) {
5907 /* we have a block, we're done */
5908 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005909 trace_btrfs_reserve_extent_cluster(root,
5910 block_group, search_start, num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005911 goto checks;
5912 }
5913
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005914 WARN_ON(last_ptr->block_group != used_block_group);
5915 if (used_block_group != block_group) {
5916 btrfs_put_block_group(used_block_group);
5917 used_block_group = block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005918 }
Chris Mason44fb5512009-06-04 15:34:51 -04005919refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005920 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005921 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5922 * set up a new clusters, so lets just skip it
5923 * and let the allocator find whatever block
5924 * it can find. If we reach this point, we
5925 * will have tried the cluster allocator
5926 * plenty of times and not have found
5927 * anything, so we are likely way too
5928 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005929 * anything.
5930 *
5931 * However, if the cluster is taken from the
5932 * current block group, release the cluster
5933 * first, so that we stand a better chance of
5934 * succeeding in the unclustered
5935 * allocation. */
5936 if (loop >= LOOP_NO_EMPTY_SIZE &&
5937 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005938 spin_unlock(&last_ptr->refill_lock);
5939 goto unclustered_alloc;
5940 }
5941
Chris Masonfa9c0d72009-04-03 09:47:43 -04005942 /*
5943 * this cluster didn't work out, free it and
5944 * start over
5945 */
5946 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5947
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005948 if (loop >= LOOP_NO_EMPTY_SIZE) {
5949 spin_unlock(&last_ptr->refill_lock);
5950 goto unclustered_alloc;
5951 }
5952
Chris Mason8de972b2013-01-04 15:39:43 -05005953 aligned_cluster = max_t(unsigned long,
5954 empty_cluster + empty_size,
5955 block_group->full_stripe_len);
5956
Chris Masonfa9c0d72009-04-03 09:47:43 -04005957 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005958 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005959 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05005960 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05005961 aligned_cluster);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005962 if (ret == 0) {
5963 /*
5964 * now pull our allocation out of this
5965 * cluster
5966 */
5967 offset = btrfs_alloc_from_cluster(block_group,
5968 last_ptr, num_bytes,
5969 search_start);
5970 if (offset) {
5971 /* we found one, proceed */
5972 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005973 trace_btrfs_reserve_extent_cluster(root,
5974 block_group, search_start,
5975 num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005976 goto checks;
5977 }
Josef Bacik0a243252009-09-11 16:11:20 -04005978 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5979 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005980 spin_unlock(&last_ptr->refill_lock);
5981
Josef Bacik0a243252009-09-11 16:11:20 -04005982 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005983 wait_block_group_cache_progress(block_group,
5984 num_bytes + empty_cluster + empty_size);
5985 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005986 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005987
Chris Masonfa9c0d72009-04-03 09:47:43 -04005988 /*
5989 * at this point we either didn't find a cluster
5990 * or we weren't able to allocate a block from our
5991 * cluster. Free the cluster we've been trying
5992 * to use, and go to the next block group
5993 */
Josef Bacik0a243252009-09-11 16:11:20 -04005994 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005995 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005996 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005997 }
5998
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005999unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006000 spin_lock(&block_group->free_space_ctl->tree_lock);
6001 if (cached &&
6002 block_group->free_space_ctl->free_space <
6003 num_bytes + empty_cluster + empty_size) {
6004 spin_unlock(&block_group->free_space_ctl->tree_lock);
6005 goto loop;
6006 }
6007 spin_unlock(&block_group->free_space_ctl->tree_lock);
6008
Josef Bacik6226cb02009-04-03 10:14:18 -04006009 offset = btrfs_find_space_for_alloc(block_group, search_start,
6010 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006011 /*
6012 * If we didn't find a chunk, and we haven't failed on this
6013 * block group before, and this block group is in the middle of
6014 * caching and we are ok with waiting, then go ahead and wait
6015 * for progress to be made, and set failed_alloc to true.
6016 *
6017 * If failed_alloc is true then we've already waited on this
6018 * block group once and should move on to the next block group.
6019 */
6020 if (!offset && !failed_alloc && !cached &&
6021 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006022 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006023 num_bytes + empty_size);
6024 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006025 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006026 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006027 if (!cached)
6028 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006029 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006030 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006031checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006032 search_start = stripe_align(root, used_block_group,
6033 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006034
Josef Bacik2552d172009-04-03 10:14:19 -04006035 /* move on to the next group */
6036 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006037 used_block_group->key.objectid + used_block_group->key.offset) {
6038 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006039 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04006040 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006041
Josef Bacik6226cb02009-04-03 10:14:18 -04006042 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006043 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04006044 search_start - offset);
6045 BUG_ON(offset > search_start);
6046
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006047 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006048 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006049 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006050 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006051 goto loop;
6052 }
Yan Zheng11833d62009-09-11 16:11:19 -04006053
Josef Bacik2552d172009-04-03 10:14:19 -04006054 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006055 ins->objectid = search_start;
6056 ins->offset = num_bytes;
6057
Josef Bacik3f7de032011-11-10 08:29:20 -05006058 trace_btrfs_reserve_extent(orig_root, block_group,
6059 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006060 if (used_block_group != block_group)
6061 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006062 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006063 break;
6064loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006065 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006066 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006067 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006068 if (used_block_group != block_group)
6069 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006070 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006071 }
6072 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006073
Miao Xie60d2adb2011-09-09 17:34:35 +08006074 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6075 goto search;
6076
Yan, Zhengb742bb82010-05-16 10:46:24 -04006077 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6078 goto search;
6079
Josef Bacik285ff5a2012-01-13 15:27:45 -05006080 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006081 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6082 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006083 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6084 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6085 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6086 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04006087 */
Josef Bacik723bda22011-05-27 16:11:38 -04006088 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006089 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006090 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006091 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04006092 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04006093 CHUNK_ALLOC_FORCE);
6094 /*
6095 * Do not bail out on ENOSPC since we
6096 * can do more things.
6097 */
6098 if (ret < 0 && ret != -ENOSPC) {
6099 btrfs_abort_transaction(trans,
6100 root, ret);
6101 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006102 }
Josef Bacik723bda22011-05-27 16:11:38 -04006103 }
6104
6105 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006106 empty_size = 0;
6107 empty_cluster = 0;
6108 }
Chris Mason42e70e72008-11-07 18:17:11 -05006109
Josef Bacik723bda22011-05-27 16:11:38 -04006110 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006111 } else if (!ins->objectid) {
6112 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006113 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006114 ret = 0;
6115 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006116out:
Chris Mason0b86a832008-03-24 15:01:56 -04006117
Chris Mason0f70abe2007-02-28 16:46:22 -05006118 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006119}
Chris Masonec44a352008-04-28 15:29:52 -04006120
Josef Bacik9ed74f22009-09-11 16:12:44 -04006121static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6122 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006123{
6124 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006125 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006126
Josef Bacik9ed74f22009-09-11 16:12:44 -04006127 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006128 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6129 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006130 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006131 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006132 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006133 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006134 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6135 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006136 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006137 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006138 (unsigned long long)info->bytes_pinned,
6139 (unsigned long long)info->bytes_reserved,
6140 (unsigned long long)info->bytes_may_use,
6141 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006142 spin_unlock(&info->lock);
6143
6144 if (!dump_block_groups)
6145 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006146
Josef Bacik80eb2342008-10-29 14:49:05 -04006147 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006148again:
6149 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006150 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006151 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006152 (unsigned long long)cache->key.objectid,
6153 (unsigned long long)cache->key.offset,
6154 (unsigned long long)btrfs_block_group_used(&cache->item),
6155 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006156 (unsigned long long)cache->reserved,
6157 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006158 btrfs_dump_free_space(cache, bytes);
6159 spin_unlock(&cache->lock);
6160 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006161 if (++index < BTRFS_NR_RAID_TYPES)
6162 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006163 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006164}
Zheng Yane8569812008-09-26 10:05:48 -04006165
Yan Zheng11833d62009-09-11 16:11:19 -04006166int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6167 struct btrfs_root *root,
6168 u64 num_bytes, u64 min_alloc_size,
6169 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006170 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006171{
Miao Xie9e622d62012-01-26 15:01:12 -05006172 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006173 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006174
Josef Bacik6a632092009-02-20 11:00:09 -05006175 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006176again:
Chris Masondb945352007-10-15 16:15:53 -04006177 WARN_ON(num_bytes < root->sectorsize);
6178 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006179 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006180
Miao Xie9e622d62012-01-26 15:01:12 -05006181 if (ret == -ENOSPC) {
6182 if (!final_tried) {
6183 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006184 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006185 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006186 if (num_bytes == min_alloc_size)
6187 final_tried = true;
6188 goto again;
6189 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6190 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006191
Miao Xie9e622d62012-01-26 15:01:12 -05006192 sinfo = __find_space_info(root->fs_info, data);
6193 printk(KERN_ERR "btrfs allocation failed flags %llu, "
6194 "wanted %llu\n", (unsigned long long)data,
6195 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006196 if (sinfo)
6197 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006198 }
Chris Mason925baed2008-06-25 16:01:30 -04006199 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006200
liubo1abe9b82011-03-24 11:18:59 +00006201 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6202
Josef Bacik0f9dd462008-09-23 13:14:11 -04006203 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006204}
6205
Chris Masone688b722011-10-31 20:52:39 -04006206static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6207 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006208{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006209 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006210 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006211
Josef Bacik0f9dd462008-09-23 13:14:11 -04006212 cache = btrfs_lookup_block_group(root->fs_info, start);
6213 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05006214 printk(KERN_ERR "Unable to find block group for %llu\n",
6215 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006216 return -ENOSPC;
6217 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006218
Li Dongyang5378e602011-03-24 10:24:27 +00006219 if (btrfs_test_opt(root, DISCARD))
6220 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006221
Chris Masone688b722011-10-31 20:52:39 -04006222 if (pin)
6223 pin_down_extent(root, cache, start, len, 1);
6224 else {
6225 btrfs_add_free_space(cache, start, len);
6226 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6227 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006228 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006229
liubo1abe9b82011-03-24 11:18:59 +00006230 trace_btrfs_reserved_extent_free(root, start, len);
6231
Chris Masone6dcd2d2008-07-17 12:53:50 -04006232 return ret;
6233}
6234
Chris Masone688b722011-10-31 20:52:39 -04006235int btrfs_free_reserved_extent(struct btrfs_root *root,
6236 u64 start, u64 len)
6237{
6238 return __btrfs_free_reserved_extent(root, start, len, 0);
6239}
6240
6241int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6242 u64 start, u64 len)
6243{
6244 return __btrfs_free_reserved_extent(root, start, len, 1);
6245}
6246
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006247static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6248 struct btrfs_root *root,
6249 u64 parent, u64 root_objectid,
6250 u64 flags, u64 owner, u64 offset,
6251 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006252{
6253 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006254 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006255 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006256 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006257 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006258 struct extent_buffer *leaf;
6259 int type;
6260 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006261
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006262 if (parent > 0)
6263 type = BTRFS_SHARED_DATA_REF_KEY;
6264 else
6265 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006266
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006267 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006268
6269 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006270 if (!path)
6271 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006272
Chris Masonb9473432009-03-13 11:00:37 -04006273 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006274 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6275 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006276 if (ret) {
6277 btrfs_free_path(path);
6278 return ret;
6279 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006281 leaf = path->nodes[0];
6282 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006283 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006284 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6285 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6286 btrfs_set_extent_flags(leaf, extent_item,
6287 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006288
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006289 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6290 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6291 if (parent > 0) {
6292 struct btrfs_shared_data_ref *ref;
6293 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6294 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6295 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6296 } else {
6297 struct btrfs_extent_data_ref *ref;
6298 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6299 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6300 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6301 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6302 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6303 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006304
6305 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006306 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006307
Liu Boc53d6132012-12-27 09:01:19 +00006308 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006309 if (ret) { /* -ENOENT, logic error */
Chris Masond3977122009-01-05 21:25:51 -05006310 printk(KERN_ERR "btrfs update block group failed for %llu "
6311 "%llu\n", (unsigned long long)ins->objectid,
6312 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006313 BUG();
6314 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006315 return ret;
6316}
6317
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006318static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6319 struct btrfs_root *root,
6320 u64 parent, u64 root_objectid,
6321 u64 flags, struct btrfs_disk_key *key,
6322 int level, struct btrfs_key *ins)
6323{
6324 int ret;
6325 struct btrfs_fs_info *fs_info = root->fs_info;
6326 struct btrfs_extent_item *extent_item;
6327 struct btrfs_tree_block_info *block_info;
6328 struct btrfs_extent_inline_ref *iref;
6329 struct btrfs_path *path;
6330 struct extent_buffer *leaf;
6331 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
6332
6333 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006334 if (!path)
6335 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006336
6337 path->leave_spinning = 1;
6338 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6339 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006340 if (ret) {
6341 btrfs_free_path(path);
6342 return ret;
6343 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006344
6345 leaf = path->nodes[0];
6346 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6347 struct btrfs_extent_item);
6348 btrfs_set_extent_refs(leaf, extent_item, 1);
6349 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6350 btrfs_set_extent_flags(leaf, extent_item,
6351 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6352 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6353
6354 btrfs_set_tree_block_key(leaf, block_info, key);
6355 btrfs_set_tree_block_level(leaf, block_info, level);
6356
6357 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6358 if (parent > 0) {
6359 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6360 btrfs_set_extent_inline_ref_type(leaf, iref,
6361 BTRFS_SHARED_BLOCK_REF_KEY);
6362 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6363 } else {
6364 btrfs_set_extent_inline_ref_type(leaf, iref,
6365 BTRFS_TREE_BLOCK_REF_KEY);
6366 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6367 }
6368
6369 btrfs_mark_buffer_dirty(leaf);
6370 btrfs_free_path(path);
6371
Liu Boc53d6132012-12-27 09:01:19 +00006372 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006373 if (ret) { /* -ENOENT, logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006374 printk(KERN_ERR "btrfs update block group failed for %llu "
6375 "%llu\n", (unsigned long long)ins->objectid,
6376 (unsigned long long)ins->offset);
6377 BUG();
6378 }
6379 return ret;
6380}
6381
6382int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6383 struct btrfs_root *root,
6384 u64 root_objectid, u64 owner,
6385 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006386{
6387 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006388
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006389 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006390
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006391 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6392 ins->offset, 0,
6393 root_objectid, owner, offset,
6394 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006395 return ret;
6396}
Chris Masone02119d2008-09-05 16:13:11 -04006397
6398/*
6399 * this is used by the tree logging recovery code. It records that
6400 * an extent has been allocated and makes sure to clear the free
6401 * space cache bits as well
6402 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006403int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6404 struct btrfs_root *root,
6405 u64 root_objectid, u64 owner, u64 offset,
6406 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006407{
6408 int ret;
6409 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006410 struct btrfs_caching_control *caching_ctl;
6411 u64 start = ins->objectid;
6412 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006413
Chris Masone02119d2008-09-05 16:13:11 -04006414 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006415 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006416 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006417
Yan Zheng11833d62009-09-11 16:11:19 -04006418 if (!caching_ctl) {
6419 BUG_ON(!block_group_cache_done(block_group));
6420 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006421 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006422 } else {
6423 mutex_lock(&caching_ctl->mutex);
6424
6425 if (start >= caching_ctl->progress) {
6426 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006427 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006428 } else if (start + num_bytes <= caching_ctl->progress) {
6429 ret = btrfs_remove_free_space(block_group,
6430 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006431 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006432 } else {
6433 num_bytes = caching_ctl->progress - start;
6434 ret = btrfs_remove_free_space(block_group,
6435 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006436 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006437
6438 start = caching_ctl->progress;
6439 num_bytes = ins->objectid + ins->offset -
6440 caching_ctl->progress;
6441 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006442 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006443 }
6444
6445 mutex_unlock(&caching_ctl->mutex);
6446 put_caching_control(caching_ctl);
6447 }
6448
Josef Bacikfb25e912011-07-26 17:00:46 -04006449 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6450 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006451 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d72009-04-03 09:47:43 -04006452 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006453 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6454 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006455 return ret;
6456}
6457
Chris Mason65b51a02008-08-01 15:11:20 -04006458struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6459 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006460 u64 bytenr, u32 blocksize,
6461 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006462{
6463 struct extent_buffer *buf;
6464
6465 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6466 if (!buf)
6467 return ERR_PTR(-ENOMEM);
6468 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006469 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006470 btrfs_tree_lock(buf);
6471 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006472 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006473
6474 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006475 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006476
Chris Masond0c803c2008-09-11 16:17:57 -04006477 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006478 /*
6479 * we allow two log transactions at a time, use different
6480 * EXENT bit to differentiate dirty pages.
6481 */
6482 if (root->log_transid % 2 == 0)
6483 set_extent_dirty(&root->dirty_log_pages, buf->start,
6484 buf->start + buf->len - 1, GFP_NOFS);
6485 else
6486 set_extent_new(&root->dirty_log_pages, buf->start,
6487 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006488 } else {
6489 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6490 buf->start + buf->len - 1, GFP_NOFS);
6491 }
Chris Mason65b51a02008-08-01 15:11:20 -04006492 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006493 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006494 return buf;
6495}
6496
Yan, Zhengf0486c62010-05-16 10:46:25 -04006497static struct btrfs_block_rsv *
6498use_block_rsv(struct btrfs_trans_handle *trans,
6499 struct btrfs_root *root, u32 blocksize)
6500{
6501 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006502 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006503 int ret;
6504
6505 block_rsv = get_block_rsv(trans, root);
6506
6507 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006508 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6509 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006510 /*
6511 * If we couldn't reserve metadata bytes try and use some from
6512 * the global reserve.
6513 */
6514 if (ret && block_rsv != global_rsv) {
6515 ret = block_rsv_use_bytes(global_rsv, blocksize);
6516 if (!ret)
6517 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006518 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006519 } else if (ret) {
6520 return ERR_PTR(ret);
6521 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006522 return block_rsv;
6523 }
6524
6525 ret = block_rsv_use_bytes(block_rsv, blocksize);
6526 if (!ret)
6527 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006528 if (ret && !block_rsv->failfast) {
David Sterbab069e0c2013-02-08 21:28:17 +00006529 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6530 static DEFINE_RATELIMIT_STATE(_rs,
6531 DEFAULT_RATELIMIT_INTERVAL * 10,
6532 /*DEFAULT_RATELIMIT_BURST*/ 1);
6533 if (__ratelimit(&_rs))
6534 WARN(1, KERN_DEBUG
6535 "btrfs: block rsv returned %d\n", ret);
6536 }
Miao Xie08e007d2012-10-16 11:33:38 +00006537 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6538 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006539 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006540 return block_rsv;
6541 } else if (ret && block_rsv != global_rsv) {
6542 ret = block_rsv_use_bytes(global_rsv, blocksize);
6543 if (!ret)
6544 return global_rsv;
6545 }
6546 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006547
Yan, Zhengf0486c62010-05-16 10:46:25 -04006548 return ERR_PTR(-ENOSPC);
6549}
6550
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006551static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6552 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006553{
6554 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006555 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006556}
6557
Chris Masonfec577f2007-02-26 10:40:21 -05006558/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006559 * finds a free extent and does all the dirty work required for allocation
6560 * returns the key for the extent through ins, and a tree buffer for
6561 * the first block of the extent through buf.
6562 *
Chris Masonfec577f2007-02-26 10:40:21 -05006563 * returns the tree buffer or NULL.
6564 */
Chris Mason5f39d392007-10-15 16:14:19 -04006565struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006566 struct btrfs_root *root, u32 blocksize,
6567 u64 parent, u64 root_objectid,
6568 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006569 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006570{
Chris Masone2fa7222007-03-12 16:22:34 -04006571 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006572 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006573 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006574 u64 flags = 0;
6575 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006576
Yan, Zhengf0486c62010-05-16 10:46:25 -04006577
6578 block_rsv = use_block_rsv(trans, root, blocksize);
6579 if (IS_ERR(block_rsv))
6580 return ERR_CAST(block_rsv);
6581
6582 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006583 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006584 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006585 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006586 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006587 }
Chris Mason55c69072008-01-09 15:55:33 -05006588
Chris Mason4008c042009-02-12 14:09:45 -05006589 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6590 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006591 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006592
6593 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6594 if (parent == 0)
6595 parent = ins.objectid;
6596 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6597 } else
6598 BUG_ON(parent > 0);
6599
6600 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6601 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006602 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006603 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006604 if (key)
6605 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6606 else
6607 memset(&extent_op->key, 0, sizeof(extent_op->key));
6608 extent_op->flags_to_set = flags;
6609 extent_op->update_key = 1;
6610 extent_op->update_flags = 1;
6611 extent_op->is_data = 0;
6612
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006613 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6614 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006615 ins.offset, parent, root_objectid,
6616 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006617 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006618 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006619 }
Chris Masonfec577f2007-02-26 10:40:21 -05006620 return buf;
6621}
Chris Masona28ec192007-03-06 20:08:01 -05006622
Yan Zheng2c47e6052009-06-27 21:07:35 -04006623struct walk_control {
6624 u64 refs[BTRFS_MAX_LEVEL];
6625 u64 flags[BTRFS_MAX_LEVEL];
6626 struct btrfs_key update_progress;
6627 int stage;
6628 int level;
6629 int shared_level;
6630 int update_ref;
6631 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006632 int reada_slot;
6633 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006634 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006635};
6636
6637#define DROP_REFERENCE 1
6638#define UPDATE_BACKREF 2
6639
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006640static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6641 struct btrfs_root *root,
6642 struct walk_control *wc,
6643 struct btrfs_path *path)
6644{
6645 u64 bytenr;
6646 u64 generation;
6647 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006648 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006649 u32 nritems;
6650 u32 blocksize;
6651 struct btrfs_key key;
6652 struct extent_buffer *eb;
6653 int ret;
6654 int slot;
6655 int nread = 0;
6656
6657 if (path->slots[wc->level] < wc->reada_slot) {
6658 wc->reada_count = wc->reada_count * 2 / 3;
6659 wc->reada_count = max(wc->reada_count, 2);
6660 } else {
6661 wc->reada_count = wc->reada_count * 3 / 2;
6662 wc->reada_count = min_t(int, wc->reada_count,
6663 BTRFS_NODEPTRS_PER_BLOCK(root));
6664 }
6665
6666 eb = path->nodes[wc->level];
6667 nritems = btrfs_header_nritems(eb);
6668 blocksize = btrfs_level_size(root, wc->level - 1);
6669
6670 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6671 if (nread >= wc->reada_count)
6672 break;
6673
6674 cond_resched();
6675 bytenr = btrfs_node_blockptr(eb, slot);
6676 generation = btrfs_node_ptr_generation(eb, slot);
6677
6678 if (slot == path->slots[wc->level])
6679 goto reada;
6680
6681 if (wc->stage == UPDATE_BACKREF &&
6682 generation <= root->root_key.offset)
6683 continue;
6684
Yan, Zheng94fcca92009-10-09 09:25:16 -04006685 /* We don't lock the tree block, it's OK to be racy here */
6686 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6687 &refs, &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006688 /* We don't care about errors in readahead. */
6689 if (ret < 0)
6690 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006691 BUG_ON(refs == 0);
6692
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006693 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006694 if (refs == 1)
6695 goto reada;
6696
Yan, Zheng94fcca92009-10-09 09:25:16 -04006697 if (wc->level == 1 &&
6698 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6699 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006700 if (!wc->update_ref ||
6701 generation <= root->root_key.offset)
6702 continue;
6703 btrfs_node_key_to_cpu(eb, &key, slot);
6704 ret = btrfs_comp_cpu_keys(&key,
6705 &wc->update_progress);
6706 if (ret < 0)
6707 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006708 } else {
6709 if (wc->level == 1 &&
6710 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6711 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006712 }
6713reada:
6714 ret = readahead_tree_block(root, bytenr, blocksize,
6715 generation);
6716 if (ret)
6717 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006718 nread++;
6719 }
6720 wc->reada_slot = slot;
6721}
6722
Chris Mason9aca1d52007-03-13 11:09:37 -04006723/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006724 * hepler to process tree block while walking down the tree.
6725 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006726 * when wc->stage == UPDATE_BACKREF, this function updates
6727 * back refs for pointers in the block.
6728 *
6729 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006730 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006731static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6732 struct btrfs_root *root,
6733 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006734 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006735{
6736 int level = wc->level;
6737 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006738 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6739 int ret;
6740
6741 if (wc->stage == UPDATE_BACKREF &&
6742 btrfs_header_owner(eb) != root->root_key.objectid)
6743 return 1;
6744
6745 /*
6746 * when reference count of tree block is 1, it won't increase
6747 * again. once full backref flag is set, we never clear it.
6748 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006749 if (lookup_info &&
6750 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6751 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006752 BUG_ON(!path->locks[level]);
6753 ret = btrfs_lookup_extent_info(trans, root,
6754 eb->start, eb->len,
6755 &wc->refs[level],
6756 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006757 BUG_ON(ret == -ENOMEM);
6758 if (ret)
6759 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006760 BUG_ON(wc->refs[level] == 0);
6761 }
6762
Yan Zheng2c47e6052009-06-27 21:07:35 -04006763 if (wc->stage == DROP_REFERENCE) {
6764 if (wc->refs[level] > 1)
6765 return 1;
6766
6767 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006768 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006769 path->locks[level] = 0;
6770 }
6771 return 0;
6772 }
6773
6774 /* wc->stage == UPDATE_BACKREF */
6775 if (!(wc->flags[level] & flag)) {
6776 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006777 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006778 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006779 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006780 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006781 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6782 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006783 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006784 wc->flags[level] |= flag;
6785 }
6786
6787 /*
6788 * the block is shared by multiple trees, so it's not good to
6789 * keep the tree lock
6790 */
6791 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006792 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006793 path->locks[level] = 0;
6794 }
6795 return 0;
6796}
6797
6798/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006799 * hepler to process tree block pointer.
6800 *
6801 * when wc->stage == DROP_REFERENCE, this function checks
6802 * reference count of the block pointed to. if the block
6803 * is shared and we need update back refs for the subtree
6804 * rooted at the block, this function changes wc->stage to
6805 * UPDATE_BACKREF. if the block is shared and there is no
6806 * need to update back, this function drops the reference
6807 * to the block.
6808 *
6809 * NOTE: return value 1 means we should stop walking down.
6810 */
6811static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6812 struct btrfs_root *root,
6813 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006814 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006815{
6816 u64 bytenr;
6817 u64 generation;
6818 u64 parent;
6819 u32 blocksize;
6820 struct btrfs_key key;
6821 struct extent_buffer *next;
6822 int level = wc->level;
6823 int reada = 0;
6824 int ret = 0;
6825
6826 generation = btrfs_node_ptr_generation(path->nodes[level],
6827 path->slots[level]);
6828 /*
6829 * if the lower level block was created before the snapshot
6830 * was created, we know there is no need to update back refs
6831 * for the subtree
6832 */
6833 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006834 generation <= root->root_key.offset) {
6835 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006836 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006837 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006838
6839 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6840 blocksize = btrfs_level_size(root, level - 1);
6841
6842 next = btrfs_find_tree_block(root, bytenr, blocksize);
6843 if (!next) {
6844 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006845 if (!next)
6846 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006847 reada = 1;
6848 }
6849 btrfs_tree_lock(next);
6850 btrfs_set_lock_blocking(next);
6851
Yan, Zheng94fcca92009-10-09 09:25:16 -04006852 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6853 &wc->refs[level - 1],
6854 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006855 if (ret < 0) {
6856 btrfs_tree_unlock(next);
6857 return ret;
6858 }
6859
Yan, Zheng94fcca92009-10-09 09:25:16 -04006860 BUG_ON(wc->refs[level - 1] == 0);
6861 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006862
Yan, Zheng94fcca92009-10-09 09:25:16 -04006863 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006864 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006865 if (level == 1 &&
6866 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6867 goto skip;
6868
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006869 if (!wc->update_ref ||
6870 generation <= root->root_key.offset)
6871 goto skip;
6872
6873 btrfs_node_key_to_cpu(path->nodes[level], &key,
6874 path->slots[level]);
6875 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6876 if (ret < 0)
6877 goto skip;
6878
6879 wc->stage = UPDATE_BACKREF;
6880 wc->shared_level = level - 1;
6881 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006882 } else {
6883 if (level == 1 &&
6884 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6885 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006886 }
6887
Chris Masonb9fab912012-05-06 07:23:47 -04006888 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006889 btrfs_tree_unlock(next);
6890 free_extent_buffer(next);
6891 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006892 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006893 }
6894
6895 if (!next) {
6896 if (reada && level == 1)
6897 reada_walk_down(trans, root, wc, path);
6898 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006899 if (!next)
6900 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006901 btrfs_tree_lock(next);
6902 btrfs_set_lock_blocking(next);
6903 }
6904
6905 level--;
6906 BUG_ON(level != btrfs_header_level(next));
6907 path->nodes[level] = next;
6908 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006909 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006910 wc->level = level;
6911 if (wc->level == 1)
6912 wc->reada_slot = 0;
6913 return 0;
6914skip:
6915 wc->refs[level - 1] = 0;
6916 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006917 if (wc->stage == DROP_REFERENCE) {
6918 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6919 parent = path->nodes[level]->start;
6920 } else {
6921 BUG_ON(root->root_key.objectid !=
6922 btrfs_header_owner(path->nodes[level]));
6923 parent = 0;
6924 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006925
Yan, Zheng94fcca92009-10-09 09:25:16 -04006926 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006927 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006928 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006929 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006930 btrfs_tree_unlock(next);
6931 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006932 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006933 return 1;
6934}
6935
6936/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006937 * hepler to process tree block while walking up the tree.
6938 *
6939 * when wc->stage == DROP_REFERENCE, this function drops
6940 * reference count on the block.
6941 *
6942 * when wc->stage == UPDATE_BACKREF, this function changes
6943 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6944 * to UPDATE_BACKREF previously while processing the block.
6945 *
6946 * NOTE: return value 1 means we should stop walking up.
6947 */
6948static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6949 struct btrfs_root *root,
6950 struct btrfs_path *path,
6951 struct walk_control *wc)
6952{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006953 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006954 int level = wc->level;
6955 struct extent_buffer *eb = path->nodes[level];
6956 u64 parent = 0;
6957
6958 if (wc->stage == UPDATE_BACKREF) {
6959 BUG_ON(wc->shared_level < level);
6960 if (level < wc->shared_level)
6961 goto out;
6962
Yan Zheng2c47e6052009-06-27 21:07:35 -04006963 ret = find_next_key(path, level + 1, &wc->update_progress);
6964 if (ret > 0)
6965 wc->update_ref = 0;
6966
6967 wc->stage = DROP_REFERENCE;
6968 wc->shared_level = -1;
6969 path->slots[level] = 0;
6970
6971 /*
6972 * check reference count again if the block isn't locked.
6973 * we should start walking down the tree again if reference
6974 * count is one.
6975 */
6976 if (!path->locks[level]) {
6977 BUG_ON(level == 0);
6978 btrfs_tree_lock(eb);
6979 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006980 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006981
6982 ret = btrfs_lookup_extent_info(trans, root,
6983 eb->start, eb->len,
6984 &wc->refs[level],
6985 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006986 if (ret < 0) {
6987 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006988 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006989 return ret;
6990 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006991 BUG_ON(wc->refs[level] == 0);
6992 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006993 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006994 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006995 return 1;
6996 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006997 }
6998 }
6999
7000 /* wc->stage == DROP_REFERENCE */
7001 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7002
7003 if (wc->refs[level] == 1) {
7004 if (level == 0) {
7005 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007006 ret = btrfs_dec_ref(trans, root, eb, 1,
7007 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007008 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007009 ret = btrfs_dec_ref(trans, root, eb, 0,
7010 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007011 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007012 }
7013 /* make block locked assertion in clean_tree_block happy */
7014 if (!path->locks[level] &&
7015 btrfs_header_generation(eb) == trans->transid) {
7016 btrfs_tree_lock(eb);
7017 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007018 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007019 }
7020 clean_tree_block(trans, root, eb);
7021 }
7022
7023 if (eb == root->node) {
7024 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7025 parent = eb->start;
7026 else
7027 BUG_ON(root->root_key.objectid !=
7028 btrfs_header_owner(eb));
7029 } else {
7030 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7031 parent = path->nodes[level + 1]->start;
7032 else
7033 BUG_ON(root->root_key.objectid !=
7034 btrfs_header_owner(path->nodes[level + 1]));
7035 }
7036
Jan Schmidt5581a512012-05-16 17:04:52 +02007037 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007038out:
7039 wc->refs[level] = 0;
7040 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007041 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007042}
7043
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007044static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7045 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007046 struct btrfs_path *path,
7047 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007048{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007049 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007050 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007051 int ret;
7052
Yan Zheng2c47e6052009-06-27 21:07:35 -04007053 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007054 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007055 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007056 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007057
Yan Zheng2c47e6052009-06-27 21:07:35 -04007058 if (level == 0)
7059 break;
7060
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007061 if (path->slots[level] >=
7062 btrfs_header_nritems(path->nodes[level]))
7063 break;
7064
Yan, Zheng94fcca92009-10-09 09:25:16 -04007065 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007066 if (ret > 0) {
7067 path->slots[level]++;
7068 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00007069 } else if (ret < 0)
7070 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007071 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007072 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007073 return 0;
7074}
7075
Chris Masond3977122009-01-05 21:25:51 -05007076static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007077 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007078 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007079 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007080{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007081 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007082 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007083
Yan Zheng2c47e6052009-06-27 21:07:35 -04007084 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7085 while (level < max_level && path->nodes[level]) {
7086 wc->level = level;
7087 if (path->slots[level] + 1 <
7088 btrfs_header_nritems(path->nodes[level])) {
7089 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007090 return 0;
7091 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007092 ret = walk_up_proc(trans, root, path, wc);
7093 if (ret > 0)
7094 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007095
Yan Zheng2c47e6052009-06-27 21:07:35 -04007096 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007097 btrfs_tree_unlock_rw(path->nodes[level],
7098 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007099 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007100 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007101 free_extent_buffer(path->nodes[level]);
7102 path->nodes[level] = NULL;
7103 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007104 }
7105 }
7106 return 1;
7107}
7108
Chris Mason9aca1d52007-03-13 11:09:37 -04007109/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007110 * drop a subvolume tree.
7111 *
7112 * this function traverses the tree freeing any blocks that only
7113 * referenced by the tree.
7114 *
7115 * when a shared tree block is found. this function decreases its
7116 * reference count by one. if update_ref is true, this function
7117 * also make sure backrefs for the shared block and all lower level
7118 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04007119 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007120int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007121 struct btrfs_block_rsv *block_rsv, int update_ref,
7122 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007123{
Chris Mason5caf2a02007-04-02 11:20:42 -04007124 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007125 struct btrfs_trans_handle *trans;
7126 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007127 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007128 struct walk_control *wc;
7129 struct btrfs_key key;
7130 int err = 0;
7131 int ret;
7132 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007133
Chris Mason5caf2a02007-04-02 11:20:42 -04007134 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007135 if (!path) {
7136 err = -ENOMEM;
7137 goto out;
7138 }
Chris Mason20524f02007-03-10 06:35:47 -05007139
Yan Zheng2c47e6052009-06-27 21:07:35 -04007140 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007141 if (!wc) {
7142 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007143 err = -ENOMEM;
7144 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007145 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007146
Yan, Zhenga22285a2010-05-16 10:48:46 -04007147 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007148 if (IS_ERR(trans)) {
7149 err = PTR_ERR(trans);
7150 goto out_free;
7151 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007152
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007153 if (block_rsv)
7154 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007155
Chris Mason9f3a7422007-08-07 15:52:19 -04007156 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007157 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007158 path->nodes[level] = btrfs_lock_root_node(root);
7159 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007160 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007161 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007162 memset(&wc->update_progress, 0,
7163 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007164 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007165 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007166 memcpy(&wc->update_progress, &key,
7167 sizeof(wc->update_progress));
7168
Chris Mason6702ed42007-08-07 16:15:09 -04007169 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007170 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007171 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007172 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7173 path->lowest_level = 0;
7174 if (ret < 0) {
7175 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007176 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007177 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007178 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007179
Chris Mason7d9eb122008-07-08 14:19:17 -04007180 /*
7181 * unlock our path, this is safe because only this
7182 * function is allowed to delete this snapshot
7183 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007184 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007185
Yan Zheng2c47e6052009-06-27 21:07:35 -04007186 level = btrfs_header_level(root->node);
7187 while (1) {
7188 btrfs_tree_lock(path->nodes[level]);
7189 btrfs_set_lock_blocking(path->nodes[level]);
7190
7191 ret = btrfs_lookup_extent_info(trans, root,
7192 path->nodes[level]->start,
7193 path->nodes[level]->len,
7194 &wc->refs[level],
7195 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007196 if (ret < 0) {
7197 err = ret;
7198 goto out_end_trans;
7199 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007200 BUG_ON(wc->refs[level] == 0);
7201
7202 if (level == root_item->drop_level)
7203 break;
7204
7205 btrfs_tree_unlock(path->nodes[level]);
7206 WARN_ON(wc->refs[level] != 1);
7207 level--;
7208 }
7209 }
7210
7211 wc->level = level;
7212 wc->shared_level = -1;
7213 wc->stage = DROP_REFERENCE;
7214 wc->update_ref = update_ref;
7215 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007216 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007217 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007218
7219 while (1) {
7220 ret = walk_down_tree(trans, root, path, wc);
7221 if (ret < 0) {
7222 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007223 break;
7224 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007225
7226 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7227 if (ret < 0) {
7228 err = ret;
7229 break;
7230 }
7231
7232 if (ret > 0) {
7233 BUG_ON(wc->stage != DROP_REFERENCE);
7234 break;
7235 }
7236
7237 if (wc->stage == DROP_REFERENCE) {
7238 level = wc->level;
7239 btrfs_node_key(path->nodes[level],
7240 &root_item->drop_progress,
7241 path->slots[level]);
7242 root_item->drop_level = level;
7243 }
7244
7245 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007246 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007247 ret = btrfs_update_root(trans, tree_root,
7248 &root->root_key,
7249 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007250 if (ret) {
7251 btrfs_abort_transaction(trans, tree_root, ret);
7252 err = ret;
7253 goto out_end_trans;
7254 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007255
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007256 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007257 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007258 if (IS_ERR(trans)) {
7259 err = PTR_ERR(trans);
7260 goto out_free;
7261 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007262 if (block_rsv)
7263 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007264 }
Chris Mason20524f02007-03-10 06:35:47 -05007265 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007266 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007267 if (err)
7268 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007269
7270 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007271 if (ret) {
7272 btrfs_abort_transaction(trans, tree_root, ret);
7273 goto out_end_trans;
7274 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007275
Yan, Zheng76dda932009-09-21 16:00:26 -04007276 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7277 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7278 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007279 if (ret < 0) {
7280 btrfs_abort_transaction(trans, tree_root, ret);
7281 err = ret;
7282 goto out_end_trans;
7283 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007284 /* if we fail to delete the orphan item this time
7285 * around, it'll get picked up the next time.
7286 *
7287 * The most common failure here is just -ENOENT.
7288 */
7289 btrfs_del_orphan_item(trans, tree_root,
7290 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007291 }
7292 }
7293
7294 if (root->in_radix) {
7295 btrfs_free_fs_root(tree_root->fs_info, root);
7296 } else {
7297 free_extent_buffer(root->node);
7298 free_extent_buffer(root->commit_root);
7299 kfree(root);
7300 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007301out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007302 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007303out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007304 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007305 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007306out:
7307 if (err)
7308 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007309 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007310}
Chris Mason9078a3e2007-04-26 16:46:15 -04007311
Yan Zheng2c47e6052009-06-27 21:07:35 -04007312/*
7313 * drop subtree rooted at tree block 'node'.
7314 *
7315 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007316 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007317 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007318int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7319 struct btrfs_root *root,
7320 struct extent_buffer *node,
7321 struct extent_buffer *parent)
7322{
7323 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007324 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007325 int level;
7326 int parent_level;
7327 int ret = 0;
7328 int wret;
7329
Yan Zheng2c47e6052009-06-27 21:07:35 -04007330 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7331
Yan Zhengf82d02d2008-10-29 14:49:05 -04007332 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007333 if (!path)
7334 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007335
Yan Zheng2c47e6052009-06-27 21:07:35 -04007336 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007337 if (!wc) {
7338 btrfs_free_path(path);
7339 return -ENOMEM;
7340 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007341
Chris Masonb9447ef2009-03-09 11:45:38 -04007342 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007343 parent_level = btrfs_header_level(parent);
7344 extent_buffer_get(parent);
7345 path->nodes[parent_level] = parent;
7346 path->slots[parent_level] = btrfs_header_nritems(parent);
7347
Chris Masonb9447ef2009-03-09 11:45:38 -04007348 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007349 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007350 path->nodes[level] = node;
7351 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007352 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007353
7354 wc->refs[parent_level] = 1;
7355 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7356 wc->level = level;
7357 wc->shared_level = -1;
7358 wc->stage = DROP_REFERENCE;
7359 wc->update_ref = 0;
7360 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007361 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007362 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007363
7364 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007365 wret = walk_down_tree(trans, root, path, wc);
7366 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007367 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007368 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007369 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007370
Yan Zheng2c47e6052009-06-27 21:07:35 -04007371 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007372 if (wret < 0)
7373 ret = wret;
7374 if (wret != 0)
7375 break;
7376 }
7377
Yan Zheng2c47e6052009-06-27 21:07:35 -04007378 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007379 btrfs_free_path(path);
7380 return ret;
7381}
7382
Chris Masonec44a352008-04-28 15:29:52 -04007383static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7384{
7385 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007386 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007387
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007388 /*
7389 * if restripe for this chunk_type is on pick target profile and
7390 * return, otherwise do the usual balance
7391 */
7392 stripped = get_restripe_target(root->fs_info, flags);
7393 if (stripped)
7394 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007395
Chris Masoncd02dca2010-12-13 14:56:23 -05007396 /*
7397 * we add in the count of missing devices because we want
7398 * to make sure that any RAID levels on a degraded FS
7399 * continue to be honored.
7400 */
7401 num_devices = root->fs_info->fs_devices->rw_devices +
7402 root->fs_info->fs_devices->missing_devices;
7403
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007404 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007405 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007406 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7407
Chris Masonec44a352008-04-28 15:29:52 -04007408 if (num_devices == 1) {
7409 stripped |= BTRFS_BLOCK_GROUP_DUP;
7410 stripped = flags & ~stripped;
7411
7412 /* turn raid0 into single device chunks */
7413 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7414 return stripped;
7415
7416 /* turn mirroring into duplication */
7417 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7418 BTRFS_BLOCK_GROUP_RAID10))
7419 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007420 } else {
7421 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007422 if (flags & stripped)
7423 return flags;
7424
7425 stripped |= BTRFS_BLOCK_GROUP_DUP;
7426 stripped = flags & ~stripped;
7427
7428 /* switch duplicated blocks with raid1 */
7429 if (flags & BTRFS_BLOCK_GROUP_DUP)
7430 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7431
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007432 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007433 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007434
Chris Masonec44a352008-04-28 15:29:52 -04007435 return flags;
7436}
7437
Miao Xie199c36e2011-07-15 10:34:36 +00007438static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007439{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007440 struct btrfs_space_info *sinfo = cache->space_info;
7441 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007442 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007443 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007444
Chris Masonc286ac42008-07-22 23:06:41 -04007445
Miao Xie199c36e2011-07-15 10:34:36 +00007446 /*
7447 * We need some metadata space and system metadata space for
7448 * allocating chunks in some corner cases until we force to set
7449 * it to be readonly.
7450 */
7451 if ((sinfo->flags &
7452 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7453 !force)
7454 min_allocable_bytes = 1 * 1024 * 1024;
7455 else
7456 min_allocable_bytes = 0;
7457
Yan, Zhengf0486c62010-05-16 10:46:25 -04007458 spin_lock(&sinfo->lock);
7459 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007460
7461 if (cache->ro) {
7462 ret = 0;
7463 goto out;
7464 }
7465
Yan, Zhengf0486c62010-05-16 10:46:25 -04007466 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7467 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007468
Yan, Zhengf0486c62010-05-16 10:46:25 -04007469 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007470 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7471 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007472 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007473 cache->ro = 1;
7474 ret = 0;
7475 }
WuBo61cfea92011-07-26 03:30:11 +00007476out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007477 spin_unlock(&cache->lock);
7478 spin_unlock(&sinfo->lock);
7479 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007480}
7481
Yan, Zhengf0486c62010-05-16 10:46:25 -04007482int btrfs_set_block_group_ro(struct btrfs_root *root,
7483 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007484
7485{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007486 struct btrfs_trans_handle *trans;
7487 u64 alloc_flags;
7488 int ret;
7489
7490 BUG_ON(cache->ro);
7491
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007492 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007493 if (IS_ERR(trans))
7494 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007495
7496 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007497 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007498 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007499 CHUNK_ALLOC_FORCE);
7500 if (ret < 0)
7501 goto out;
7502 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007503
Miao Xie199c36e2011-07-15 10:34:36 +00007504 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007505 if (!ret)
7506 goto out;
7507 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007508 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007509 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007510 if (ret < 0)
7511 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007512 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007513out:
7514 btrfs_end_transaction(trans, root);
7515 return ret;
7516}
7517
Chris Masonc87f08c2011-02-16 13:57:04 -05007518int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7519 struct btrfs_root *root, u64 type)
7520{
7521 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007522 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007523 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007524}
7525
Miao Xie6d07bce2011-01-05 10:07:31 +00007526/*
7527 * helper to account the unused space of all the readonly block group in the
7528 * list. takes mirrors into account.
7529 */
7530static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7531{
7532 struct btrfs_block_group_cache *block_group;
7533 u64 free_bytes = 0;
7534 int factor;
7535
7536 list_for_each_entry(block_group, groups_list, list) {
7537 spin_lock(&block_group->lock);
7538
7539 if (!block_group->ro) {
7540 spin_unlock(&block_group->lock);
7541 continue;
7542 }
7543
7544 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7545 BTRFS_BLOCK_GROUP_RAID10 |
7546 BTRFS_BLOCK_GROUP_DUP))
7547 factor = 2;
7548 else
7549 factor = 1;
7550
7551 free_bytes += (block_group->key.offset -
7552 btrfs_block_group_used(&block_group->item)) *
7553 factor;
7554
7555 spin_unlock(&block_group->lock);
7556 }
7557
7558 return free_bytes;
7559}
7560
7561/*
7562 * helper to account the unused space of all the readonly block group in the
7563 * space_info. takes mirrors into account.
7564 */
7565u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7566{
7567 int i;
7568 u64 free_bytes = 0;
7569
7570 spin_lock(&sinfo->lock);
7571
7572 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7573 if (!list_empty(&sinfo->block_groups[i]))
7574 free_bytes += __btrfs_get_ro_block_group_free_space(
7575 &sinfo->block_groups[i]);
7576
7577 spin_unlock(&sinfo->lock);
7578
7579 return free_bytes;
7580}
7581
Jeff Mahoney143bede2012-03-01 14:56:26 +01007582void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007583 struct btrfs_block_group_cache *cache)
7584{
7585 struct btrfs_space_info *sinfo = cache->space_info;
7586 u64 num_bytes;
7587
7588 BUG_ON(!cache->ro);
7589
7590 spin_lock(&sinfo->lock);
7591 spin_lock(&cache->lock);
7592 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7593 cache->bytes_super - btrfs_block_group_used(&cache->item);
7594 sinfo->bytes_readonly -= num_bytes;
7595 cache->ro = 0;
7596 spin_unlock(&cache->lock);
7597 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007598}
7599
Josef Bacikba1bf482009-09-11 16:11:19 -04007600/*
7601 * checks to see if its even possible to relocate this block group.
7602 *
7603 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7604 * ok to go ahead and try.
7605 */
7606int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007607{
Zheng Yan1a40e232008-09-26 10:09:34 -04007608 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007609 struct btrfs_space_info *space_info;
7610 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7611 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007612 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007613 u64 dev_min = 1;
7614 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007615 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007616 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007617 int full = 0;
7618 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007619
Josef Bacikba1bf482009-09-11 16:11:19 -04007620 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007621
Josef Bacikba1bf482009-09-11 16:11:19 -04007622 /* odd, couldn't find the block group, leave it alone */
7623 if (!block_group)
7624 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007625
liubocdcb7252011-08-03 10:15:25 +00007626 min_free = btrfs_block_group_used(&block_group->item);
7627
Josef Bacikba1bf482009-09-11 16:11:19 -04007628 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007629 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007630 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007631
Josef Bacikba1bf482009-09-11 16:11:19 -04007632 space_info = block_group->space_info;
7633 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007634
Josef Bacikba1bf482009-09-11 16:11:19 -04007635 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007636
Josef Bacikba1bf482009-09-11 16:11:19 -04007637 /*
7638 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007639 * relocate it unless we're able to allocate a new chunk below.
7640 *
7641 * Otherwise, we need to make sure we have room in the space to handle
7642 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007643 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007644 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007645 (space_info->bytes_used + space_info->bytes_reserved +
7646 space_info->bytes_pinned + space_info->bytes_readonly +
7647 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007648 spin_unlock(&space_info->lock);
7649 goto out;
7650 }
7651 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007652
Josef Bacikba1bf482009-09-11 16:11:19 -04007653 /*
7654 * ok we don't have enough space, but maybe we have free space on our
7655 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007656 * alloc devices and guess if we have enough space. if this block
7657 * group is going to be restriped, run checks against the target
7658 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007659 */
7660 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007661
liubocdcb7252011-08-03 10:15:25 +00007662 /*
7663 * index:
7664 * 0: raid10
7665 * 1: raid1
7666 * 2: dup
7667 * 3: raid0
7668 * 4: single
7669 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007670 target = get_restripe_target(root->fs_info, block_group->flags);
7671 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007672 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007673 } else {
7674 /*
7675 * this is just a balance, so if we were marked as full
7676 * we know there is no space for a new chunk
7677 */
7678 if (full)
7679 goto out;
7680
7681 index = get_block_group_index(block_group);
7682 }
7683
Miao Xiee6ec7162013-01-17 05:38:51 +00007684 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00007685 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007686 /* Divide by 2 */
7687 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007688 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00007689 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00007690 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04007691 /* Multiply by 2 */
7692 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007693 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00007694 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007695 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007696 }
7697
Josef Bacikba1bf482009-09-11 16:11:19 -04007698 mutex_lock(&root->fs_info->chunk_mutex);
7699 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007700 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007701
Josef Bacikba1bf482009-09-11 16:11:19 -04007702 /*
7703 * check to make sure we can actually find a chunk with enough
7704 * space to fit our block group in.
7705 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007706 if (device->total_bytes > device->bytes_used + min_free &&
7707 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007708 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007709 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007710 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007711 dev_nr++;
7712
7713 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007714 break;
liubocdcb7252011-08-03 10:15:25 +00007715
Josef Bacikba1bf482009-09-11 16:11:19 -04007716 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007717 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007718 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007719 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007720out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007721 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007722 return ret;
7723}
7724
Christoph Hellwigb2950862008-12-02 09:54:17 -05007725static int find_first_block_group(struct btrfs_root *root,
7726 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007727{
Chris Mason925baed2008-06-25 16:01:30 -04007728 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007729 struct btrfs_key found_key;
7730 struct extent_buffer *leaf;
7731 int slot;
7732
7733 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7734 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007735 goto out;
7736
Chris Masond3977122009-01-05 21:25:51 -05007737 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007738 slot = path->slots[0];
7739 leaf = path->nodes[0];
7740 if (slot >= btrfs_header_nritems(leaf)) {
7741 ret = btrfs_next_leaf(root, path);
7742 if (ret == 0)
7743 continue;
7744 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007745 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007746 break;
7747 }
7748 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7749
7750 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007751 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7752 ret = 0;
7753 goto out;
7754 }
Chris Mason0b86a832008-03-24 15:01:56 -04007755 path->slots[0]++;
7756 }
Chris Mason925baed2008-06-25 16:01:30 -04007757out:
Chris Mason0b86a832008-03-24 15:01:56 -04007758 return ret;
7759}
7760
Josef Bacik0af3d002010-06-21 14:48:16 -04007761void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7762{
7763 struct btrfs_block_group_cache *block_group;
7764 u64 last = 0;
7765
7766 while (1) {
7767 struct inode *inode;
7768
7769 block_group = btrfs_lookup_first_block_group(info, last);
7770 while (block_group) {
7771 spin_lock(&block_group->lock);
7772 if (block_group->iref)
7773 break;
7774 spin_unlock(&block_group->lock);
7775 block_group = next_block_group(info->tree_root,
7776 block_group);
7777 }
7778 if (!block_group) {
7779 if (last == 0)
7780 break;
7781 last = 0;
7782 continue;
7783 }
7784
7785 inode = block_group->inode;
7786 block_group->iref = 0;
7787 block_group->inode = NULL;
7788 spin_unlock(&block_group->lock);
7789 iput(inode);
7790 last = block_group->key.objectid + block_group->key.offset;
7791 btrfs_put_block_group(block_group);
7792 }
7793}
7794
Zheng Yan1a40e232008-09-26 10:09:34 -04007795int btrfs_free_block_groups(struct btrfs_fs_info *info)
7796{
7797 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007798 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007799 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007800 struct rb_node *n;
7801
Yan Zheng11833d62009-09-11 16:11:19 -04007802 down_write(&info->extent_commit_sem);
7803 while (!list_empty(&info->caching_block_groups)) {
7804 caching_ctl = list_entry(info->caching_block_groups.next,
7805 struct btrfs_caching_control, list);
7806 list_del(&caching_ctl->list);
7807 put_caching_control(caching_ctl);
7808 }
7809 up_write(&info->extent_commit_sem);
7810
Zheng Yan1a40e232008-09-26 10:09:34 -04007811 spin_lock(&info->block_group_cache_lock);
7812 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7813 block_group = rb_entry(n, struct btrfs_block_group_cache,
7814 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007815 rb_erase(&block_group->cache_node,
7816 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007817 spin_unlock(&info->block_group_cache_lock);
7818
Josef Bacik80eb2342008-10-29 14:49:05 -04007819 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007820 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007821 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007822
Josef Bacik817d52f2009-07-13 21:29:25 -04007823 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007824 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007825
Josef Bacik3c148742011-02-02 15:53:47 +00007826 /*
7827 * We haven't cached this block group, which means we could
7828 * possibly have excluded extents on this block group.
7829 */
7830 if (block_group->cached == BTRFS_CACHE_NO)
7831 free_excluded_extents(info->extent_root, block_group);
7832
Josef Bacik817d52f2009-07-13 21:29:25 -04007833 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007834 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007835
7836 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007837 }
7838 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007839
7840 /* now that all the block groups are freed, go through and
7841 * free all the space_info structs. This is only called during
7842 * the final stages of unmount, and so we know nobody is
7843 * using them. We call synchronize_rcu() once before we start,
7844 * just to be on the safe side.
7845 */
7846 synchronize_rcu();
7847
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007848 release_global_block_rsv(info);
7849
Chris Mason4184ea72009-03-10 12:39:20 -04007850 while(!list_empty(&info->space_info)) {
7851 space_info = list_entry(info->space_info.next,
7852 struct btrfs_space_info,
7853 list);
David Sterbab069e0c2013-02-08 21:28:17 +00007854 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
7855 if (space_info->bytes_pinned > 0 ||
7856 space_info->bytes_reserved > 0 ||
7857 space_info->bytes_may_use > 0) {
7858 WARN_ON(1);
7859 dump_space_info(space_info, 0, 0);
7860 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007861 }
Chris Mason4184ea72009-03-10 12:39:20 -04007862 list_del(&space_info->list);
7863 kfree(space_info);
7864 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007865 return 0;
7866}
7867
Yan, Zhengb742bb82010-05-16 10:46:24 -04007868static void __link_block_group(struct btrfs_space_info *space_info,
7869 struct btrfs_block_group_cache *cache)
7870{
7871 int index = get_block_group_index(cache);
7872
7873 down_write(&space_info->groups_sem);
7874 list_add_tail(&cache->list, &space_info->block_groups[index]);
7875 up_write(&space_info->groups_sem);
7876}
7877
Chris Mason9078a3e2007-04-26 16:46:15 -04007878int btrfs_read_block_groups(struct btrfs_root *root)
7879{
7880 struct btrfs_path *path;
7881 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007882 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007883 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007884 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007885 struct btrfs_key key;
7886 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007887 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007888 int need_clear = 0;
7889 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007890
Chris Masonbe744172007-05-06 10:15:01 -04007891 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007892 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007893 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007894 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007895 path = btrfs_alloc_path();
7896 if (!path)
7897 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007898 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007899
David Sterba6c417612011-04-13 15:41:04 +02007900 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007901 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02007902 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04007903 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007904 if (btrfs_test_opt(root, CLEAR_CACHE))
7905 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007906
Chris Masond3977122009-01-05 21:25:51 -05007907 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007908 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007909 if (ret > 0)
7910 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007911 if (ret != 0)
7912 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007913 leaf = path->nodes[0];
7914 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007915 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007916 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007917 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007918 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007919 }
Li Zefan34d52cb2011-03-29 13:46:06 +08007920 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7921 GFP_NOFS);
7922 if (!cache->free_space_ctl) {
7923 kfree(cache);
7924 ret = -ENOMEM;
7925 goto error;
7926 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007927
Yan Zhengd2fb3432008-12-11 16:30:39 -05007928 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007929 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007930 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007931 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007932 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007933
Liu Bocf7c1ef2012-07-06 03:31:34 -06007934 if (need_clear) {
7935 /*
7936 * When we mount with old space cache, we need to
7937 * set BTRFS_DC_CLEAR and set dirty flag.
7938 *
7939 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7940 * truncate the old free space cache inode and
7941 * setup a new one.
7942 * b) Setting 'dirty flag' makes sure that we flush
7943 * the new space cache info onto disk.
7944 */
Josef Bacik0af3d002010-06-21 14:48:16 -04007945 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007946 if (btrfs_test_opt(root, SPACE_CACHE))
7947 cache->dirty = 1;
7948 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007949
Chris Mason5f39d392007-10-15 16:14:19 -04007950 read_extent_buffer(leaf, &cache->item,
7951 btrfs_item_ptr_offset(leaf, path->slots[0]),
7952 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007953 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007954
Chris Mason9078a3e2007-04-26 16:46:15 -04007955 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007956 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007957 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007958 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05007959 cache->full_stripe_len = btrfs_full_stripe_len(root,
7960 &root->fs_info->mapping_tree,
7961 found_key.objectid);
Li Zefan34d52cb2011-03-29 13:46:06 +08007962 btrfs_init_free_space_ctl(cache);
7963
Josef Bacik817d52f2009-07-13 21:29:25 -04007964 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007965 * We need to exclude the super stripes now so that the space
7966 * info has super bytes accounted for, otherwise we'll think
7967 * we have more space than we actually do.
7968 */
7969 exclude_super_stripes(root, cache);
7970
7971 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007972 * check for two cases, either we are full, and therefore
7973 * don't need to bother with the caching work since we won't
7974 * find any space, or we are empty, and we can just add all
7975 * the space in and be done with it. This saves us _alot_ of
7976 * time, particularly in the full case.
7977 */
7978 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007979 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007980 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007981 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007982 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007983 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007984 cache->cached = BTRFS_CACHE_FINISHED;
7985 add_new_free_space(cache, root->fs_info,
7986 found_key.objectid,
7987 found_key.objectid +
7988 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007989 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007990 }
Chris Mason96b51792007-10-15 16:15:19 -04007991
Chris Mason6324fbf2008-03-24 15:01:59 -04007992 ret = update_space_info(info, cache->flags, found_key.offset,
7993 btrfs_block_group_used(&cache->item),
7994 &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007995 BUG_ON(ret); /* -ENOMEM */
Chris Mason6324fbf2008-03-24 15:01:59 -04007996 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007997 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007998 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007999 spin_unlock(&cache->space_info->lock);
8000
Yan, Zhengb742bb82010-05-16 10:46:24 -04008001 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008002
Josef Bacik0f9dd462008-09-23 13:14:11 -04008003 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008004 BUG_ON(ret); /* Logic error */
Chris Mason75ccf472008-09-30 19:24:06 -04008005
8006 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008007 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008008 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008009 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008010
8011 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8012 if (!(get_alloc_profile(root, space_info->flags) &
8013 (BTRFS_BLOCK_GROUP_RAID10 |
8014 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008015 BTRFS_BLOCK_GROUP_RAID5 |
8016 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008017 BTRFS_BLOCK_GROUP_DUP)))
8018 continue;
8019 /*
8020 * avoid allocating from un-mirrored block group if there are
8021 * mirrored block groups.
8022 */
8023 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008024 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008025 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008026 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008027 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008028
8029 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008030 ret = 0;
8031error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008032 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008033 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008034}
Chris Mason6324fbf2008-03-24 15:01:59 -04008035
Josef Bacikea658ba2012-09-11 16:57:25 -04008036void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8037 struct btrfs_root *root)
8038{
8039 struct btrfs_block_group_cache *block_group, *tmp;
8040 struct btrfs_root *extent_root = root->fs_info->extent_root;
8041 struct btrfs_block_group_item item;
8042 struct btrfs_key key;
8043 int ret = 0;
8044
8045 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8046 new_bg_list) {
8047 list_del_init(&block_group->new_bg_list);
8048
8049 if (ret)
8050 continue;
8051
8052 spin_lock(&block_group->lock);
8053 memcpy(&item, &block_group->item, sizeof(item));
8054 memcpy(&key, &block_group->key, sizeof(key));
8055 spin_unlock(&block_group->lock);
8056
8057 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8058 sizeof(item));
8059 if (ret)
8060 btrfs_abort_transaction(trans, extent_root, ret);
8061 }
8062}
8063
Chris Mason6324fbf2008-03-24 15:01:59 -04008064int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8065 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008066 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008067 u64 size)
8068{
8069 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008070 struct btrfs_root *extent_root;
8071 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008072
8073 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008074
Chris Mason12fcfd22009-03-24 10:24:20 -04008075 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008076
Chris Mason8f18cf12008-04-25 16:53:30 -04008077 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008078 if (!cache)
8079 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008080 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8081 GFP_NOFS);
8082 if (!cache->free_space_ctl) {
8083 kfree(cache);
8084 return -ENOMEM;
8085 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008086
Chris Masone17cade2008-04-15 15:41:47 -04008087 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008088 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008089 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008090 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008091 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008092 cache->full_stripe_len = btrfs_full_stripe_len(root,
8093 &root->fs_info->mapping_tree,
8094 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008095
Yan Zhengd2fb3432008-12-11 16:30:39 -05008096 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008097 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008098 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008099 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008100 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008101
Li Zefan34d52cb2011-03-29 13:46:06 +08008102 btrfs_init_free_space_ctl(cache);
8103
Chris Mason6324fbf2008-03-24 15:01:59 -04008104 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008105 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8106 cache->flags = type;
8107 btrfs_set_block_group_flags(&cache->item, type);
8108
Yan Zheng11833d62009-09-11 16:11:19 -04008109 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008110 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04008111 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04008112
Josef Bacik817d52f2009-07-13 21:29:25 -04008113 add_new_free_space(cache, root->fs_info, chunk_offset,
8114 chunk_offset + size);
8115
Yan Zheng11833d62009-09-11 16:11:19 -04008116 free_excluded_extents(root, cache);
8117
Chris Mason6324fbf2008-03-24 15:01:59 -04008118 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8119 &cache->space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008120 BUG_ON(ret); /* -ENOMEM */
Li Zefanc7c144d2011-12-07 10:39:22 +08008121 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008122
8123 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008124 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008125 spin_unlock(&cache->space_info->lock);
8126
Yan, Zhengb742bb82010-05-16 10:46:24 -04008127 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008128
Josef Bacik0f9dd462008-09-23 13:14:11 -04008129 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008130 BUG_ON(ret); /* Logic error */
Chris Masonc286ac42008-07-22 23:06:41 -04008131
Josef Bacikea658ba2012-09-11 16:57:25 -04008132 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008133
Chris Masond18a2c42008-04-04 15:40:00 -04008134 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008135
Chris Mason6324fbf2008-03-24 15:01:59 -04008136 return 0;
8137}
Zheng Yan1a40e232008-09-26 10:09:34 -04008138
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008139static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8140{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008141 u64 extra_flags = chunk_to_extended(flags) &
8142 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008143
Miao Xiede98ced2013-01-29 10:13:12 +00008144 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008145 if (flags & BTRFS_BLOCK_GROUP_DATA)
8146 fs_info->avail_data_alloc_bits &= ~extra_flags;
8147 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8148 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8149 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8150 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008151 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008152}
8153
Zheng Yan1a40e232008-09-26 10:09:34 -04008154int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8155 struct btrfs_root *root, u64 group_start)
8156{
8157 struct btrfs_path *path;
8158 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008159 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008160 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008161 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008162 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008163 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008164 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008165 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008166
Zheng Yan1a40e232008-09-26 10:09:34 -04008167 root = root->fs_info->extent_root;
8168
8169 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8170 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008171 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008172
liubo9f7c43c2011-03-07 02:13:33 +00008173 /*
8174 * Free the reserved super bytes from this block group before
8175 * remove it.
8176 */
8177 free_excluded_extents(root, block_group);
8178
Zheng Yan1a40e232008-09-26 10:09:34 -04008179 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008180 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008181 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8182 BTRFS_BLOCK_GROUP_RAID1 |
8183 BTRFS_BLOCK_GROUP_RAID10))
8184 factor = 2;
8185 else
8186 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008187
Chris Mason44fb5512009-06-04 15:34:51 -04008188 /* make sure this block group isn't part of an allocation cluster */
8189 cluster = &root->fs_info->data_alloc_cluster;
8190 spin_lock(&cluster->refill_lock);
8191 btrfs_return_cluster_to_free_space(block_group, cluster);
8192 spin_unlock(&cluster->refill_lock);
8193
8194 /*
8195 * make sure this block group isn't part of a metadata
8196 * allocation cluster
8197 */
8198 cluster = &root->fs_info->meta_alloc_cluster;
8199 spin_lock(&cluster->refill_lock);
8200 btrfs_return_cluster_to_free_space(block_group, cluster);
8201 spin_unlock(&cluster->refill_lock);
8202
Zheng Yan1a40e232008-09-26 10:09:34 -04008203 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008204 if (!path) {
8205 ret = -ENOMEM;
8206 goto out;
8207 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008208
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008209 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008210 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008211 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008212 if (ret) {
8213 btrfs_add_delayed_iput(inode);
8214 goto out;
8215 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008216 clear_nlink(inode);
8217 /* One for the block groups ref */
8218 spin_lock(&block_group->lock);
8219 if (block_group->iref) {
8220 block_group->iref = 0;
8221 block_group->inode = NULL;
8222 spin_unlock(&block_group->lock);
8223 iput(inode);
8224 } else {
8225 spin_unlock(&block_group->lock);
8226 }
8227 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008228 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008229 }
8230
8231 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8232 key.offset = block_group->key.objectid;
8233 key.type = 0;
8234
8235 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8236 if (ret < 0)
8237 goto out;
8238 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008239 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008240 if (ret == 0) {
8241 ret = btrfs_del_item(trans, tree_root, path);
8242 if (ret)
8243 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008244 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008245 }
8246
Yan Zheng3dfdb932009-01-21 10:49:16 -05008247 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008248 rb_erase(&block_group->cache_node,
8249 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008250
8251 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8252 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008253 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008254
Josef Bacik80eb2342008-10-29 14:49:05 -04008255 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008256 /*
8257 * we must use list_del_init so people can check to see if they
8258 * are still on the list after taking the semaphore
8259 */
8260 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008261 if (list_empty(&block_group->space_info->block_groups[index]))
8262 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008263 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008264
Josef Bacik817d52f2009-07-13 21:29:25 -04008265 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008266 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008267
8268 btrfs_remove_free_space_cache(block_group);
8269
Yan Zhengc146afa2008-11-12 14:34:12 -05008270 spin_lock(&block_group->space_info->lock);
8271 block_group->space_info->total_bytes -= block_group->key.offset;
8272 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008273 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008274 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008275
Josef Bacik0af3d002010-06-21 14:48:16 -04008276 memcpy(&key, &block_group->key, sizeof(key));
8277
Chris Mason283bb192009-07-24 16:30:55 -04008278 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008279
Chris Masonfa9c0d72009-04-03 09:47:43 -04008280 btrfs_put_block_group(block_group);
8281 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008282
8283 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8284 if (ret > 0)
8285 ret = -EIO;
8286 if (ret < 0)
8287 goto out;
8288
8289 ret = btrfs_del_item(trans, root, path);
8290out:
8291 btrfs_free_path(path);
8292 return ret;
8293}
liuboacce9522011-01-06 19:30:25 +08008294
liuboc59021f2011-03-07 02:13:14 +00008295int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8296{
8297 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008298 struct btrfs_super_block *disk_super;
8299 u64 features;
8300 u64 flags;
8301 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008302 int ret;
8303
David Sterba6c417612011-04-13 15:41:04 +02008304 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008305 if (!btrfs_super_root(disk_super))
8306 return 1;
liuboc59021f2011-03-07 02:13:14 +00008307
liubo1aba86d2011-04-08 08:44:37 +00008308 features = btrfs_super_incompat_flags(disk_super);
8309 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8310 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008311
liubo1aba86d2011-04-08 08:44:37 +00008312 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8313 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008314 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008315 goto out;
liuboc59021f2011-03-07 02:13:14 +00008316
liubo1aba86d2011-04-08 08:44:37 +00008317 if (mixed) {
8318 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8319 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8320 } else {
8321 flags = BTRFS_BLOCK_GROUP_METADATA;
8322 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8323 if (ret)
8324 goto out;
8325
8326 flags = BTRFS_BLOCK_GROUP_DATA;
8327 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8328 }
8329out:
liuboc59021f2011-03-07 02:13:14 +00008330 return ret;
8331}
8332
liuboacce9522011-01-06 19:30:25 +08008333int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8334{
8335 return unpin_extent_range(root, start, end);
8336}
8337
8338int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008339 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008340{
Li Dongyang5378e602011-03-24 10:24:27 +00008341 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008342}
Li Dongyangf7039b12011-03-24 10:24:28 +00008343
8344int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8345{
8346 struct btrfs_fs_info *fs_info = root->fs_info;
8347 struct btrfs_block_group_cache *cache = NULL;
8348 u64 group_trimmed;
8349 u64 start;
8350 u64 end;
8351 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008352 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008353 int ret = 0;
8354
Liu Bo2cac13e2012-02-09 18:17:41 +08008355 /*
8356 * try to trim all FS space, our block group may start from non-zero.
8357 */
8358 if (range->len == total_bytes)
8359 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8360 else
8361 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008362
8363 while (cache) {
8364 if (cache->key.objectid >= (range->start + range->len)) {
8365 btrfs_put_block_group(cache);
8366 break;
8367 }
8368
8369 start = max(range->start, cache->key.objectid);
8370 end = min(range->start + range->len,
8371 cache->key.objectid + cache->key.offset);
8372
8373 if (end - start >= range->minlen) {
8374 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008375 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008376 if (!ret)
8377 wait_block_group_cache_done(cache);
8378 }
8379 ret = btrfs_trim_block_group(cache,
8380 &group_trimmed,
8381 start,
8382 end,
8383 range->minlen);
8384
8385 trimmed += group_trimmed;
8386 if (ret) {
8387 btrfs_put_block_group(cache);
8388 break;
8389 }
8390 }
8391
8392 cache = next_block_group(fs_info->tree_root, cache);
8393 }
8394
8395 range->len = trimmed;
8396 return ret;
8397}