blob: c944b6e64e7a8e3747a4820f7c77cc8863447aa0 [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 Masonfa9c0d792009-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);
Josef Bacik835d9742013-03-19 12:13:25 -0400260 if (ret)
261 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000262 }
263
Josef Bacik817d52f2009-07-13 21:29:25 -0400264 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
265 bytenr = btrfs_sb_offset(i);
266 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
267 cache->key.objectid, bytenr,
268 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400269 if (ret)
270 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400271
Josef Bacik817d52f2009-07-13 21:29:25 -0400272 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400273 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400274 ret = add_excluded_extent(root, logical[nr],
275 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400276 if (ret) {
277 kfree(logical);
278 return ret;
279 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400280 }
Yan Zheng11833d62009-09-11 16:11:19 -0400281
Josef Bacik817d52f2009-07-13 21:29:25 -0400282 kfree(logical);
283 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400284 return 0;
285}
286
Yan Zheng11833d62009-09-11 16:11:19 -0400287static struct btrfs_caching_control *
288get_caching_control(struct btrfs_block_group_cache *cache)
289{
290 struct btrfs_caching_control *ctl;
291
292 spin_lock(&cache->lock);
293 if (cache->cached != BTRFS_CACHE_STARTED) {
294 spin_unlock(&cache->lock);
295 return NULL;
296 }
297
Josef Bacikdde5abe2010-09-16 16:17:03 -0400298 /* We're loading it the fast way, so we don't have a caching_ctl. */
299 if (!cache->caching_ctl) {
300 spin_unlock(&cache->lock);
301 return NULL;
302 }
303
Yan Zheng11833d62009-09-11 16:11:19 -0400304 ctl = cache->caching_ctl;
305 atomic_inc(&ctl->count);
306 spin_unlock(&cache->lock);
307 return ctl;
308}
309
310static void put_caching_control(struct btrfs_caching_control *ctl)
311{
312 if (atomic_dec_and_test(&ctl->count))
313 kfree(ctl);
314}
315
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316/*
317 * this is only called by cache_block_group, since we could have freed extents
318 * we need to check the pinned_extents for any extents that can't be used yet
319 * since their free space will be released as soon as the transaction commits.
320 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400321static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400322 struct btrfs_fs_info *info, u64 start, u64 end)
323{
Josef Bacik817d52f2009-07-13 21:29:25 -0400324 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400325 int ret;
326
327 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400328 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400329 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400330 EXTENT_DIRTY | EXTENT_UPTODATE,
331 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400332 if (ret)
333 break;
334
Yan, Zheng06b23312009-11-26 09:31:11 +0000335 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400336 start = extent_end + 1;
337 } else if (extent_start > start && extent_start < end) {
338 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400339 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500340 ret = btrfs_add_free_space(block_group, start,
341 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100342 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343 start = extent_end + 1;
344 } else {
345 break;
346 }
347 }
348
349 if (start < end) {
350 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400351 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500352 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100353 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400354 }
355
Josef Bacik817d52f2009-07-13 21:29:25 -0400356 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357}
358
Josef Bacikbab39bf2011-06-30 14:42:28 -0400359static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400360{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400361 struct btrfs_block_group_cache *block_group;
362 struct btrfs_fs_info *fs_info;
363 struct btrfs_caching_control *caching_ctl;
364 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400365 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400366 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400367 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400368 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400369 u64 last = 0;
370 u32 nritems;
371 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400372
Josef Bacikbab39bf2011-06-30 14:42:28 -0400373 caching_ctl = container_of(work, struct btrfs_caching_control, work);
374 block_group = caching_ctl->block_group;
375 fs_info = block_group->fs_info;
376 extent_root = fs_info->extent_root;
377
Chris Masone37c9e62007-05-09 20:13:14 -0400378 path = btrfs_alloc_path();
379 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400380 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400381
Josef Bacik817d52f2009-07-13 21:29:25 -0400382 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400383
Chris Mason5cd57b22008-06-25 16:01:30 -0400384 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400385 * We don't want to deadlock with somebody trying to allocate a new
386 * extent for the extent root while also trying to search the extent
387 * root to add free space. So we skip locking and search the commit
388 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400389 */
390 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400391 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400392 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400393
Yan Zhenge4404d62008-12-12 10:03:26 -0500394 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400395 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400396 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400397again:
Yan Zheng11833d62009-09-11 16:11:19 -0400398 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400399 /* need to make sure the commit_root doesn't disappear */
400 down_read(&fs_info->extent_commit_sem);
401
Yan Zheng11833d62009-09-11 16:11:19 -0400402 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400403 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400404 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500405
Yan Zheng11833d62009-09-11 16:11:19 -0400406 leaf = path->nodes[0];
407 nritems = btrfs_header_nritems(leaf);
408
Chris Masond3977122009-01-05 21:25:51 -0500409 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200410 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400411 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400412 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400413 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400414
Yan Zheng11833d62009-09-11 16:11:19 -0400415 if (path->slots[0] < nritems) {
416 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
417 } else {
418 ret = find_next_key(path, 0, &key);
419 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400420 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400421
Josef Bacik589d8ad2011-05-11 17:30:53 -0400422 if (need_resched() ||
423 btrfs_next_leaf(extent_root, path)) {
424 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400425 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400426 up_read(&fs_info->extent_commit_sem);
427 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400428 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400429 goto again;
430 }
431 leaf = path->nodes[0];
432 nritems = btrfs_header_nritems(leaf);
433 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400434 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400435
Yan Zheng11833d62009-09-11 16:11:19 -0400436 if (key.objectid < block_group->key.objectid) {
437 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400438 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400439 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400440
Chris Masone37c9e62007-05-09 20:13:14 -0400441 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400442 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400443 break;
Yan7d7d6062007-09-14 16:15:28 -0400444
Josef Bacik3173a182013-03-07 14:22:04 -0500445 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
446 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400447 total_found += add_new_free_space(block_group,
448 fs_info, last,
449 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500450 if (key.type == BTRFS_METADATA_ITEM_KEY)
451 last = key.objectid +
452 fs_info->tree_root->leafsize;
453 else
454 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400455
Yan Zheng11833d62009-09-11 16:11:19 -0400456 if (total_found > (1024 * 1024 * 2)) {
457 total_found = 0;
458 wake_up(&caching_ctl->wait);
459 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400460 }
Chris Masone37c9e62007-05-09 20:13:14 -0400461 path->slots[0]++;
462 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400463 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400464
465 total_found += add_new_free_space(block_group, fs_info, last,
466 block_group->key.objectid +
467 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400468 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400469
470 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400471 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400472 block_group->cached = BTRFS_CACHE_FINISHED;
473 spin_unlock(&block_group->lock);
474
Chris Mason54aa1f42007-06-22 14:16:25 -0400475err:
Chris Masone37c9e62007-05-09 20:13:14 -0400476 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400477 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400478
Yan Zheng11833d62009-09-11 16:11:19 -0400479 free_excluded_extents(extent_root, block_group);
480
481 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400482out:
Yan Zheng11833d62009-09-11 16:11:19 -0400483 wake_up(&caching_ctl->wait);
484
485 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000486 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400487}
488
Josef Bacik9d66e232010-08-25 16:54:15 -0400489static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400490 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400491{
Josef Bacik291c7d22011-11-14 13:52:14 -0500492 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400493 struct btrfs_fs_info *fs_info = cache->fs_info;
494 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400495 int ret = 0;
496
Josef Bacik291c7d22011-11-14 13:52:14 -0500497 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100498 if (!caching_ctl)
499 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500500
501 INIT_LIST_HEAD(&caching_ctl->list);
502 mutex_init(&caching_ctl->mutex);
503 init_waitqueue_head(&caching_ctl->wait);
504 caching_ctl->block_group = cache;
505 caching_ctl->progress = cache->key.objectid;
506 atomic_set(&caching_ctl->count, 1);
507 caching_ctl->work.func = caching_thread;
508
509 spin_lock(&cache->lock);
510 /*
511 * This should be a rare occasion, but this could happen I think in the
512 * case where one thread starts to load the space cache info, and then
513 * some other thread starts a transaction commit which tries to do an
514 * allocation while the other thread is still loading the space cache
515 * info. The previous loop should have kept us from choosing this block
516 * group, but if we've moved to the state where we will wait on caching
517 * block groups we need to first check if we're doing a fast load here,
518 * so we can wait for it to finish, otherwise we could end up allocating
519 * from a block group who's cache gets evicted for one reason or
520 * another.
521 */
522 while (cache->cached == BTRFS_CACHE_FAST) {
523 struct btrfs_caching_control *ctl;
524
525 ctl = cache->caching_ctl;
526 atomic_inc(&ctl->count);
527 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
528 spin_unlock(&cache->lock);
529
530 schedule();
531
532 finish_wait(&ctl->wait, &wait);
533 put_caching_control(ctl);
534 spin_lock(&cache->lock);
535 }
536
537 if (cache->cached != BTRFS_CACHE_NO) {
538 spin_unlock(&cache->lock);
539 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400540 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500541 }
542 WARN_ON(cache->caching_ctl);
543 cache->caching_ctl = caching_ctl;
544 cache->cached = BTRFS_CACHE_FAST;
545 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400546
Josef Bacikd53ba472012-04-12 16:03:57 -0400547 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400548 ret = load_free_space_cache(fs_info, cache);
549
550 spin_lock(&cache->lock);
551 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500552 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400553 cache->cached = BTRFS_CACHE_FINISHED;
554 cache->last_byte_to_unpin = (u64)-1;
555 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500556 if (load_cache_only) {
557 cache->caching_ctl = NULL;
558 cache->cached = BTRFS_CACHE_NO;
559 } else {
560 cache->cached = BTRFS_CACHE_STARTED;
561 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400562 }
563 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500564 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000565 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500566 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000567 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400568 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000569 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500570 } else {
571 /*
572 * We are not going to do the fast caching, set cached to the
573 * appropriate value and wakeup any waiters.
574 */
575 spin_lock(&cache->lock);
576 if (load_cache_only) {
577 cache->caching_ctl = NULL;
578 cache->cached = BTRFS_CACHE_NO;
579 } else {
580 cache->cached = BTRFS_CACHE_STARTED;
581 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400582 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500583 wake_up(&caching_ctl->wait);
584 }
585
586 if (load_cache_only) {
587 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400588 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400589 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400590
Yan Zheng11833d62009-09-11 16:11:19 -0400591 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500592 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400593 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
594 up_write(&fs_info->extent_commit_sem);
595
Josef Bacik11dfe352009-11-13 20:12:59 +0000596 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400597
Josef Bacikbab39bf2011-06-30 14:42:28 -0400598 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400599
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400600 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400601}
602
Josef Bacik0f9dd462008-09-23 13:14:11 -0400603/*
604 * return the block group that starts at or after bytenr
605 */
Chris Masond3977122009-01-05 21:25:51 -0500606static struct btrfs_block_group_cache *
607btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400608{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400609 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400610
Josef Bacik0f9dd462008-09-23 13:14:11 -0400611 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400612
Josef Bacik0f9dd462008-09-23 13:14:11 -0400613 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400614}
615
Josef Bacik0f9dd462008-09-23 13:14:11 -0400616/*
Sankar P9f556842009-05-14 13:52:22 -0400617 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400618 */
Chris Masond3977122009-01-05 21:25:51 -0500619struct btrfs_block_group_cache *btrfs_lookup_block_group(
620 struct btrfs_fs_info *info,
621 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400622{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400623 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400624
Josef Bacik0f9dd462008-09-23 13:14:11 -0400625 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400626
Josef Bacik0f9dd462008-09-23 13:14:11 -0400627 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400628}
Chris Mason0b86a832008-03-24 15:01:56 -0400629
Josef Bacik0f9dd462008-09-23 13:14:11 -0400630static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
631 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400632{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400633 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400634 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400635
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200636 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400637
Chris Mason4184ea72009-03-10 12:39:20 -0400638 rcu_read_lock();
639 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400640 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400641 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400642 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400643 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400644 }
Chris Mason4184ea72009-03-10 12:39:20 -0400645 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400646 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400647}
648
Chris Mason4184ea72009-03-10 12:39:20 -0400649/*
650 * after adding space to the filesystem, we need to clear the full flags
651 * on all the space infos.
652 */
653void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
654{
655 struct list_head *head = &info->space_info;
656 struct btrfs_space_info *found;
657
658 rcu_read_lock();
659 list_for_each_entry_rcu(found, head, list)
660 found->full = 0;
661 rcu_read_unlock();
662}
663
Yan Zhengd2fb3432008-12-11 16:30:39 -0500664u64 btrfs_find_block_group(struct btrfs_root *root,
665 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400666{
Chris Mason96b51792007-10-15 16:15:19 -0400667 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400668 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500669 u64 last = max(search_hint, search_start);
670 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400671 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500672 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400673 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400674again:
Zheng Yane8569812008-09-26 10:05:48 -0400675 while (1) {
676 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400677 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400678 break;
Chris Mason96b51792007-10-15 16:15:19 -0400679
Chris Masonc286ac42008-07-22 23:06:41 -0400680 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400681 last = cache->key.objectid + cache->key.offset;
682 used = btrfs_block_group_used(&cache->item);
683
Yan Zhengd2fb3432008-12-11 16:30:39 -0500684 if ((full_search || !cache->ro) &&
685 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400686 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500687 div_factor(cache->key.offset, factor)) {
688 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400689 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400690 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400691 goto found;
692 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400693 }
Chris Masonc286ac42008-07-22 23:06:41 -0400694 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400695 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400696 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400697 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400698 if (!wrapped) {
699 last = search_start;
700 wrapped = 1;
701 goto again;
702 }
703 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400704 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400705 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400706 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400707 goto again;
708 }
Chris Masonbe744172007-05-06 10:15:01 -0400709found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500710 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400711}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400712
Chris Masone02119d2008-09-05 16:13:11 -0400713/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400714int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400715{
716 int ret;
717 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400718 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400719
Zheng Yan31840ae2008-09-23 13:14:14 -0400720 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700721 if (!path)
722 return -ENOMEM;
723
Chris Masone02119d2008-09-05 16:13:11 -0400724 key.objectid = start;
725 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500726 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400727 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
728 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500729 if (ret > 0) {
730 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
731 if (key.objectid == start &&
732 key.type == BTRFS_METADATA_ITEM_KEY)
733 ret = 0;
734 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400735 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500736 return ret;
737}
738
Chris Masond8d5f3e2007-12-11 12:42:00 -0500739/*
Josef Bacik3173a182013-03-07 14:22:04 -0500740 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400741 *
742 * the head node for delayed ref is used to store the sum of all the
743 * reference count modifications queued up in the rbtree. the head
744 * node may also store the extent flags to set. This way you can check
745 * to see what the reference count and extent flags would be if all of
746 * the delayed refs are not processed.
747 */
748int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
749 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500750 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400751{
752 struct btrfs_delayed_ref_head *head;
753 struct btrfs_delayed_ref_root *delayed_refs;
754 struct btrfs_path *path;
755 struct btrfs_extent_item *ei;
756 struct extent_buffer *leaf;
757 struct btrfs_key key;
758 u32 item_size;
759 u64 num_refs;
760 u64 extent_flags;
761 int ret;
762
Josef Bacik3173a182013-03-07 14:22:04 -0500763 /*
764 * If we don't have skinny metadata, don't bother doing anything
765 * different
766 */
767 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
768 offset = root->leafsize;
769 metadata = 0;
770 }
771
Yan, Zhenga22285a2010-05-16 10:48:46 -0400772 path = btrfs_alloc_path();
773 if (!path)
774 return -ENOMEM;
775
Josef Bacik3173a182013-03-07 14:22:04 -0500776 if (metadata) {
777 key.objectid = bytenr;
778 key.type = BTRFS_METADATA_ITEM_KEY;
779 key.offset = offset;
780 } else {
781 key.objectid = bytenr;
782 key.type = BTRFS_EXTENT_ITEM_KEY;
783 key.offset = offset;
784 }
785
Yan, Zhenga22285a2010-05-16 10:48:46 -0400786 if (!trans) {
787 path->skip_locking = 1;
788 path->search_commit_root = 1;
789 }
790again:
791 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
792 &key, path, 0, 0);
793 if (ret < 0)
794 goto out_free;
795
Josef Bacik3173a182013-03-07 14:22:04 -0500796 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
797 key.type = BTRFS_EXTENT_ITEM_KEY;
798 key.offset = root->leafsize;
799 btrfs_release_path(path);
800 goto again;
801 }
802
Yan, Zhenga22285a2010-05-16 10:48:46 -0400803 if (ret == 0) {
804 leaf = path->nodes[0];
805 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
806 if (item_size >= sizeof(*ei)) {
807 ei = btrfs_item_ptr(leaf, path->slots[0],
808 struct btrfs_extent_item);
809 num_refs = btrfs_extent_refs(leaf, ei);
810 extent_flags = btrfs_extent_flags(leaf, ei);
811 } else {
812#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
813 struct btrfs_extent_item_v0 *ei0;
814 BUG_ON(item_size != sizeof(*ei0));
815 ei0 = btrfs_item_ptr(leaf, path->slots[0],
816 struct btrfs_extent_item_v0);
817 num_refs = btrfs_extent_refs_v0(leaf, ei0);
818 /* FIXME: this isn't correct for data */
819 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
820#else
821 BUG();
822#endif
823 }
824 BUG_ON(num_refs == 0);
825 } else {
826 num_refs = 0;
827 extent_flags = 0;
828 ret = 0;
829 }
830
831 if (!trans)
832 goto out;
833
834 delayed_refs = &trans->transaction->delayed_refs;
835 spin_lock(&delayed_refs->lock);
836 head = btrfs_find_delayed_ref_head(trans, bytenr);
837 if (head) {
838 if (!mutex_trylock(&head->mutex)) {
839 atomic_inc(&head->node.refs);
840 spin_unlock(&delayed_refs->lock);
841
David Sterbab3b4aa72011-04-21 01:20:15 +0200842 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400843
David Sterba8cc33e52011-05-02 15:29:25 +0200844 /*
845 * Mutex was contended, block until it's released and try
846 * again
847 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400848 mutex_lock(&head->mutex);
849 mutex_unlock(&head->mutex);
850 btrfs_put_delayed_ref(&head->node);
851 goto again;
852 }
853 if (head->extent_op && head->extent_op->update_flags)
854 extent_flags |= head->extent_op->flags_to_set;
855 else
856 BUG_ON(num_refs == 0);
857
858 num_refs += head->node.ref_mod;
859 mutex_unlock(&head->mutex);
860 }
861 spin_unlock(&delayed_refs->lock);
862out:
863 WARN_ON(num_refs == 0);
864 if (refs)
865 *refs = num_refs;
866 if (flags)
867 *flags = extent_flags;
868out_free:
869 btrfs_free_path(path);
870 return ret;
871}
872
873/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500874 * Back reference rules. Back refs have three main goals:
875 *
876 * 1) differentiate between all holders of references to an extent so that
877 * when a reference is dropped we can make sure it was a valid reference
878 * before freeing the extent.
879 *
880 * 2) Provide enough information to quickly find the holders of an extent
881 * if we notice a given block is corrupted or bad.
882 *
883 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
884 * maintenance. This is actually the same as #2, but with a slightly
885 * different use case.
886 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400887 * There are two kinds of back refs. The implicit back refs is optimized
888 * for pointers in non-shared tree blocks. For a given pointer in a block,
889 * back refs of this kind provide information about the block's owner tree
890 * and the pointer's key. These information allow us to find the block by
891 * b-tree searching. The full back refs is for pointers in tree blocks not
892 * referenced by their owner trees. The location of tree block is recorded
893 * in the back refs. Actually the full back refs is generic, and can be
894 * used in all cases the implicit back refs is used. The major shortcoming
895 * of the full back refs is its overhead. Every time a tree block gets
896 * COWed, we have to update back refs entry for all pointers in it.
897 *
898 * For a newly allocated tree block, we use implicit back refs for
899 * pointers in it. This means most tree related operations only involve
900 * implicit back refs. For a tree block created in old transaction, the
901 * only way to drop a reference to it is COW it. So we can detect the
902 * event that tree block loses its owner tree's reference and do the
903 * back refs conversion.
904 *
905 * When a tree block is COW'd through a tree, there are four cases:
906 *
907 * The reference count of the block is one and the tree is the block's
908 * owner tree. Nothing to do in this case.
909 *
910 * The reference count of the block is one and the tree is not the
911 * block's owner tree. In this case, full back refs is used for pointers
912 * in the block. Remove these full back refs, add implicit back refs for
913 * every pointers in the new block.
914 *
915 * The reference count of the block is greater than one and the tree is
916 * the block's owner tree. In this case, implicit back refs is used for
917 * pointers in the block. Add full back refs for every pointers in the
918 * block, increase lower level extents' reference counts. The original
919 * implicit back refs are entailed to the new block.
920 *
921 * The reference count of the block is greater than one and the tree is
922 * not the block's owner tree. Add implicit back refs for every pointer in
923 * the new block, increase lower level extents' reference count.
924 *
925 * Back Reference Key composing:
926 *
927 * The key objectid corresponds to the first byte in the extent,
928 * The key type is used to differentiate between types of back refs.
929 * There are different meanings of the key offset for different types
930 * of back refs.
931 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500932 * File extents can be referenced by:
933 *
934 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400935 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500936 * - different offsets inside a file (bookend extents in file.c)
937 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400938 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500939 *
940 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500941 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942 * - original offset in the file
943 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400944 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 * The key offset for the implicit back refs is hash of the first
946 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500947 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400948 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500949 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500951 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952 * The key offset for the implicit back refs is the first byte of
953 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500954 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400955 * When a file extent is allocated, The implicit back refs is used.
956 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500957 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400958 * (root_key.objectid, inode objectid, offset in file, 1)
959 *
960 * When a file extent is removed file truncation, we find the
961 * corresponding implicit back refs and check the following fields:
962 *
963 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500964 *
965 * Btree extents can be referenced by:
966 *
967 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500968 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 * Both the implicit back refs and the full back refs for tree blocks
970 * only consist of key. The key offset for the implicit back refs is
971 * objectid of block's owner tree. The key offset for the full back refs
972 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500973 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400974 * When implicit back refs is used, information about the lowest key and
975 * level of the tree block are required. These information are stored in
976 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500977 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400978
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400979#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
980static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
981 struct btrfs_root *root,
982 struct btrfs_path *path,
983 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500984{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 struct btrfs_extent_item *item;
986 struct btrfs_extent_item_v0 *ei0;
987 struct btrfs_extent_ref_v0 *ref0;
988 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400989 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990 struct btrfs_key key;
991 struct btrfs_key found_key;
992 u32 new_size = sizeof(*item);
993 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500994 int ret;
995
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400996 leaf = path->nodes[0];
997 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500998
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1000 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1001 struct btrfs_extent_item_v0);
1002 refs = btrfs_extent_refs_v0(leaf, ei0);
1003
1004 if (owner == (u64)-1) {
1005 while (1) {
1006 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1007 ret = btrfs_next_leaf(root, path);
1008 if (ret < 0)
1009 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001010 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001011 leaf = path->nodes[0];
1012 }
1013 btrfs_item_key_to_cpu(leaf, &found_key,
1014 path->slots[0]);
1015 BUG_ON(key.objectid != found_key.objectid);
1016 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1017 path->slots[0]++;
1018 continue;
1019 }
1020 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1021 struct btrfs_extent_ref_v0);
1022 owner = btrfs_ref_objectid_v0(leaf, ref0);
1023 break;
1024 }
1025 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001026 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027
1028 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1029 new_size += sizeof(*bi);
1030
1031 new_size -= sizeof(*ei0);
1032 ret = btrfs_search_slot(trans, root, &key, path,
1033 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001034 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001035 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001036 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001038 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039
1040 leaf = path->nodes[0];
1041 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1042 btrfs_set_extent_refs(leaf, item, refs);
1043 /* FIXME: get real generation */
1044 btrfs_set_extent_generation(leaf, item, 0);
1045 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1046 btrfs_set_extent_flags(leaf, item,
1047 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1048 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1049 bi = (struct btrfs_tree_block_info *)(item + 1);
1050 /* FIXME: get first key of the block */
1051 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1052 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1053 } else {
1054 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1055 }
1056 btrfs_mark_buffer_dirty(leaf);
1057 return 0;
1058}
1059#endif
1060
1061static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1062{
1063 u32 high_crc = ~(u32)0;
1064 u32 low_crc = ~(u32)0;
1065 __le64 lenum;
1066
1067 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001068 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001069 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001070 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001071 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001072 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001073
1074 return ((u64)high_crc << 31) ^ (u64)low_crc;
1075}
1076
1077static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1078 struct btrfs_extent_data_ref *ref)
1079{
1080 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1081 btrfs_extent_data_ref_objectid(leaf, ref),
1082 btrfs_extent_data_ref_offset(leaf, ref));
1083}
1084
1085static int match_extent_data_ref(struct extent_buffer *leaf,
1086 struct btrfs_extent_data_ref *ref,
1087 u64 root_objectid, u64 owner, u64 offset)
1088{
1089 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1090 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1091 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1092 return 0;
1093 return 1;
1094}
1095
1096static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1097 struct btrfs_root *root,
1098 struct btrfs_path *path,
1099 u64 bytenr, u64 parent,
1100 u64 root_objectid,
1101 u64 owner, u64 offset)
1102{
1103 struct btrfs_key key;
1104 struct btrfs_extent_data_ref *ref;
1105 struct extent_buffer *leaf;
1106 u32 nritems;
1107 int ret;
1108 int recow;
1109 int err = -ENOENT;
1110
1111 key.objectid = bytenr;
1112 if (parent) {
1113 key.type = BTRFS_SHARED_DATA_REF_KEY;
1114 key.offset = parent;
1115 } else {
1116 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1117 key.offset = hash_extent_data_ref(root_objectid,
1118 owner, offset);
1119 }
1120again:
1121 recow = 0;
1122 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1123 if (ret < 0) {
1124 err = ret;
1125 goto fail;
1126 }
1127
1128 if (parent) {
1129 if (!ret)
1130 return 0;
1131#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1132 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001133 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1135 if (ret < 0) {
1136 err = ret;
1137 goto fail;
1138 }
1139 if (!ret)
1140 return 0;
1141#endif
1142 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001143 }
1144
1145 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001146 nritems = btrfs_header_nritems(leaf);
1147 while (1) {
1148 if (path->slots[0] >= nritems) {
1149 ret = btrfs_next_leaf(root, path);
1150 if (ret < 0)
1151 err = ret;
1152 if (ret)
1153 goto fail;
1154
1155 leaf = path->nodes[0];
1156 nritems = btrfs_header_nritems(leaf);
1157 recow = 1;
1158 }
1159
1160 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1161 if (key.objectid != bytenr ||
1162 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1163 goto fail;
1164
1165 ref = btrfs_item_ptr(leaf, path->slots[0],
1166 struct btrfs_extent_data_ref);
1167
1168 if (match_extent_data_ref(leaf, ref, root_objectid,
1169 owner, offset)) {
1170 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001171 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001172 goto again;
1173 }
1174 err = 0;
1175 break;
1176 }
1177 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001178 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179fail:
1180 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001181}
1182
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1184 struct btrfs_root *root,
1185 struct btrfs_path *path,
1186 u64 bytenr, u64 parent,
1187 u64 root_objectid, u64 owner,
1188 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001189{
1190 struct btrfs_key key;
1191 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001192 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001193 u32 num_refs;
1194 int ret;
1195
1196 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001197 if (parent) {
1198 key.type = BTRFS_SHARED_DATA_REF_KEY;
1199 key.offset = parent;
1200 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001201 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001202 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1203 key.offset = hash_extent_data_ref(root_objectid,
1204 owner, offset);
1205 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001206 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001207
1208 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1209 if (ret && ret != -EEXIST)
1210 goto fail;
1211
1212 leaf = path->nodes[0];
1213 if (parent) {
1214 struct btrfs_shared_data_ref *ref;
1215 ref = btrfs_item_ptr(leaf, path->slots[0],
1216 struct btrfs_shared_data_ref);
1217 if (ret == 0) {
1218 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1219 } else {
1220 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1221 num_refs += refs_to_add;
1222 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1223 }
1224 } else {
1225 struct btrfs_extent_data_ref *ref;
1226 while (ret == -EEXIST) {
1227 ref = btrfs_item_ptr(leaf, path->slots[0],
1228 struct btrfs_extent_data_ref);
1229 if (match_extent_data_ref(leaf, ref, root_objectid,
1230 owner, offset))
1231 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001232 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001233 key.offset++;
1234 ret = btrfs_insert_empty_item(trans, root, path, &key,
1235 size);
1236 if (ret && ret != -EEXIST)
1237 goto fail;
1238
1239 leaf = path->nodes[0];
1240 }
1241 ref = btrfs_item_ptr(leaf, path->slots[0],
1242 struct btrfs_extent_data_ref);
1243 if (ret == 0) {
1244 btrfs_set_extent_data_ref_root(leaf, ref,
1245 root_objectid);
1246 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1247 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1248 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1249 } else {
1250 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1251 num_refs += refs_to_add;
1252 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1253 }
1254 }
1255 btrfs_mark_buffer_dirty(leaf);
1256 ret = 0;
1257fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001258 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001259 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001260}
1261
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001262static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1263 struct btrfs_root *root,
1264 struct btrfs_path *path,
1265 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001266{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001267 struct btrfs_key key;
1268 struct btrfs_extent_data_ref *ref1 = NULL;
1269 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001270 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001271 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001272 int ret = 0;
1273
1274 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001275 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1276
1277 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1278 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1279 struct btrfs_extent_data_ref);
1280 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1281 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1282 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1283 struct btrfs_shared_data_ref);
1284 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1285#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1286 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1287 struct btrfs_extent_ref_v0 *ref0;
1288 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1289 struct btrfs_extent_ref_v0);
1290 num_refs = btrfs_ref_count_v0(leaf, ref0);
1291#endif
1292 } else {
1293 BUG();
1294 }
1295
Chris Mason56bec292009-03-13 10:10:06 -04001296 BUG_ON(num_refs < refs_to_drop);
1297 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001298
Zheng Yan31840ae2008-09-23 13:14:14 -04001299 if (num_refs == 0) {
1300 ret = btrfs_del_item(trans, root, path);
1301 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001302 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1303 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1304 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1305 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1306#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1307 else {
1308 struct btrfs_extent_ref_v0 *ref0;
1309 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1310 struct btrfs_extent_ref_v0);
1311 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1312 }
1313#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001314 btrfs_mark_buffer_dirty(leaf);
1315 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316 return ret;
1317}
1318
1319static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1320 struct btrfs_path *path,
1321 struct btrfs_extent_inline_ref *iref)
1322{
1323 struct btrfs_key key;
1324 struct extent_buffer *leaf;
1325 struct btrfs_extent_data_ref *ref1;
1326 struct btrfs_shared_data_ref *ref2;
1327 u32 num_refs = 0;
1328
1329 leaf = path->nodes[0];
1330 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1331 if (iref) {
1332 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1333 BTRFS_EXTENT_DATA_REF_KEY) {
1334 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1335 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1336 } else {
1337 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1338 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1339 }
1340 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1341 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1342 struct btrfs_extent_data_ref);
1343 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1344 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1345 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1346 struct btrfs_shared_data_ref);
1347 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1348#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1349 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1350 struct btrfs_extent_ref_v0 *ref0;
1351 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1352 struct btrfs_extent_ref_v0);
1353 num_refs = btrfs_ref_count_v0(leaf, ref0);
1354#endif
1355 } else {
1356 WARN_ON(1);
1357 }
1358 return num_refs;
1359}
1360
1361static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1362 struct btrfs_root *root,
1363 struct btrfs_path *path,
1364 u64 bytenr, u64 parent,
1365 u64 root_objectid)
1366{
1367 struct btrfs_key key;
1368 int ret;
1369
1370 key.objectid = bytenr;
1371 if (parent) {
1372 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1373 key.offset = parent;
1374 } else {
1375 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1376 key.offset = root_objectid;
1377 }
1378
1379 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1380 if (ret > 0)
1381 ret = -ENOENT;
1382#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1383 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001384 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001385 key.type = BTRFS_EXTENT_REF_V0_KEY;
1386 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1387 if (ret > 0)
1388 ret = -ENOENT;
1389 }
1390#endif
1391 return ret;
1392}
1393
1394static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1395 struct btrfs_root *root,
1396 struct btrfs_path *path,
1397 u64 bytenr, u64 parent,
1398 u64 root_objectid)
1399{
1400 struct btrfs_key key;
1401 int ret;
1402
1403 key.objectid = bytenr;
1404 if (parent) {
1405 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1406 key.offset = parent;
1407 } else {
1408 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1409 key.offset = root_objectid;
1410 }
1411
1412 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001413 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001414 return ret;
1415}
1416
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001417static inline int extent_ref_type(u64 parent, u64 owner)
1418{
1419 int type;
1420 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1421 if (parent > 0)
1422 type = BTRFS_SHARED_BLOCK_REF_KEY;
1423 else
1424 type = BTRFS_TREE_BLOCK_REF_KEY;
1425 } else {
1426 if (parent > 0)
1427 type = BTRFS_SHARED_DATA_REF_KEY;
1428 else
1429 type = BTRFS_EXTENT_DATA_REF_KEY;
1430 }
1431 return type;
1432}
1433
Yan Zheng2c47e6052009-06-27 21:07:35 -04001434static int find_next_key(struct btrfs_path *path, int level,
1435 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001436
1437{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001438 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001439 if (!path->nodes[level])
1440 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441 if (path->slots[level] + 1 >=
1442 btrfs_header_nritems(path->nodes[level]))
1443 continue;
1444 if (level == 0)
1445 btrfs_item_key_to_cpu(path->nodes[level], key,
1446 path->slots[level] + 1);
1447 else
1448 btrfs_node_key_to_cpu(path->nodes[level], key,
1449 path->slots[level] + 1);
1450 return 0;
1451 }
1452 return 1;
1453}
1454
1455/*
1456 * look for inline back ref. if back ref is found, *ref_ret is set
1457 * to the address of inline back ref, and 0 is returned.
1458 *
1459 * if back ref isn't found, *ref_ret is set to the address where it
1460 * should be inserted, and -ENOENT is returned.
1461 *
1462 * if insert is true and there are too many inline back refs, the path
1463 * points to the extent item, and -EAGAIN is returned.
1464 *
1465 * NOTE: inline back refs are ordered in the same way that back ref
1466 * items in the tree are ordered.
1467 */
1468static noinline_for_stack
1469int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1470 struct btrfs_root *root,
1471 struct btrfs_path *path,
1472 struct btrfs_extent_inline_ref **ref_ret,
1473 u64 bytenr, u64 num_bytes,
1474 u64 parent, u64 root_objectid,
1475 u64 owner, u64 offset, int insert)
1476{
1477 struct btrfs_key key;
1478 struct extent_buffer *leaf;
1479 struct btrfs_extent_item *ei;
1480 struct btrfs_extent_inline_ref *iref;
1481 u64 flags;
1482 u64 item_size;
1483 unsigned long ptr;
1484 unsigned long end;
1485 int extra_size;
1486 int type;
1487 int want;
1488 int ret;
1489 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001490 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1491 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001492
1493 key.objectid = bytenr;
1494 key.type = BTRFS_EXTENT_ITEM_KEY;
1495 key.offset = num_bytes;
1496
1497 want = extent_ref_type(parent, owner);
1498 if (insert) {
1499 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001500 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001501 } else
1502 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001503
1504 /*
1505 * Owner is our parent level, so we can just add one to get the level
1506 * for the block we are interested in.
1507 */
1508 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1509 key.type = BTRFS_METADATA_ITEM_KEY;
1510 key.offset = owner;
1511 }
1512
1513again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001514 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1515 if (ret < 0) {
1516 err = ret;
1517 goto out;
1518 }
Josef Bacik3173a182013-03-07 14:22:04 -05001519
1520 /*
1521 * We may be a newly converted file system which still has the old fat
1522 * extent entries for metadata, so try and see if we have one of those.
1523 */
1524 if (ret > 0 && skinny_metadata) {
1525 skinny_metadata = false;
1526 if (path->slots[0]) {
1527 path->slots[0]--;
1528 btrfs_item_key_to_cpu(path->nodes[0], &key,
1529 path->slots[0]);
1530 if (key.objectid == bytenr &&
1531 key.type == BTRFS_EXTENT_ITEM_KEY &&
1532 key.offset == num_bytes)
1533 ret = 0;
1534 }
1535 if (ret) {
1536 key.type = BTRFS_EXTENT_ITEM_KEY;
1537 key.offset = num_bytes;
1538 btrfs_release_path(path);
1539 goto again;
1540 }
1541 }
1542
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001543 if (ret && !insert) {
1544 err = -ENOENT;
1545 goto out;
Josef Bacik492104c2013-03-08 15:41:02 -05001546 } else if (ret) {
1547 err = -EIO;
1548 WARN_ON(1);
1549 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001551
1552 leaf = path->nodes[0];
1553 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1554#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1555 if (item_size < sizeof(*ei)) {
1556 if (!insert) {
1557 err = -ENOENT;
1558 goto out;
1559 }
1560 ret = convert_extent_item_v0(trans, root, path, owner,
1561 extra_size);
1562 if (ret < 0) {
1563 err = ret;
1564 goto out;
1565 }
1566 leaf = path->nodes[0];
1567 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1568 }
1569#endif
1570 BUG_ON(item_size < sizeof(*ei));
1571
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001572 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1573 flags = btrfs_extent_flags(leaf, ei);
1574
1575 ptr = (unsigned long)(ei + 1);
1576 end = (unsigned long)ei + item_size;
1577
Josef Bacik3173a182013-03-07 14:22:04 -05001578 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001579 ptr += sizeof(struct btrfs_tree_block_info);
1580 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001581 }
1582
1583 err = -ENOENT;
1584 while (1) {
1585 if (ptr >= end) {
1586 WARN_ON(ptr > end);
1587 break;
1588 }
1589 iref = (struct btrfs_extent_inline_ref *)ptr;
1590 type = btrfs_extent_inline_ref_type(leaf, iref);
1591 if (want < type)
1592 break;
1593 if (want > type) {
1594 ptr += btrfs_extent_inline_ref_size(type);
1595 continue;
1596 }
1597
1598 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1599 struct btrfs_extent_data_ref *dref;
1600 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1601 if (match_extent_data_ref(leaf, dref, root_objectid,
1602 owner, offset)) {
1603 err = 0;
1604 break;
1605 }
1606 if (hash_extent_data_ref_item(leaf, dref) <
1607 hash_extent_data_ref(root_objectid, owner, offset))
1608 break;
1609 } else {
1610 u64 ref_offset;
1611 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1612 if (parent > 0) {
1613 if (parent == ref_offset) {
1614 err = 0;
1615 break;
1616 }
1617 if (ref_offset < parent)
1618 break;
1619 } else {
1620 if (root_objectid == ref_offset) {
1621 err = 0;
1622 break;
1623 }
1624 if (ref_offset < root_objectid)
1625 break;
1626 }
1627 }
1628 ptr += btrfs_extent_inline_ref_size(type);
1629 }
1630 if (err == -ENOENT && insert) {
1631 if (item_size + extra_size >=
1632 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1633 err = -EAGAIN;
1634 goto out;
1635 }
1636 /*
1637 * To add new inline back ref, we have to make sure
1638 * there is no corresponding back ref item.
1639 * For simplicity, we just do not add new inline back
1640 * ref if there is any kind of item for this block
1641 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001642 if (find_next_key(path, 0, &key) == 0 &&
1643 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001644 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001645 err = -EAGAIN;
1646 goto out;
1647 }
1648 }
1649 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1650out:
Yan Zheng85d41982009-06-11 08:51:10 -04001651 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001652 path->keep_locks = 0;
1653 btrfs_unlock_up_safe(path, 1);
1654 }
1655 return err;
1656}
1657
1658/*
1659 * helper to add new inline back ref
1660 */
1661static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001662void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1663 struct btrfs_root *root,
1664 struct btrfs_path *path,
1665 struct btrfs_extent_inline_ref *iref,
1666 u64 parent, u64 root_objectid,
1667 u64 owner, u64 offset, int refs_to_add,
1668 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001669{
1670 struct extent_buffer *leaf;
1671 struct btrfs_extent_item *ei;
1672 unsigned long ptr;
1673 unsigned long end;
1674 unsigned long item_offset;
1675 u64 refs;
1676 int size;
1677 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678
1679 leaf = path->nodes[0];
1680 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1681 item_offset = (unsigned long)iref - (unsigned long)ei;
1682
1683 type = extent_ref_type(parent, owner);
1684 size = btrfs_extent_inline_ref_size(type);
1685
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001686 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001687
1688 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1689 refs = btrfs_extent_refs(leaf, ei);
1690 refs += refs_to_add;
1691 btrfs_set_extent_refs(leaf, ei, refs);
1692 if (extent_op)
1693 __run_delayed_extent_op(extent_op, leaf, ei);
1694
1695 ptr = (unsigned long)ei + item_offset;
1696 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1697 if (ptr < end - size)
1698 memmove_extent_buffer(leaf, ptr + size, ptr,
1699 end - size - ptr);
1700
1701 iref = (struct btrfs_extent_inline_ref *)ptr;
1702 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1703 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1704 struct btrfs_extent_data_ref *dref;
1705 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1706 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1707 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1708 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1709 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1710 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1711 struct btrfs_shared_data_ref *sref;
1712 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1713 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1714 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1715 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1716 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1717 } else {
1718 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1719 }
1720 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001721}
1722
1723static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1724 struct btrfs_root *root,
1725 struct btrfs_path *path,
1726 struct btrfs_extent_inline_ref **ref_ret,
1727 u64 bytenr, u64 num_bytes, u64 parent,
1728 u64 root_objectid, u64 owner, u64 offset)
1729{
1730 int ret;
1731
1732 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1733 bytenr, num_bytes, parent,
1734 root_objectid, owner, offset, 0);
1735 if (ret != -ENOENT)
1736 return ret;
1737
David Sterbab3b4aa72011-04-21 01:20:15 +02001738 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001739 *ref_ret = NULL;
1740
1741 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1742 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1743 root_objectid);
1744 } else {
1745 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1746 root_objectid, owner, offset);
1747 }
1748 return ret;
1749}
1750
1751/*
1752 * helper to update/remove inline back ref
1753 */
1754static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001755void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001756 struct btrfs_path *path,
1757 struct btrfs_extent_inline_ref *iref,
1758 int refs_to_mod,
1759 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001760{
1761 struct extent_buffer *leaf;
1762 struct btrfs_extent_item *ei;
1763 struct btrfs_extent_data_ref *dref = NULL;
1764 struct btrfs_shared_data_ref *sref = NULL;
1765 unsigned long ptr;
1766 unsigned long end;
1767 u32 item_size;
1768 int size;
1769 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001770 u64 refs;
1771
1772 leaf = path->nodes[0];
1773 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1774 refs = btrfs_extent_refs(leaf, ei);
1775 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1776 refs += refs_to_mod;
1777 btrfs_set_extent_refs(leaf, ei, refs);
1778 if (extent_op)
1779 __run_delayed_extent_op(extent_op, leaf, ei);
1780
1781 type = btrfs_extent_inline_ref_type(leaf, iref);
1782
1783 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1784 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1785 refs = btrfs_extent_data_ref_count(leaf, dref);
1786 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1787 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1788 refs = btrfs_shared_data_ref_count(leaf, sref);
1789 } else {
1790 refs = 1;
1791 BUG_ON(refs_to_mod != -1);
1792 }
1793
1794 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1795 refs += refs_to_mod;
1796
1797 if (refs > 0) {
1798 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1799 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1800 else
1801 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1802 } else {
1803 size = btrfs_extent_inline_ref_size(type);
1804 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1805 ptr = (unsigned long)iref;
1806 end = (unsigned long)ei + item_size;
1807 if (ptr + size < end)
1808 memmove_extent_buffer(leaf, ptr, ptr + size,
1809 end - ptr - size);
1810 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001811 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001812 }
1813 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001814}
1815
1816static noinline_for_stack
1817int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1818 struct btrfs_root *root,
1819 struct btrfs_path *path,
1820 u64 bytenr, u64 num_bytes, u64 parent,
1821 u64 root_objectid, u64 owner,
1822 u64 offset, int refs_to_add,
1823 struct btrfs_delayed_extent_op *extent_op)
1824{
1825 struct btrfs_extent_inline_ref *iref;
1826 int ret;
1827
1828 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1829 bytenr, num_bytes, parent,
1830 root_objectid, owner, offset, 1);
1831 if (ret == 0) {
1832 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001833 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001834 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001835 } else if (ret == -ENOENT) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001836 setup_inline_extent_backref(trans, root, path, iref, parent,
1837 root_objectid, owner, offset,
1838 refs_to_add, extent_op);
1839 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001840 }
1841 return ret;
1842}
1843
1844static int insert_extent_backref(struct btrfs_trans_handle *trans,
1845 struct btrfs_root *root,
1846 struct btrfs_path *path,
1847 u64 bytenr, u64 parent, u64 root_objectid,
1848 u64 owner, u64 offset, int refs_to_add)
1849{
1850 int ret;
1851 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1852 BUG_ON(refs_to_add != 1);
1853 ret = insert_tree_block_ref(trans, root, path, bytenr,
1854 parent, root_objectid);
1855 } else {
1856 ret = insert_extent_data_ref(trans, root, path, bytenr,
1857 parent, root_objectid,
1858 owner, offset, refs_to_add);
1859 }
1860 return ret;
1861}
1862
1863static int remove_extent_backref(struct btrfs_trans_handle *trans,
1864 struct btrfs_root *root,
1865 struct btrfs_path *path,
1866 struct btrfs_extent_inline_ref *iref,
1867 int refs_to_drop, int is_data)
1868{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001869 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870
1871 BUG_ON(!is_data && refs_to_drop != 1);
1872 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001873 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001874 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001875 } else if (is_data) {
1876 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1877 } else {
1878 ret = btrfs_del_item(trans, root, path);
1879 }
1880 return ret;
1881}
1882
Li Dongyang5378e602011-03-24 10:24:27 +00001883static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001884 u64 start, u64 len)
1885{
Li Dongyang5378e602011-03-24 10:24:27 +00001886 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001887}
Chris Mason15916de2008-11-19 21:17:22 -05001888
Liu Hui1f3c79a2009-01-05 15:57:51 -05001889static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001890 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001891{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001892 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001893 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001894 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001895
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001896
Liu Hui1f3c79a2009-01-05 15:57:51 -05001897 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001898 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001899 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001900 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001901 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001902 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001903 int i;
1904
Liu Hui1f3c79a2009-01-05 15:57:51 -05001905
Jan Schmidta1d3c472011-08-04 17:15:33 +02001906 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001907 if (!stripe->dev->can_discard)
1908 continue;
1909
Li Dongyang5378e602011-03-24 10:24:27 +00001910 ret = btrfs_issue_discard(stripe->dev->bdev,
1911 stripe->physical,
1912 stripe->length);
1913 if (!ret)
1914 discarded_bytes += stripe->length;
1915 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001916 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001917
1918 /*
1919 * Just in case we get back EOPNOTSUPP for some reason,
1920 * just ignore the return value so we don't screw up
1921 * people calling discard_extent.
1922 */
1923 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001924 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001925 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001926 }
Li Dongyang5378e602011-03-24 10:24:27 +00001927
1928 if (actual_bytes)
1929 *actual_bytes = discarded_bytes;
1930
Liu Hui1f3c79a2009-01-05 15:57:51 -05001931
David Woodhouse53b381b2013-01-29 18:40:14 -05001932 if (ret == -EOPNOTSUPP)
1933 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001934 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001935}
1936
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001937/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001938int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1939 struct btrfs_root *root,
1940 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001941 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001942{
1943 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001944 struct btrfs_fs_info *fs_info = root->fs_info;
1945
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1947 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001948
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001949 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001950 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1951 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001953 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001954 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001955 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1956 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001957 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001958 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001959 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001960 return ret;
1961}
1962
Chris Mason925baed2008-06-25 16:01:30 -04001963static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001964 struct btrfs_root *root,
1965 u64 bytenr, u64 num_bytes,
1966 u64 parent, u64 root_objectid,
1967 u64 owner, u64 offset, int refs_to_add,
1968 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001969{
Chris Mason5caf2a02007-04-02 11:20:42 -04001970 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001971 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001972 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001973 u64 refs;
1974 int ret;
1975 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001976
Chris Mason5caf2a02007-04-02 11:20:42 -04001977 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001978 if (!path)
1979 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001980
Chris Mason3c12ac72008-04-21 12:01:38 -04001981 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001982 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001983 /* this will setup the path even if it fails to insert the back ref */
1984 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1985 path, bytenr, num_bytes, parent,
1986 root_objectid, owner, offset,
1987 refs_to_add, extent_op);
1988 if (ret == 0)
1989 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001990
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001991 if (ret != -EAGAIN) {
1992 err = ret;
1993 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001994 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001996 leaf = path->nodes[0];
1997 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1998 refs = btrfs_extent_refs(leaf, item);
1999 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2000 if (extent_op)
2001 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002002
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002003 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002004 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002005
Chris Mason3c12ac72008-04-21 12:01:38 -04002006 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002007 path->leave_spinning = 1;
2008
Chris Mason56bec292009-03-13 10:10:06 -04002009 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002010 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002011 path, bytenr, parent, root_objectid,
2012 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002013 if (ret)
2014 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002015out:
Chris Mason74493f72007-12-11 09:25:06 -05002016 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002017 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05002018}
2019
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002020static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2021 struct btrfs_root *root,
2022 struct btrfs_delayed_ref_node *node,
2023 struct btrfs_delayed_extent_op *extent_op,
2024 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002025{
Chris Mason56bec292009-03-13 10:10:06 -04002026 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002027 struct btrfs_delayed_data_ref *ref;
2028 struct btrfs_key ins;
2029 u64 parent = 0;
2030 u64 ref_root = 0;
2031 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002032
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002033 ins.objectid = node->bytenr;
2034 ins.offset = node->num_bytes;
2035 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037 ref = btrfs_delayed_node_to_data_ref(node);
2038 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2039 parent = ref->parent;
2040 else
2041 ref_root = ref->root;
2042
2043 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002044 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002045 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002046 ret = alloc_reserved_file_extent(trans, root,
2047 parent, ref_root, flags,
2048 ref->objectid, ref->offset,
2049 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002050 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2051 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2052 node->num_bytes, parent,
2053 ref_root, ref->objectid,
2054 ref->offset, node->ref_mod,
2055 extent_op);
2056 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2057 ret = __btrfs_free_extent(trans, root, node->bytenr,
2058 node->num_bytes, parent,
2059 ref_root, ref->objectid,
2060 ref->offset, node->ref_mod,
2061 extent_op);
2062 } else {
2063 BUG();
2064 }
Chris Mason56bec292009-03-13 10:10:06 -04002065 return ret;
2066}
2067
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002068static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2069 struct extent_buffer *leaf,
2070 struct btrfs_extent_item *ei)
2071{
2072 u64 flags = btrfs_extent_flags(leaf, ei);
2073 if (extent_op->update_flags) {
2074 flags |= extent_op->flags_to_set;
2075 btrfs_set_extent_flags(leaf, ei, flags);
2076 }
2077
2078 if (extent_op->update_key) {
2079 struct btrfs_tree_block_info *bi;
2080 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2081 bi = (struct btrfs_tree_block_info *)(ei + 1);
2082 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2083 }
2084}
2085
2086static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2087 struct btrfs_root *root,
2088 struct btrfs_delayed_ref_node *node,
2089 struct btrfs_delayed_extent_op *extent_op)
2090{
2091 struct btrfs_key key;
2092 struct btrfs_path *path;
2093 struct btrfs_extent_item *ei;
2094 struct extent_buffer *leaf;
2095 u32 item_size;
2096 int ret;
2097 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002098 int metadata = (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2099 node->type == BTRFS_SHARED_BLOCK_REF_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002101 if (trans->aborted)
2102 return 0;
2103
Josef Bacik3173a182013-03-07 14:22:04 -05002104 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2105 metadata = 0;
2106
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002107 path = btrfs_alloc_path();
2108 if (!path)
2109 return -ENOMEM;
2110
2111 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002112
Josef Bacik3173a182013-03-07 14:22:04 -05002113 if (metadata) {
2114 struct btrfs_delayed_tree_ref *tree_ref;
2115
2116 tree_ref = btrfs_delayed_node_to_tree_ref(node);
2117 key.type = BTRFS_METADATA_ITEM_KEY;
2118 key.offset = tree_ref->level;
2119 } else {
2120 key.type = BTRFS_EXTENT_ITEM_KEY;
2121 key.offset = node->num_bytes;
2122 }
2123
2124again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002125 path->reada = 1;
2126 path->leave_spinning = 1;
2127 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2128 path, 0, 1);
2129 if (ret < 0) {
2130 err = ret;
2131 goto out;
2132 }
2133 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002134 if (metadata) {
2135 btrfs_release_path(path);
2136 metadata = 0;
2137
2138 key.offset = node->num_bytes;
2139 key.type = BTRFS_EXTENT_ITEM_KEY;
2140 goto again;
2141 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002142 err = -EIO;
2143 goto out;
2144 }
2145
2146 leaf = path->nodes[0];
2147 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2148#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2149 if (item_size < sizeof(*ei)) {
2150 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2151 path, (u64)-1, 0);
2152 if (ret < 0) {
2153 err = ret;
2154 goto out;
2155 }
2156 leaf = path->nodes[0];
2157 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2158 }
2159#endif
2160 BUG_ON(item_size < sizeof(*ei));
2161 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2162 __run_delayed_extent_op(extent_op, leaf, ei);
2163
2164 btrfs_mark_buffer_dirty(leaf);
2165out:
2166 btrfs_free_path(path);
2167 return err;
2168}
2169
2170static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2171 struct btrfs_root *root,
2172 struct btrfs_delayed_ref_node *node,
2173 struct btrfs_delayed_extent_op *extent_op,
2174 int insert_reserved)
2175{
2176 int ret = 0;
2177 struct btrfs_delayed_tree_ref *ref;
2178 struct btrfs_key ins;
2179 u64 parent = 0;
2180 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002181 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2182 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002183
2184 ref = btrfs_delayed_node_to_tree_ref(node);
2185 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2186 parent = ref->parent;
2187 else
2188 ref_root = ref->root;
2189
Josef Bacik3173a182013-03-07 14:22:04 -05002190 ins.objectid = node->bytenr;
2191 if (skinny_metadata) {
2192 ins.offset = ref->level;
2193 ins.type = BTRFS_METADATA_ITEM_KEY;
2194 } else {
2195 ins.offset = node->num_bytes;
2196 ins.type = BTRFS_EXTENT_ITEM_KEY;
2197 }
2198
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002199 BUG_ON(node->ref_mod != 1);
2200 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002201 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202 ret = alloc_reserved_tree_block(trans, root,
2203 parent, ref_root,
2204 extent_op->flags_to_set,
2205 &extent_op->key,
2206 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002207 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2208 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2209 node->num_bytes, parent, ref_root,
2210 ref->level, 0, 1, extent_op);
2211 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2212 ret = __btrfs_free_extent(trans, root, node->bytenr,
2213 node->num_bytes, parent, ref_root,
2214 ref->level, 0, 1, extent_op);
2215 } else {
2216 BUG();
2217 }
2218 return ret;
2219}
2220
Chris Mason56bec292009-03-13 10:10:06 -04002221/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2223 struct btrfs_root *root,
2224 struct btrfs_delayed_ref_node *node,
2225 struct btrfs_delayed_extent_op *extent_op,
2226 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002227{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002228 int ret = 0;
2229
2230 if (trans->aborted)
2231 return 0;
2232
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002233 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002234 struct btrfs_delayed_ref_head *head;
2235 /*
2236 * we've hit the end of the chain and we were supposed
2237 * to insert this extent into the tree. But, it got
2238 * deleted before we ever needed to insert it, so all
2239 * we have to do is clean up the accounting
2240 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 BUG_ON(extent_op);
2242 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002243 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002244 btrfs_pin_extent(root, node->bytenr,
2245 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 if (head->is_data) {
2247 ret = btrfs_del_csums(trans, root,
2248 node->bytenr,
2249 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002250 }
Chris Mason56bec292009-03-13 10:10:06 -04002251 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002252 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002253 }
Josef Bacikeb099672009-02-12 09:27:38 -05002254
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002255 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2256 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2257 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2258 insert_reserved);
2259 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2260 node->type == BTRFS_SHARED_DATA_REF_KEY)
2261 ret = run_delayed_data_ref(trans, root, node, extent_op,
2262 insert_reserved);
2263 else
2264 BUG();
2265 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002266}
2267
Chris Mason56bec292009-03-13 10:10:06 -04002268static noinline struct btrfs_delayed_ref_node *
2269select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002270{
Chris Mason56bec292009-03-13 10:10:06 -04002271 struct rb_node *node;
2272 struct btrfs_delayed_ref_node *ref;
2273 int action = BTRFS_ADD_DELAYED_REF;
2274again:
2275 /*
2276 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2277 * this prevents ref count from going down to zero when
2278 * there still are pending delayed ref.
2279 */
2280 node = rb_prev(&head->node.rb_node);
2281 while (1) {
2282 if (!node)
2283 break;
2284 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2285 rb_node);
2286 if (ref->bytenr != head->node.bytenr)
2287 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002289 return ref;
2290 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002291 }
Chris Mason56bec292009-03-13 10:10:06 -04002292 if (action == BTRFS_ADD_DELAYED_REF) {
2293 action = BTRFS_DROP_DELAYED_REF;
2294 goto again;
2295 }
2296 return NULL;
2297}
2298
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002299/*
2300 * Returns 0 on success or if called with an already aborted transaction.
2301 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2302 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002303static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2304 struct btrfs_root *root,
2305 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002306{
Chris Mason56bec292009-03-13 10:10:06 -04002307 struct btrfs_delayed_ref_root *delayed_refs;
2308 struct btrfs_delayed_ref_node *ref;
2309 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002310 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002311 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002312 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002313 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002314 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002315
2316 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002317 while (1) {
2318 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002319 /* pick a new head ref from the cluster list */
2320 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002321 break;
Chris Mason56bec292009-03-13 10:10:06 -04002322
Chris Masonc3e69d52009-03-13 10:17:05 -04002323 locked_ref = list_entry(cluster->next,
2324 struct btrfs_delayed_ref_head, cluster);
2325
2326 /* grab the lock that says we are going to process
2327 * all the refs for this head */
2328 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2329
2330 /*
2331 * we may have dropped the spin lock to get the head
2332 * mutex lock, and that might have given someone else
2333 * time to free the head. If that's true, it has been
2334 * removed from our list and we can move on.
2335 */
2336 if (ret == -EAGAIN) {
2337 locked_ref = NULL;
2338 count++;
2339 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002340 }
2341 }
2342
2343 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002344 * We need to try and merge add/drops of the same ref since we
2345 * can run into issues with relocate dropping the implicit ref
2346 * and then it being added back again before the drop can
2347 * finish. If we merged anything we need to re-loop so we can
2348 * get a good ref.
2349 */
2350 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2351 locked_ref);
2352
2353 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002354 * locked_ref is the head node, so we have to go one
2355 * node back for any delayed ref updates
2356 */
2357 ref = select_delayed_ref(locked_ref);
2358
2359 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002360 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002361 /*
2362 * there are still refs with lower seq numbers in the
2363 * process of being added. Don't run this ref yet.
2364 */
2365 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002366 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002367 locked_ref = NULL;
2368 delayed_refs->num_heads_ready++;
2369 spin_unlock(&delayed_refs->lock);
2370 cond_resched();
2371 spin_lock(&delayed_refs->lock);
2372 continue;
2373 }
2374
2375 /*
Chris Mason56bec292009-03-13 10:10:06 -04002376 * record the must insert reserved flag before we
2377 * drop the spin lock.
2378 */
2379 must_insert_reserved = locked_ref->must_insert_reserved;
2380 locked_ref->must_insert_reserved = 0;
2381
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002382 extent_op = locked_ref->extent_op;
2383 locked_ref->extent_op = NULL;
2384
Chris Mason56bec292009-03-13 10:10:06 -04002385 if (!ref) {
2386 /* All delayed refs have been processed, Go ahead
2387 * and send the head node to run_one_delayed_ref,
2388 * so that any accounting fixes can happen
2389 */
2390 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002391
2392 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002393 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002394 extent_op = NULL;
2395 }
2396
2397 if (extent_op) {
2398 spin_unlock(&delayed_refs->lock);
2399
2400 ret = run_delayed_extent_op(trans, root,
2401 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002402 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002403
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002404 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002405 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002406 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002407 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002408 return ret;
2409 }
2410
Chris Mason203bf282012-01-06 15:23:57 -05002411 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002412 }
Chris Mason56bec292009-03-13 10:10:06 -04002413 }
2414
2415 ref->in_tree = 0;
2416 rb_erase(&ref->rb_node, &delayed_refs->root);
2417 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002418 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002419 /*
2420 * when we play the delayed ref, also correct the
2421 * ref_mod on head
2422 */
2423 switch (ref->action) {
2424 case BTRFS_ADD_DELAYED_REF:
2425 case BTRFS_ADD_DELAYED_EXTENT:
2426 locked_ref->node.ref_mod -= ref->ref_mod;
2427 break;
2428 case BTRFS_DROP_DELAYED_REF:
2429 locked_ref->node.ref_mod += ref->ref_mod;
2430 break;
2431 default:
2432 WARN_ON(1);
2433 }
2434 }
Chris Mason56bec292009-03-13 10:10:06 -04002435 spin_unlock(&delayed_refs->lock);
2436
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002437 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002438 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002439
Miao Xie78a61842012-11-21 02:21:28 +00002440 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002441 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002442 btrfs_delayed_ref_unlock(locked_ref);
2443 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002444 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002445 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002446 return ret;
2447 }
2448
Miao Xie093486c2012-12-19 08:10:10 +00002449 /*
2450 * If this node is a head, that means all the refs in this head
2451 * have been dealt with, and we will pick the next head to deal
2452 * with, so we must unlock the head and drop it from the cluster
2453 * list before we release it.
2454 */
2455 if (btrfs_delayed_ref_is_head(ref)) {
2456 list_del_init(&locked_ref->cluster);
2457 btrfs_delayed_ref_unlock(locked_ref);
2458 locked_ref = NULL;
2459 }
2460 btrfs_put_delayed_ref(ref);
2461 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002462next:
Chris Mason1887be62009-03-13 10:11:24 -04002463 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002464 spin_lock(&delayed_refs->lock);
2465 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002466 return count;
2467}
2468
Arne Jansen709c0482011-09-12 12:22:57 +02002469#ifdef SCRAMBLE_DELAYED_REFS
2470/*
2471 * Normally delayed refs get processed in ascending bytenr order. This
2472 * correlates in most cases to the order added. To expose dependencies on this
2473 * order, we start to process the tree in the middle instead of the beginning
2474 */
2475static u64 find_middle(struct rb_root *root)
2476{
2477 struct rb_node *n = root->rb_node;
2478 struct btrfs_delayed_ref_node *entry;
2479 int alt = 1;
2480 u64 middle;
2481 u64 first = 0, last = 0;
2482
2483 n = rb_first(root);
2484 if (n) {
2485 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2486 first = entry->bytenr;
2487 }
2488 n = rb_last(root);
2489 if (n) {
2490 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2491 last = entry->bytenr;
2492 }
2493 n = root->rb_node;
2494
2495 while (n) {
2496 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2497 WARN_ON(!entry->in_tree);
2498
2499 middle = entry->bytenr;
2500
2501 if (alt)
2502 n = n->rb_left;
2503 else
2504 n = n->rb_right;
2505
2506 alt = 1 - alt;
2507 }
2508 return middle;
2509}
2510#endif
2511
Arne Jansenbed92ea2012-06-28 18:03:02 +02002512int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2513 struct btrfs_fs_info *fs_info)
2514{
2515 struct qgroup_update *qgroup_update;
2516 int ret = 0;
2517
2518 if (list_empty(&trans->qgroup_ref_list) !=
2519 !trans->delayed_ref_elem.seq) {
2520 /* list without seq or seq without list */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002521 btrfs_err(fs_info,
2522 "qgroup accounting update error, list is%s empty, seq is %llu",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002523 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2524 trans->delayed_ref_elem.seq);
2525 BUG();
2526 }
2527
2528 if (!trans->delayed_ref_elem.seq)
2529 return 0;
2530
2531 while (!list_empty(&trans->qgroup_ref_list)) {
2532 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2533 struct qgroup_update, list);
2534 list_del(&qgroup_update->list);
2535 if (!ret)
2536 ret = btrfs_qgroup_account_ref(
2537 trans, fs_info, qgroup_update->node,
2538 qgroup_update->extent_op);
2539 kfree(qgroup_update);
2540 }
2541
2542 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2543
2544 return ret;
2545}
2546
Chris Masonbb721702013-01-29 18:44:12 -05002547static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2548 int count)
2549{
2550 int val = atomic_read(&delayed_refs->ref_seq);
2551
2552 if (val < seq || val >= seq + count)
2553 return 1;
2554 return 0;
2555}
2556
Chris Masonc3e69d52009-03-13 10:17:05 -04002557/*
2558 * this starts processing the delayed reference count updates and
2559 * extent insertions we have queued up so far. count can be
2560 * 0, which means to process everything in the tree at the start
2561 * of the run (but not newly added entries), or it can be some target
2562 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002563 *
2564 * Returns 0 on success or if called with an aborted transaction
2565 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002566 */
2567int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2568 struct btrfs_root *root, unsigned long count)
2569{
2570 struct rb_node *node;
2571 struct btrfs_delayed_ref_root *delayed_refs;
2572 struct btrfs_delayed_ref_node *ref;
2573 struct list_head cluster;
2574 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002575 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002576 int run_all = count == (unsigned long)-1;
2577 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002578 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002579
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002580 /* We'll clean this up in btrfs_cleanup_transaction */
2581 if (trans->aborted)
2582 return 0;
2583
Chris Masonc3e69d52009-03-13 10:17:05 -04002584 if (root == root->fs_info->extent_root)
2585 root = root->fs_info->tree_root;
2586
Jan Schmidtedf39272012-06-28 18:04:55 +02002587 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2588
Chris Masonc3e69d52009-03-13 10:17:05 -04002589 delayed_refs = &trans->transaction->delayed_refs;
2590 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002591 if (count == 0) {
2592 count = delayed_refs->num_entries * 2;
2593 run_most = 1;
2594 }
2595
2596 if (!run_all && !run_most) {
2597 int old;
2598 int seq = atomic_read(&delayed_refs->ref_seq);
2599
2600progress:
2601 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2602 if (old) {
2603 DEFINE_WAIT(__wait);
2604 if (delayed_refs->num_entries < 16348)
2605 return 0;
2606
2607 prepare_to_wait(&delayed_refs->wait, &__wait,
2608 TASK_UNINTERRUPTIBLE);
2609
2610 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2611 if (old) {
2612 schedule();
2613 finish_wait(&delayed_refs->wait, &__wait);
2614
2615 if (!refs_newer(delayed_refs, seq, 256))
2616 goto progress;
2617 else
2618 return 0;
2619 } else {
2620 finish_wait(&delayed_refs->wait, &__wait);
2621 goto again;
2622 }
2623 }
2624
2625 } else {
2626 atomic_inc(&delayed_refs->procs_running_refs);
2627 }
2628
Chris Masonc3e69d52009-03-13 10:17:05 -04002629again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002630 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002631 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002632
Arne Jansen709c0482011-09-12 12:22:57 +02002633#ifdef SCRAMBLE_DELAYED_REFS
2634 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2635#endif
2636
Chris Masonc3e69d52009-03-13 10:17:05 -04002637 while (1) {
2638 if (!(run_all || run_most) &&
2639 delayed_refs->num_heads_ready < 64)
2640 break;
2641
2642 /*
2643 * go find something we can process in the rbtree. We start at
2644 * the beginning of the tree, and then build a cluster
2645 * of refs to process starting at the first one we are able to
2646 * lock
2647 */
Jan Schmidta1686502011-12-12 16:10:07 +01002648 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002649 ret = btrfs_find_ref_cluster(trans, &cluster,
2650 delayed_refs->run_delayed_start);
2651 if (ret)
2652 break;
2653
2654 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002655 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002656 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657 spin_unlock(&delayed_refs->lock);
2658 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002659 atomic_dec(&delayed_refs->procs_running_refs);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002660 return ret;
2661 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002662
Chris Masonbb721702013-01-29 18:44:12 -05002663 atomic_add(ret, &delayed_refs->ref_seq);
2664
Chris Masonc3e69d52009-03-13 10:17:05 -04002665 count -= min_t(unsigned long, ret, count);
2666
2667 if (count == 0)
2668 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002669
Arne Jansen1fa11e22012-08-06 14:18:51 -06002670 if (delayed_start >= delayed_refs->run_delayed_start) {
2671 if (loops == 0) {
2672 /*
2673 * btrfs_find_ref_cluster looped. let's do one
2674 * more cycle. if we don't run any delayed ref
2675 * during that cycle (because we can't because
2676 * all of them are blocked), bail out.
2677 */
2678 loops = 1;
2679 } else {
2680 /*
2681 * no runnable refs left, stop trying
2682 */
2683 BUG_ON(run_all);
2684 break;
2685 }
2686 }
2687 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002688 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002689 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002690 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002691 }
2692
Chris Mason56bec292009-03-13 10:10:06 -04002693 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002694 if (!list_empty(&trans->new_bgs)) {
2695 spin_unlock(&delayed_refs->lock);
2696 btrfs_create_pending_block_groups(trans, root);
2697 spin_lock(&delayed_refs->lock);
2698 }
2699
Chris Mason56bec292009-03-13 10:10:06 -04002700 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002701 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002702 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002703 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002704
2705 while (node) {
2706 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2707 rb_node);
2708 if (btrfs_delayed_ref_is_head(ref)) {
2709 struct btrfs_delayed_ref_head *head;
2710
2711 head = btrfs_delayed_node_to_head(ref);
2712 atomic_inc(&ref->refs);
2713
2714 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002715 /*
2716 * Mutex was contended, block until it's
2717 * released and try again
2718 */
Chris Mason56bec292009-03-13 10:10:06 -04002719 mutex_lock(&head->mutex);
2720 mutex_unlock(&head->mutex);
2721
2722 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002723 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002724 goto again;
2725 }
2726 node = rb_next(node);
2727 }
2728 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002729 schedule_timeout(1);
2730 goto again;
2731 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002732out:
Chris Masonbb721702013-01-29 18:44:12 -05002733 atomic_dec(&delayed_refs->procs_running_refs);
2734 smp_mb();
2735 if (waitqueue_active(&delayed_refs->wait))
2736 wake_up(&delayed_refs->wait);
2737
Chris Masonc3e69d52009-03-13 10:17:05 -04002738 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002739 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002740 return 0;
2741}
2742
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002743int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2744 struct btrfs_root *root,
2745 u64 bytenr, u64 num_bytes, u64 flags,
2746 int is_data)
2747{
2748 struct btrfs_delayed_extent_op *extent_op;
2749 int ret;
2750
Miao Xie78a61842012-11-21 02:21:28 +00002751 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002752 if (!extent_op)
2753 return -ENOMEM;
2754
2755 extent_op->flags_to_set = flags;
2756 extent_op->update_flags = 1;
2757 extent_op->update_key = 0;
2758 extent_op->is_data = is_data ? 1 : 0;
2759
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002760 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2761 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002762 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002763 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002764 return ret;
2765}
2766
2767static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2768 struct btrfs_root *root,
2769 struct btrfs_path *path,
2770 u64 objectid, u64 offset, u64 bytenr)
2771{
2772 struct btrfs_delayed_ref_head *head;
2773 struct btrfs_delayed_ref_node *ref;
2774 struct btrfs_delayed_data_ref *data_ref;
2775 struct btrfs_delayed_ref_root *delayed_refs;
2776 struct rb_node *node;
2777 int ret = 0;
2778
2779 ret = -ENOENT;
2780 delayed_refs = &trans->transaction->delayed_refs;
2781 spin_lock(&delayed_refs->lock);
2782 head = btrfs_find_delayed_ref_head(trans, bytenr);
2783 if (!head)
2784 goto out;
2785
2786 if (!mutex_trylock(&head->mutex)) {
2787 atomic_inc(&head->node.refs);
2788 spin_unlock(&delayed_refs->lock);
2789
David Sterbab3b4aa72011-04-21 01:20:15 +02002790 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002791
David Sterba8cc33e52011-05-02 15:29:25 +02002792 /*
2793 * Mutex was contended, block until it's released and let
2794 * caller try again
2795 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002796 mutex_lock(&head->mutex);
2797 mutex_unlock(&head->mutex);
2798 btrfs_put_delayed_ref(&head->node);
2799 return -EAGAIN;
2800 }
2801
2802 node = rb_prev(&head->node.rb_node);
2803 if (!node)
2804 goto out_unlock;
2805
2806 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2807
2808 if (ref->bytenr != bytenr)
2809 goto out_unlock;
2810
2811 ret = 1;
2812 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2813 goto out_unlock;
2814
2815 data_ref = btrfs_delayed_node_to_data_ref(ref);
2816
2817 node = rb_prev(node);
2818 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002819 int seq = ref->seq;
2820
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002821 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002822 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002823 goto out_unlock;
2824 }
2825
2826 if (data_ref->root != root->root_key.objectid ||
2827 data_ref->objectid != objectid || data_ref->offset != offset)
2828 goto out_unlock;
2829
2830 ret = 0;
2831out_unlock:
2832 mutex_unlock(&head->mutex);
2833out:
2834 spin_unlock(&delayed_refs->lock);
2835 return ret;
2836}
2837
2838static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2839 struct btrfs_root *root,
2840 struct btrfs_path *path,
2841 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002842{
2843 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002844 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002845 struct btrfs_extent_data_ref *ref;
2846 struct btrfs_extent_inline_ref *iref;
2847 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002848 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002849 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002850 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002851
Chris Masonbe20aa92007-12-17 20:14:01 -05002852 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002853 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002854 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002855
Chris Masonbe20aa92007-12-17 20:14:01 -05002856 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2857 if (ret < 0)
2858 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002859 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002860
2861 ret = -ENOENT;
2862 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002863 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002864
Zheng Yan31840ae2008-09-23 13:14:14 -04002865 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002866 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002867 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002868
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002869 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002870 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002871
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002872 ret = 1;
2873 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2874#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2875 if (item_size < sizeof(*ei)) {
2876 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2877 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002878 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002879#endif
2880 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2881
2882 if (item_size != sizeof(*ei) +
2883 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2884 goto out;
2885
2886 if (btrfs_extent_generation(leaf, ei) <=
2887 btrfs_root_last_snapshot(&root->root_item))
2888 goto out;
2889
2890 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2891 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2892 BTRFS_EXTENT_DATA_REF_KEY)
2893 goto out;
2894
2895 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2896 if (btrfs_extent_refs(leaf, ei) !=
2897 btrfs_extent_data_ref_count(leaf, ref) ||
2898 btrfs_extent_data_ref_root(leaf, ref) !=
2899 root->root_key.objectid ||
2900 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2901 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2902 goto out;
2903
Yan Zhengf321e492008-07-30 09:26:11 -04002904 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002905out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002906 return ret;
2907}
2908
2909int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2910 struct btrfs_root *root,
2911 u64 objectid, u64 offset, u64 bytenr)
2912{
2913 struct btrfs_path *path;
2914 int ret;
2915 int ret2;
2916
2917 path = btrfs_alloc_path();
2918 if (!path)
2919 return -ENOENT;
2920
2921 do {
2922 ret = check_committed_ref(trans, root, path, objectid,
2923 offset, bytenr);
2924 if (ret && ret != -ENOENT)
2925 goto out;
2926
2927 ret2 = check_delayed_ref(trans, root, path, objectid,
2928 offset, bytenr);
2929 } while (ret2 == -EAGAIN);
2930
2931 if (ret2 && ret2 != -ENOENT) {
2932 ret = ret2;
2933 goto out;
2934 }
2935
2936 if (ret != -ENOENT || ret2 != -ENOENT)
2937 ret = 0;
2938out:
Yan Zhengf321e492008-07-30 09:26:11 -04002939 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002940 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2941 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002942 return ret;
2943}
2944
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002945static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002946 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002947 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002948 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002949{
2950 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002951 u64 num_bytes;
2952 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002953 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002954 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002955 struct btrfs_key key;
2956 struct btrfs_file_extent_item *fi;
2957 int i;
2958 int level;
2959 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002960 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002961 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002962
2963 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002964 nritems = btrfs_header_nritems(buf);
2965 level = btrfs_header_level(buf);
2966
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002967 if (!root->ref_cows && level == 0)
2968 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002969
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002970 if (inc)
2971 process_func = btrfs_inc_extent_ref;
2972 else
2973 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002974
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002975 if (full_backref)
2976 parent = buf->start;
2977 else
2978 parent = 0;
2979
Zheng Yan31840ae2008-09-23 13:14:14 -04002980 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002981 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002982 btrfs_item_key_to_cpu(buf, &key, i);
2983 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002984 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002985 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002986 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002987 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002988 BTRFS_FILE_EXTENT_INLINE)
2989 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002990 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2991 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002992 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002993
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002994 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2995 key.offset -= btrfs_file_extent_offset(buf, fi);
2996 ret = process_func(trans, root, bytenr, num_bytes,
2997 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002998 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002999 if (ret)
3000 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003001 } else {
3002 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003003 num_bytes = btrfs_level_size(root, level - 1);
3004 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003005 parent, ref_root, level - 1, 0,
3006 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003007 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003008 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003009 }
3010 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003011 return 0;
3012fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003013 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003014}
3015
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003016int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003017 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04003018{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003019 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020}
Zheng Yan31840ae2008-09-23 13:14:14 -04003021
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003022int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003023 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003024{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003025 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04003026}
3027
Chris Mason9078a3e2007-04-26 16:46:15 -04003028static int write_one_cache_group(struct btrfs_trans_handle *trans,
3029 struct btrfs_root *root,
3030 struct btrfs_path *path,
3031 struct btrfs_block_group_cache *cache)
3032{
3033 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003034 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003035 unsigned long bi;
3036 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003037
Chris Mason9078a3e2007-04-26 16:46:15 -04003038 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003039 if (ret < 0)
3040 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003041 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003042
3043 leaf = path->nodes[0];
3044 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3045 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3046 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003047 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003048fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 if (ret) {
3050 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003051 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003052 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003053 return 0;
3054
3055}
3056
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003057static struct btrfs_block_group_cache *
3058next_block_group(struct btrfs_root *root,
3059 struct btrfs_block_group_cache *cache)
3060{
3061 struct rb_node *node;
3062 spin_lock(&root->fs_info->block_group_cache_lock);
3063 node = rb_next(&cache->cache_node);
3064 btrfs_put_block_group(cache);
3065 if (node) {
3066 cache = rb_entry(node, struct btrfs_block_group_cache,
3067 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003068 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003069 } else
3070 cache = NULL;
3071 spin_unlock(&root->fs_info->block_group_cache_lock);
3072 return cache;
3073}
3074
Josef Bacik0af3d002010-06-21 14:48:16 -04003075static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3076 struct btrfs_trans_handle *trans,
3077 struct btrfs_path *path)
3078{
3079 struct btrfs_root *root = block_group->fs_info->tree_root;
3080 struct inode *inode = NULL;
3081 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003082 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003083 int num_pages = 0;
3084 int retries = 0;
3085 int ret = 0;
3086
3087 /*
3088 * If this block group is smaller than 100 megs don't bother caching the
3089 * block group.
3090 */
3091 if (block_group->key.offset < (100 * 1024 * 1024)) {
3092 spin_lock(&block_group->lock);
3093 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3094 spin_unlock(&block_group->lock);
3095 return 0;
3096 }
3097
3098again:
3099 inode = lookup_free_space_inode(root, block_group, path);
3100 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3101 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003102 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003103 goto out;
3104 }
3105
3106 if (IS_ERR(inode)) {
3107 BUG_ON(retries);
3108 retries++;
3109
3110 if (block_group->ro)
3111 goto out_free;
3112
3113 ret = create_free_space_inode(root, trans, block_group, path);
3114 if (ret)
3115 goto out_free;
3116 goto again;
3117 }
3118
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003119 /* We've already setup this transaction, go ahead and exit */
3120 if (block_group->cache_generation == trans->transid &&
3121 i_size_read(inode)) {
3122 dcs = BTRFS_DC_SETUP;
3123 goto out_put;
3124 }
3125
Josef Bacik0af3d002010-06-21 14:48:16 -04003126 /*
3127 * We want to set the generation to 0, that way if anything goes wrong
3128 * from here on out we know not to trust this cache when we load up next
3129 * time.
3130 */
3131 BTRFS_I(inode)->generation = 0;
3132 ret = btrfs_update_inode(trans, root, inode);
3133 WARN_ON(ret);
3134
3135 if (i_size_read(inode) > 0) {
3136 ret = btrfs_truncate_free_space_cache(root, trans, path,
3137 inode);
3138 if (ret)
3139 goto out_put;
3140 }
3141
3142 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003143 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3144 !btrfs_test_opt(root, SPACE_CACHE)) {
3145 /*
3146 * don't bother trying to write stuff out _if_
3147 * a) we're not cached,
3148 * b) we're with nospace_cache mount option.
3149 */
Josef Bacik2b209822010-12-03 13:17:53 -05003150 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003151 spin_unlock(&block_group->lock);
3152 goto out_put;
3153 }
3154 spin_unlock(&block_group->lock);
3155
Josef Bacik6fc823b2012-08-06 13:46:38 -06003156 /*
3157 * Try to preallocate enough space based on how big the block group is.
3158 * Keep in mind this has to include any pinned space which could end up
3159 * taking up quite a bit since it's not folded into the other space
3160 * cache.
3161 */
3162 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003163 if (!num_pages)
3164 num_pages = 1;
3165
Josef Bacik0af3d002010-06-21 14:48:16 -04003166 num_pages *= 16;
3167 num_pages *= PAGE_CACHE_SIZE;
3168
3169 ret = btrfs_check_data_free_space(inode, num_pages);
3170 if (ret)
3171 goto out_put;
3172
3173 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3174 num_pages, num_pages,
3175 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003176 if (!ret)
3177 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003178 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003179
Josef Bacik0af3d002010-06-21 14:48:16 -04003180out_put:
3181 iput(inode);
3182out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003183 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003184out:
3185 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003186 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003187 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003188 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003189 spin_unlock(&block_group->lock);
3190
3191 return ret;
3192}
3193
Chris Mason96b51792007-10-15 16:15:19 -04003194int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3195 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003196{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003197 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003198 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003199 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003200 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003201
3202 path = btrfs_alloc_path();
3203 if (!path)
3204 return -ENOMEM;
3205
Josef Bacik0af3d002010-06-21 14:48:16 -04003206again:
3207 while (1) {
3208 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3209 while (cache) {
3210 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3211 break;
3212 cache = next_block_group(root, cache);
3213 }
3214 if (!cache) {
3215 if (last == 0)
3216 break;
3217 last = 0;
3218 continue;
3219 }
3220 err = cache_save_setup(cache, trans, path);
3221 last = cache->key.objectid + cache->key.offset;
3222 btrfs_put_block_group(cache);
3223 }
3224
Chris Masond3977122009-01-05 21:25:51 -05003225 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003226 if (last == 0) {
3227 err = btrfs_run_delayed_refs(trans, root,
3228 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003229 if (err) /* File system offline */
3230 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003231 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003232
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003233 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3234 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003235 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3236 btrfs_put_block_group(cache);
3237 goto again;
3238 }
3239
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003240 if (cache->dirty)
3241 break;
3242 cache = next_block_group(root, cache);
3243 }
3244 if (!cache) {
3245 if (last == 0)
3246 break;
3247 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003248 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003249 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003250
Josef Bacik0cb59c92010-07-02 12:14:14 -04003251 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3252 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003253 cache->dirty = 0;
3254 last = cache->key.objectid + cache->key.offset;
3255
3256 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003257 if (err) /* File system offline */
3258 goto out;
3259
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003260 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003261 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003262
Josef Bacik0cb59c92010-07-02 12:14:14 -04003263 while (1) {
3264 /*
3265 * I don't think this is needed since we're just marking our
3266 * preallocated extent as written, but just in case it can't
3267 * hurt.
3268 */
3269 if (last == 0) {
3270 err = btrfs_run_delayed_refs(trans, root,
3271 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003272 if (err) /* File system offline */
3273 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003274 }
3275
3276 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3277 while (cache) {
3278 /*
3279 * Really this shouldn't happen, but it could if we
3280 * couldn't write the entire preallocated extent and
3281 * splitting the extent resulted in a new block.
3282 */
3283 if (cache->dirty) {
3284 btrfs_put_block_group(cache);
3285 goto again;
3286 }
3287 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3288 break;
3289 cache = next_block_group(root, cache);
3290 }
3291 if (!cache) {
3292 if (last == 0)
3293 break;
3294 last = 0;
3295 continue;
3296 }
3297
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003298 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003299
3300 /*
3301 * If we didn't have an error then the cache state is still
3302 * NEED_WRITE, so we can set it to WRITTEN.
3303 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003304 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003305 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3306 last = cache->key.objectid + cache->key.offset;
3307 btrfs_put_block_group(cache);
3308 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003309out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003310
Chris Mason9078a3e2007-04-26 16:46:15 -04003311 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003312 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003313}
3314
Yan Zhengd2fb3432008-12-11 16:30:39 -05003315int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3316{
3317 struct btrfs_block_group_cache *block_group;
3318 int readonly = 0;
3319
3320 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3321 if (!block_group || block_group->ro)
3322 readonly = 1;
3323 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003324 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003325 return readonly;
3326}
3327
Chris Mason593060d2008-03-25 16:50:33 -04003328static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3329 u64 total_bytes, u64 bytes_used,
3330 struct btrfs_space_info **space_info)
3331{
3332 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003333 int i;
3334 int factor;
3335
3336 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3337 BTRFS_BLOCK_GROUP_RAID10))
3338 factor = 2;
3339 else
3340 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003341
3342 found = __find_space_info(info, flags);
3343 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003344 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003345 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003346 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003347 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003348 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003349 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003350 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003351 *space_info = found;
3352 return 0;
3353 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003354 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003355 if (!found)
3356 return -ENOMEM;
3357
Yan, Zhengb742bb82010-05-16 10:46:24 -04003358 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3359 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003360 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003361 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003362 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003363 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003364 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003365 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003366 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003367 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003368 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003369 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003370 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003371 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003372 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003373 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003374 found->flush = 0;
3375 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003376 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003377 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003378 if (flags & BTRFS_BLOCK_GROUP_DATA)
3379 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003380 return 0;
3381}
3382
Chris Mason8790d502008-04-03 16:29:03 -04003383static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3384{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003385 u64 extra_flags = chunk_to_extended(flags) &
3386 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003387
Miao Xiede98ced2013-01-29 10:13:12 +00003388 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003389 if (flags & BTRFS_BLOCK_GROUP_DATA)
3390 fs_info->avail_data_alloc_bits |= extra_flags;
3391 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3392 fs_info->avail_metadata_alloc_bits |= extra_flags;
3393 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3394 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003395 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003396}
Chris Mason593060d2008-03-25 16:50:33 -04003397
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003398/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003399 * returns target flags in extended format or 0 if restripe for this
3400 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003401 *
3402 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003403 */
3404static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3405{
3406 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3407 u64 target = 0;
3408
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003409 if (!bctl)
3410 return 0;
3411
3412 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3413 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3414 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3415 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3416 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3417 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3418 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3419 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3420 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3421 }
3422
3423 return target;
3424}
3425
3426/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003427 * @flags: available profiles in extended format (see ctree.h)
3428 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003429 * Returns reduced profile in chunk format. If profile changing is in
3430 * progress (either running or paused) picks the target profile (if it's
3431 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003432 */
Yan Zheng2b820322008-11-17 21:11:30 -05003433u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003434{
Chris Masoncd02dca2010-12-13 14:56:23 -05003435 /*
3436 * we add in the count of missing devices because we want
3437 * to make sure that any RAID levels on a degraded FS
3438 * continue to be honored.
3439 */
3440 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3441 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003442 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003443 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003444
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003445 /*
3446 * see if restripe for this chunk_type is in progress, if so
3447 * try to reduce to the target profile
3448 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003449 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003450 target = get_restripe_target(root->fs_info, flags);
3451 if (target) {
3452 /* pick target profile only if it's already available */
3453 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003454 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003455 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003456 }
3457 }
3458 spin_unlock(&root->fs_info->balance_lock);
3459
David Woodhouse53b381b2013-01-29 18:40:14 -05003460 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003461 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003462 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3463 BTRFS_BLOCK_GROUP_RAID5);
3464 if (num_devices < 3)
3465 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003466 if (num_devices < 4)
3467 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3468
David Woodhouse53b381b2013-01-29 18:40:14 -05003469 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3470 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3471 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3472 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003473
David Woodhouse53b381b2013-01-29 18:40:14 -05003474 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3475 tmp = BTRFS_BLOCK_GROUP_RAID6;
3476 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3477 tmp = BTRFS_BLOCK_GROUP_RAID5;
3478 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3479 tmp = BTRFS_BLOCK_GROUP_RAID10;
3480 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3481 tmp = BTRFS_BLOCK_GROUP_RAID1;
3482 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3483 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003484
David Woodhouse53b381b2013-01-29 18:40:14 -05003485 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003486}
3487
Yan, Zhengb742bb82010-05-16 10:46:24 -04003488static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003489{
Miao Xiede98ced2013-01-29 10:13:12 +00003490 unsigned seq;
3491
3492 do {
3493 seq = read_seqbegin(&root->fs_info->profiles_lock);
3494
3495 if (flags & BTRFS_BLOCK_GROUP_DATA)
3496 flags |= root->fs_info->avail_data_alloc_bits;
3497 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3498 flags |= root->fs_info->avail_system_alloc_bits;
3499 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3500 flags |= root->fs_info->avail_metadata_alloc_bits;
3501 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003502
Yan, Zhengb742bb82010-05-16 10:46:24 -04003503 return btrfs_reduce_alloc_profile(root, flags);
3504}
Josef Bacik6a632092009-02-20 11:00:09 -05003505
Miao Xie6d07bce2011-01-05 10:07:31 +00003506u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003507{
3508 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003509 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003510
Yan, Zhengb742bb82010-05-16 10:46:24 -04003511 if (data)
3512 flags = BTRFS_BLOCK_GROUP_DATA;
3513 else if (root == root->fs_info->chunk_root)
3514 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3515 else
3516 flags = BTRFS_BLOCK_GROUP_METADATA;
3517
David Woodhouse53b381b2013-01-29 18:40:14 -05003518 ret = get_alloc_profile(root, flags);
3519 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003520}
3521
Josef Bacik6a632092009-02-20 11:00:09 -05003522/*
3523 * This will check the space that the inode allocates from to make sure we have
3524 * enough space for bytes.
3525 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003526int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003527{
3528 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003529 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003530 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003531 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003532 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003533
3534 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003535 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003536
Li Zefan82d59022011-04-20 10:33:24 +08003537 if (root == root->fs_info->tree_root ||
3538 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003539 alloc_chunk = 0;
3540 committed = 1;
3541 }
3542
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003543 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003544 if (!data_sinfo)
3545 goto alloc;
3546
Josef Bacik6a632092009-02-20 11:00:09 -05003547again:
3548 /* make sure we have enough space to handle the data first */
3549 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003550 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3551 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3552 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003553
3554 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003555 struct btrfs_trans_handle *trans;
3556
Josef Bacik6a632092009-02-20 11:00:09 -05003557 /*
3558 * if we don't have enough free bytes in this space then we need
3559 * to alloc a new chunk.
3560 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003561 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003562 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003563
Chris Mason0e4f8f82011-04-15 16:05:44 -04003564 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003565 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003566alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003567 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003568 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003569 if (IS_ERR(trans))
3570 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003571
3572 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003573 alloc_target,
3574 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003575 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003576 if (ret < 0) {
3577 if (ret != -ENOSPC)
3578 return ret;
3579 else
3580 goto commit_trans;
3581 }
Chris Mason33b4d472009-09-22 14:45:50 -04003582
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003583 if (!data_sinfo)
3584 data_sinfo = fs_info->data_sinfo;
3585
Josef Bacik6a632092009-02-20 11:00:09 -05003586 goto again;
3587 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003588
3589 /*
3590 * If we have less pinned bytes than we want to allocate then
3591 * don't bother committing the transaction, it won't help us.
3592 */
3593 if (data_sinfo->bytes_pinned < bytes)
3594 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003595 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003596
3597 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003598commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003599 if (!committed &&
3600 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003601 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003602 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003603 if (IS_ERR(trans))
3604 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003605 ret = btrfs_commit_transaction(trans, root);
3606 if (ret)
3607 return ret;
3608 goto again;
3609 }
3610
Josef Bacik6a632092009-02-20 11:00:09 -05003611 return -ENOSPC;
3612 }
3613 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003614 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003615 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003616 spin_unlock(&data_sinfo->lock);
3617
Josef Bacik9ed74f22009-09-11 16:12:44 -04003618 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003619}
3620
3621/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003622 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003623 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003624void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003625{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003626 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003627 struct btrfs_space_info *data_sinfo;
3628
3629 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003630 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003631
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003632 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003633 spin_lock(&data_sinfo->lock);
3634 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003635 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003636 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003637 spin_unlock(&data_sinfo->lock);
3638}
3639
Josef Bacik97e728d2009-04-21 17:40:57 -04003640static void force_metadata_allocation(struct btrfs_fs_info *info)
3641{
3642 struct list_head *head = &info->space_info;
3643 struct btrfs_space_info *found;
3644
3645 rcu_read_lock();
3646 list_for_each_entry_rcu(found, head, list) {
3647 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003648 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003649 }
3650 rcu_read_unlock();
3651}
3652
Chris Masone5bc2452010-10-26 13:37:56 -04003653static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003654 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003655{
Josef Bacikfb25e912011-07-26 17:00:46 -04003656 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003657 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003658 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003659 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003660
Chris Mason0e4f8f82011-04-15 16:05:44 -04003661 if (force == CHUNK_ALLOC_FORCE)
3662 return 1;
3663
3664 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003665 * We need to take into account the global rsv because for all intents
3666 * and purposes it's used space. Don't worry about locking the
3667 * global_rsv, it doesn't change except when the transaction commits.
3668 */
Josef Bacik54338b52012-08-14 16:20:52 -04003669 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3670 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003671
3672 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003673 * in limited mode, we want to have some free space up to
3674 * about 1% of the FS size.
3675 */
3676 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003677 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003678 thresh = max_t(u64, 64 * 1024 * 1024,
3679 div_factor_fine(thresh, 1));
3680
3681 if (num_bytes - num_allocated < thresh)
3682 return 1;
3683 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003684
Josef Bacik698d0082012-09-12 14:08:47 -04003685 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003686 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003687 return 1;
3688}
3689
Liu Bo15d1ff82012-03-29 09:57:44 -04003690static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3691{
3692 u64 num_dev;
3693
David Woodhouse53b381b2013-01-29 18:40:14 -05003694 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3695 BTRFS_BLOCK_GROUP_RAID0 |
3696 BTRFS_BLOCK_GROUP_RAID5 |
3697 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003698 num_dev = root->fs_info->fs_devices->rw_devices;
3699 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3700 num_dev = 2;
3701 else
3702 num_dev = 1; /* DUP or single */
3703
3704 /* metadata for updaing devices and chunk tree */
3705 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3706}
3707
3708static void check_system_chunk(struct btrfs_trans_handle *trans,
3709 struct btrfs_root *root, u64 type)
3710{
3711 struct btrfs_space_info *info;
3712 u64 left;
3713 u64 thresh;
3714
3715 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3716 spin_lock(&info->lock);
3717 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3718 info->bytes_reserved - info->bytes_readonly;
3719 spin_unlock(&info->lock);
3720
3721 thresh = get_system_chunk_thresh(root, type);
3722 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003723 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3724 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003725 dump_space_info(info, 0, 0);
3726 }
3727
3728 if (left < thresh) {
3729 u64 flags;
3730
3731 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3732 btrfs_alloc_chunk(trans, root, flags);
3733 }
3734}
3735
Chris Mason6324fbf2008-03-24 15:01:59 -04003736static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003737 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003738{
3739 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003740 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003741 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003742 int ret = 0;
3743
Josef Bacikc6b305a2012-12-18 09:16:16 -05003744 /* Don't re-enter if we're already allocating a chunk */
3745 if (trans->allocating_chunk)
3746 return -ENOSPC;
3747
Chris Mason6324fbf2008-03-24 15:01:59 -04003748 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003749 if (!space_info) {
3750 ret = update_space_info(extent_root->fs_info, flags,
3751 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003752 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003753 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003755
Josef Bacik6d741192011-04-11 20:20:11 -04003756again:
Josef Bacik25179202008-10-29 14:49:05 -04003757 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003758 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003759 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003760 if (space_info->full) {
3761 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003762 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003763 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003764
Josef Bacik698d0082012-09-12 14:08:47 -04003765 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003766 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003767 return 0;
3768 } else if (space_info->chunk_alloc) {
3769 wait_for_alloc = 1;
3770 } else {
3771 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003772 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003773
Josef Bacik25179202008-10-29 14:49:05 -04003774 spin_unlock(&space_info->lock);
3775
Josef Bacik6d741192011-04-11 20:20:11 -04003776 mutex_lock(&fs_info->chunk_mutex);
3777
3778 /*
3779 * The chunk_mutex is held throughout the entirety of a chunk
3780 * allocation, so once we've acquired the chunk_mutex we know that the
3781 * other guy is done and we need to recheck and see if we should
3782 * allocate.
3783 */
3784 if (wait_for_alloc) {
3785 mutex_unlock(&fs_info->chunk_mutex);
3786 wait_for_alloc = 0;
3787 goto again;
3788 }
3789
Josef Bacikc6b305a2012-12-18 09:16:16 -05003790 trans->allocating_chunk = true;
3791
Josef Bacik97e728d2009-04-21 17:40:57 -04003792 /*
Josef Bacik67377732010-09-16 16:19:09 -04003793 * If we have mixed data/metadata chunks we want to make sure we keep
3794 * allocating mixed chunks instead of individual chunks.
3795 */
3796 if (btrfs_mixed_space_info(space_info))
3797 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3798
3799 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003800 * if we're doing a data chunk, go ahead and make sure that
3801 * we keep a reasonable number of metadata chunks allocated in the
3802 * FS as well.
3803 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003804 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003805 fs_info->data_chunk_allocations++;
3806 if (!(fs_info->data_chunk_allocations %
3807 fs_info->metadata_ratio))
3808 force_metadata_allocation(fs_info);
3809 }
3810
Liu Bo15d1ff82012-03-29 09:57:44 -04003811 /*
3812 * Check if we have enough space in SYSTEM chunk because we may need
3813 * to update devices.
3814 */
3815 check_system_chunk(trans, extent_root, flags);
3816
Yan Zheng2b820322008-11-17 21:11:30 -05003817 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003818 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003819
Josef Bacik9ed74f22009-09-11 16:12:44 -04003820 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003821 if (ret < 0 && ret != -ENOSPC)
3822 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003823 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003824 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003825 else
3826 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003827
Chris Mason0e4f8f82011-04-15 16:05:44 -04003828 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003829out:
Josef Bacik6d741192011-04-11 20:20:11 -04003830 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003831 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003832 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003833 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003834}
3835
Josef Bacika80c8dc2012-09-06 16:59:33 -04003836static int can_overcommit(struct btrfs_root *root,
3837 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003838 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003839{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003840 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003841 u64 profile = btrfs_get_alloc_profile(root, 0);
Josef Bacik96f1bb52013-01-30 17:02:51 -05003842 u64 rsv_size = 0;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003843 u64 avail;
3844 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003845 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003846
3847 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003848 space_info->bytes_pinned + space_info->bytes_readonly;
3849
3850 spin_lock(&global_rsv->lock);
3851 rsv_size = global_rsv->size;
3852 spin_unlock(&global_rsv->lock);
3853
3854 /*
3855 * We only want to allow over committing if we have lots of actual space
3856 * free, but if we don't have enough space to handle the global reserve
3857 * space then we could end up having a real enospc problem when trying
3858 * to allocate a chunk or some other such important allocation.
3859 */
3860 rsv_size <<= 1;
3861 if (used + rsv_size >= space_info->total_bytes)
3862 return 0;
3863
3864 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003865
3866 spin_lock(&root->fs_info->free_chunk_lock);
3867 avail = root->fs_info->free_chunk_space;
3868 spin_unlock(&root->fs_info->free_chunk_lock);
3869
3870 /*
3871 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003872 * space is actually useable. For raid56, the space info used
3873 * doesn't include the parity drive, so we don't have to
3874 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003875 */
3876 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3877 BTRFS_BLOCK_GROUP_RAID1 |
3878 BTRFS_BLOCK_GROUP_RAID10))
3879 avail >>= 1;
3880
Josef Bacik70afa392013-02-06 13:53:19 -05003881 to_add = space_info->total_bytes;
3882
Josef Bacika80c8dc2012-09-06 16:59:33 -04003883 /*
Miao Xie561c2942012-10-16 11:32:18 +00003884 * If we aren't flushing all things, let us overcommit up to
3885 * 1/2th of the space. If we can flush, don't let us overcommit
3886 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003887 */
Miao Xie08e007d2012-10-16 11:33:38 +00003888 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003889 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003890 else
Josef Bacik70afa392013-02-06 13:53:19 -05003891 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003892
Josef Bacik70afa392013-02-06 13:53:19 -05003893 /*
3894 * Limit the overcommit to the amount of free space we could possibly
3895 * allocate for chunks.
3896 */
3897 to_add = min(avail, to_add);
3898
3899 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003900 return 1;
3901 return 0;
3902}
3903
Miao Xieda633a42012-12-20 11:19:09 +00003904void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3905 unsigned long nr_pages)
3906{
3907 struct super_block *sb = root->fs_info->sb;
3908 int started;
3909
3910 /* If we can not start writeback, just sync all the delalloc file. */
Linus Torvaldsb6951882013-03-02 16:41:54 -08003911 started = try_to_writeback_inodes_sb_nr(sb, nr_pages,
Miao Xieda633a42012-12-20 11:19:09 +00003912 WB_REASON_FS_FREE_SPACE);
3913 if (!started) {
3914 /*
3915 * We needn't worry the filesystem going from r/w to r/o though
3916 * we don't acquire ->s_umount mutex, because the filesystem
3917 * should guarantee the delalloc inodes list be empty after
3918 * the filesystem is readonly(all dirty pages are written to
3919 * the disk).
3920 */
3921 btrfs_start_delalloc_inodes(root, 0);
Josef Bacik98ad69c2013-04-04 11:55:49 -04003922 if (!current->journal_info)
3923 btrfs_wait_ordered_extents(root, 0);
Miao Xieda633a42012-12-20 11:19:09 +00003924 }
3925}
3926
Yan, Zheng5da9d012010-05-16 10:46:25 -04003927/*
3928 * shrink metadata reservation for delalloc
3929 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003930static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3931 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003932{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003933 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003934 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003935 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003936 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003937 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003938 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003939 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003940 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003941 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003942
Josef Bacik663350a2011-11-03 22:54:25 -04003943 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003944 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003945 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003946
3947 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003948 delalloc_bytes = percpu_counter_sum_positive(
3949 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003950 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003951 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003952 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003953 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003954 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003955 }
3956
Josef Bacikf4c738c2012-07-02 17:10:51 -04003957 while (delalloc_bytes && loops < 3) {
3958 max_reclaim = min(delalloc_bytes, to_reclaim);
3959 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003960 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003961 /*
3962 * We need to wait for the async pages to actually start before
3963 * we do anything.
3964 */
3965 wait_event(root->fs_info->async_submit_wait,
3966 !atomic_read(&root->fs_info->async_delalloc_pages));
3967
Miao Xie08e007d2012-10-16 11:33:38 +00003968 if (!trans)
3969 flush = BTRFS_RESERVE_FLUSH_ALL;
3970 else
3971 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003972 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003973 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003974 spin_unlock(&space_info->lock);
3975 break;
3976 }
Josef Bacik0019f102010-10-15 15:18:40 -04003977 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003978
Chris Mason36e39c42011-03-12 07:08:42 -05003979 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003980 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003981 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003982 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003983 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003984 if (time_left)
3985 break;
3986 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003987 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003988 delalloc_bytes = percpu_counter_sum_positive(
3989 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003990 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003991}
3992
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003993/**
Josef Bacik663350a2011-11-03 22:54:25 -04003994 * maybe_commit_transaction - possibly commit the transaction if its ok to
3995 * @root - the root we're allocating for
3996 * @bytes - the number of bytes we want to reserve
3997 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003998 *
Josef Bacik663350a2011-11-03 22:54:25 -04003999 * This will check to make sure that committing the transaction will actually
4000 * get us somewhere and then commit the transaction if it does. Otherwise it
4001 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004002 */
Josef Bacik663350a2011-11-03 22:54:25 -04004003static int may_commit_transaction(struct btrfs_root *root,
4004 struct btrfs_space_info *space_info,
4005 u64 bytes, int force)
4006{
4007 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4008 struct btrfs_trans_handle *trans;
4009
4010 trans = (struct btrfs_trans_handle *)current->journal_info;
4011 if (trans)
4012 return -EAGAIN;
4013
4014 if (force)
4015 goto commit;
4016
4017 /* See if there is enough pinned space to make this reservation */
4018 spin_lock(&space_info->lock);
4019 if (space_info->bytes_pinned >= bytes) {
4020 spin_unlock(&space_info->lock);
4021 goto commit;
4022 }
4023 spin_unlock(&space_info->lock);
4024
4025 /*
4026 * See if there is some space in the delayed insertion reservation for
4027 * this reservation.
4028 */
4029 if (space_info != delayed_rsv->space_info)
4030 return -ENOSPC;
4031
Liu Bod9b02182012-02-16 18:34:39 +08004032 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004033 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004034 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04004035 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004036 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004037 return -ENOSPC;
4038 }
4039 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004040 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004041
4042commit:
4043 trans = btrfs_join_transaction(root);
4044 if (IS_ERR(trans))
4045 return -ENOSPC;
4046
4047 return btrfs_commit_transaction(trans, root);
4048}
4049
Josef Bacik96c3f432012-06-21 14:05:49 -04004050enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004051 FLUSH_DELAYED_ITEMS_NR = 1,
4052 FLUSH_DELAYED_ITEMS = 2,
4053 FLUSH_DELALLOC = 3,
4054 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004055 ALLOC_CHUNK = 5,
4056 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004057};
4058
4059static int flush_space(struct btrfs_root *root,
4060 struct btrfs_space_info *space_info, u64 num_bytes,
4061 u64 orig_bytes, int state)
4062{
4063 struct btrfs_trans_handle *trans;
4064 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004065 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004066
4067 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004068 case FLUSH_DELAYED_ITEMS_NR:
4069 case FLUSH_DELAYED_ITEMS:
4070 if (state == FLUSH_DELAYED_ITEMS_NR) {
4071 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
4072
4073 nr = (int)div64_u64(num_bytes, bytes);
4074 if (!nr)
4075 nr = 1;
4076 nr *= 2;
4077 } else {
4078 nr = -1;
4079 }
4080 trans = btrfs_join_transaction(root);
4081 if (IS_ERR(trans)) {
4082 ret = PTR_ERR(trans);
4083 break;
4084 }
4085 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4086 btrfs_end_transaction(trans, root);
4087 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004088 case FLUSH_DELALLOC:
4089 case FLUSH_DELALLOC_WAIT:
4090 shrink_delalloc(root, num_bytes, orig_bytes,
4091 state == FLUSH_DELALLOC_WAIT);
4092 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004093 case ALLOC_CHUNK:
4094 trans = btrfs_join_transaction(root);
4095 if (IS_ERR(trans)) {
4096 ret = PTR_ERR(trans);
4097 break;
4098 }
4099 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004100 btrfs_get_alloc_profile(root, 0),
4101 CHUNK_ALLOC_NO_FORCE);
4102 btrfs_end_transaction(trans, root);
4103 if (ret == -ENOSPC)
4104 ret = 0;
4105 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004106 case COMMIT_TRANS:
4107 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4108 break;
4109 default:
4110 ret = -ENOSPC;
4111 break;
4112 }
4113
4114 return ret;
4115}
Josef Bacik663350a2011-11-03 22:54:25 -04004116/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004117 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4118 * @root - the root we're allocating for
4119 * @block_rsv - the block_rsv we're allocating for
4120 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004121 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004122 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004123 * This will reserve orgi_bytes number of bytes from the space info associated
4124 * with the block_rsv. If there is not enough space it will make an attempt to
4125 * flush out space to make room. It will do this by flushing delalloc if
4126 * possible or committing the transaction. If flush is 0 then no attempts to
4127 * regain reservations will be made and this will fail if there is not enough
4128 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004129 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004130static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004131 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004132 u64 orig_bytes,
4133 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004134{
4135 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004136 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004137 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004138 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004139 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004140 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004141
4142again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004143 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004144 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004145 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004146 * We only want to wait if somebody other than us is flushing and we
4147 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004148 */
Miao Xie08e007d2012-10-16 11:33:38 +00004149 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4150 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004151 spin_unlock(&space_info->lock);
4152 /*
4153 * If we have a trans handle we can't wait because the flusher
4154 * may have to commit the transaction, which would mean we would
4155 * deadlock since we are waiting for the flusher to finish, but
4156 * hold the current transaction open.
4157 */
Josef Bacik663350a2011-11-03 22:54:25 -04004158 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004159 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004160 ret = wait_event_killable(space_info->wait, !space_info->flush);
4161 /* Must have been killed, return */
4162 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004163 return -EINTR;
4164
4165 spin_lock(&space_info->lock);
4166 }
4167
4168 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004169 used = space_info->bytes_used + space_info->bytes_reserved +
4170 space_info->bytes_pinned + space_info->bytes_readonly +
4171 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004172
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004173 /*
4174 * The idea here is that we've not already over-reserved the block group
4175 * then we can go ahead and save our reservation first and then start
4176 * flushing if we need to. Otherwise if we've already overcommitted
4177 * lets start flushing stuff first and then come back and try to make
4178 * our reservation.
4179 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004180 if (used <= space_info->total_bytes) {
4181 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004182 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004183 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004184 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004185 ret = 0;
4186 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004187 /*
4188 * Ok set num_bytes to orig_bytes since we aren't
4189 * overocmmitted, this way we only try and reclaim what
4190 * we need.
4191 */
4192 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004193 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004194 } else {
4195 /*
4196 * Ok we're over committed, set num_bytes to the overcommitted
4197 * amount plus the amount of bytes that we need for this
4198 * reservation.
4199 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004200 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004201 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004202 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004203
Josef Bacik44734ed2012-09-28 16:04:19 -04004204 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4205 space_info->bytes_may_use += orig_bytes;
4206 trace_btrfs_space_reservation(root->fs_info, "space_info",
4207 space_info->flags, orig_bytes,
4208 1);
4209 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004210 }
4211
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004212 /*
4213 * Couldn't make our reservation, save our place so while we're trying
4214 * to reclaim space we can actually use it instead of somebody else
4215 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004216 *
4217 * We make the other tasks wait for the flush only when we can flush
4218 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004219 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004220 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004221 flushing = true;
4222 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004223 }
4224
Yan, Zhengf0486c62010-05-16 10:46:25 -04004225 spin_unlock(&space_info->lock);
4226
Miao Xie08e007d2012-10-16 11:33:38 +00004227 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004228 goto out;
4229
Josef Bacik96c3f432012-06-21 14:05:49 -04004230 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4231 flush_state);
4232 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004233
4234 /*
4235 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4236 * would happen. So skip delalloc flush.
4237 */
4238 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4239 (flush_state == FLUSH_DELALLOC ||
4240 flush_state == FLUSH_DELALLOC_WAIT))
4241 flush_state = ALLOC_CHUNK;
4242
Josef Bacik96c3f432012-06-21 14:05:49 -04004243 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004244 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004245 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4246 flush_state < COMMIT_TRANS)
4247 goto again;
4248 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4249 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004250 goto again;
4251
4252out:
Josef Bacik5d803662013-02-07 16:06:02 -05004253 if (ret == -ENOSPC &&
4254 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4255 struct btrfs_block_rsv *global_rsv =
4256 &root->fs_info->global_block_rsv;
4257
4258 if (block_rsv != global_rsv &&
4259 !block_rsv_use_bytes(global_rsv, orig_bytes))
4260 ret = 0;
4261 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004262 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004263 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004264 space_info->flush = 0;
4265 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004266 spin_unlock(&space_info->lock);
4267 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004268 return ret;
4269}
4270
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004271static struct btrfs_block_rsv *get_block_rsv(
4272 const struct btrfs_trans_handle *trans,
4273 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004274{
Josef Bacik4c13d752011-08-30 11:31:29 -04004275 struct btrfs_block_rsv *block_rsv = NULL;
4276
Josef Bacik0e721102012-06-26 16:13:18 -04004277 if (root->ref_cows)
4278 block_rsv = trans->block_rsv;
4279
4280 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004281 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004282
4283 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004284 block_rsv = root->block_rsv;
4285
4286 if (!block_rsv)
4287 block_rsv = &root->fs_info->empty_block_rsv;
4288
4289 return block_rsv;
4290}
4291
4292static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4293 u64 num_bytes)
4294{
4295 int ret = -ENOSPC;
4296 spin_lock(&block_rsv->lock);
4297 if (block_rsv->reserved >= num_bytes) {
4298 block_rsv->reserved -= num_bytes;
4299 if (block_rsv->reserved < block_rsv->size)
4300 block_rsv->full = 0;
4301 ret = 0;
4302 }
4303 spin_unlock(&block_rsv->lock);
4304 return ret;
4305}
4306
4307static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4308 u64 num_bytes, int update_size)
4309{
4310 spin_lock(&block_rsv->lock);
4311 block_rsv->reserved += num_bytes;
4312 if (update_size)
4313 block_rsv->size += num_bytes;
4314 else if (block_rsv->reserved >= block_rsv->size)
4315 block_rsv->full = 1;
4316 spin_unlock(&block_rsv->lock);
4317}
4318
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004319static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4320 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004321 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004322{
4323 struct btrfs_space_info *space_info = block_rsv->space_info;
4324
4325 spin_lock(&block_rsv->lock);
4326 if (num_bytes == (u64)-1)
4327 num_bytes = block_rsv->size;
4328 block_rsv->size -= num_bytes;
4329 if (block_rsv->reserved >= block_rsv->size) {
4330 num_bytes = block_rsv->reserved - block_rsv->size;
4331 block_rsv->reserved = block_rsv->size;
4332 block_rsv->full = 1;
4333 } else {
4334 num_bytes = 0;
4335 }
4336 spin_unlock(&block_rsv->lock);
4337
4338 if (num_bytes > 0) {
4339 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004340 spin_lock(&dest->lock);
4341 if (!dest->full) {
4342 u64 bytes_to_add;
4343
4344 bytes_to_add = dest->size - dest->reserved;
4345 bytes_to_add = min(num_bytes, bytes_to_add);
4346 dest->reserved += bytes_to_add;
4347 if (dest->reserved >= dest->size)
4348 dest->full = 1;
4349 num_bytes -= bytes_to_add;
4350 }
4351 spin_unlock(&dest->lock);
4352 }
4353 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004354 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004355 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004356 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004357 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004358 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004359 spin_unlock(&space_info->lock);
4360 }
4361 }
4362}
4363
4364static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4365 struct btrfs_block_rsv *dst, u64 num_bytes)
4366{
4367 int ret;
4368
4369 ret = block_rsv_use_bytes(src, num_bytes);
4370 if (ret)
4371 return ret;
4372
4373 block_rsv_add_bytes(dst, num_bytes, 1);
4374 return 0;
4375}
4376
Miao Xie66d8f3d2012-09-06 04:02:28 -06004377void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004378{
4379 memset(rsv, 0, sizeof(*rsv));
4380 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004381 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004382}
4383
Miao Xie66d8f3d2012-09-06 04:02:28 -06004384struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4385 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004386{
4387 struct btrfs_block_rsv *block_rsv;
4388 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004389
4390 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4391 if (!block_rsv)
4392 return NULL;
4393
Miao Xie66d8f3d2012-09-06 04:02:28 -06004394 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004395 block_rsv->space_info = __find_space_info(fs_info,
4396 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004397 return block_rsv;
4398}
4399
4400void btrfs_free_block_rsv(struct btrfs_root *root,
4401 struct btrfs_block_rsv *rsv)
4402{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004403 if (!rsv)
4404 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004405 btrfs_block_rsv_release(root, rsv, (u64)-1);
4406 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004407}
4408
Miao Xie08e007d2012-10-16 11:33:38 +00004409int btrfs_block_rsv_add(struct btrfs_root *root,
4410 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4411 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004412{
4413 int ret;
4414
4415 if (num_bytes == 0)
4416 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004417
Miao Xie61b520a2011-11-10 20:45:05 -05004418 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004419 if (!ret) {
4420 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4421 return 0;
4422 }
4423
Yan, Zhengf0486c62010-05-16 10:46:25 -04004424 return ret;
4425}
4426
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004427int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004428 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004429{
4430 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004431 int ret = -ENOSPC;
4432
4433 if (!block_rsv)
4434 return 0;
4435
4436 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004437 num_bytes = div_factor(block_rsv->size, min_factor);
4438 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004439 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004440 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004441
Josef Bacik36ba0222011-10-18 12:15:48 -04004442 return ret;
4443}
4444
Miao Xie08e007d2012-10-16 11:33:38 +00004445int btrfs_block_rsv_refill(struct btrfs_root *root,
4446 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4447 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004448{
4449 u64 num_bytes = 0;
4450 int ret = -ENOSPC;
4451
4452 if (!block_rsv)
4453 return 0;
4454
4455 spin_lock(&block_rsv->lock);
4456 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004457 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004458 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004459 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004460 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004461 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004462
Yan, Zhengf0486c62010-05-16 10:46:25 -04004463 if (!ret)
4464 return 0;
4465
Miao Xieaa38a712011-11-18 17:43:00 +08004466 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004467 if (!ret) {
4468 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004469 return 0;
4470 }
4471
Josef Bacik13553e52011-08-08 13:33:21 -04004472 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004473}
4474
4475int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4476 struct btrfs_block_rsv *dst_rsv,
4477 u64 num_bytes)
4478{
4479 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4480}
4481
4482void btrfs_block_rsv_release(struct btrfs_root *root,
4483 struct btrfs_block_rsv *block_rsv,
4484 u64 num_bytes)
4485{
4486 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4487 if (global_rsv->full || global_rsv == block_rsv ||
4488 block_rsv->space_info != global_rsv->space_info)
4489 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004490 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4491 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004492}
4493
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004494/*
4495 * helper to calculate size of global block reservation.
4496 * the desired value is sum of space used by extent tree,
4497 * checksum tree and root tree
4498 */
4499static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4500{
4501 struct btrfs_space_info *sinfo;
4502 u64 num_bytes;
4503 u64 meta_used;
4504 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004505 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004506
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004507 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4508 spin_lock(&sinfo->lock);
4509 data_used = sinfo->bytes_used;
4510 spin_unlock(&sinfo->lock);
4511
4512 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4513 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004514 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4515 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004516 meta_used = sinfo->bytes_used;
4517 spin_unlock(&sinfo->lock);
4518
4519 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4520 csum_size * 2;
4521 num_bytes += div64_u64(data_used + meta_used, 50);
4522
4523 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004524 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004525
4526 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4527}
4528
4529static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4530{
4531 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4532 struct btrfs_space_info *sinfo = block_rsv->space_info;
4533 u64 num_bytes;
4534
4535 num_bytes = calc_global_metadata_size(fs_info);
4536
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004537 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004538 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004539
Josef Bacikfdf30d12013-03-26 15:31:45 -04004540 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004541
4542 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004543 sinfo->bytes_reserved + sinfo->bytes_readonly +
4544 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004545
4546 if (sinfo->total_bytes > num_bytes) {
4547 num_bytes = sinfo->total_bytes - num_bytes;
4548 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004549 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004550 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004551 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004552 }
4553
4554 if (block_rsv->reserved >= block_rsv->size) {
4555 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004556 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004557 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004558 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004559 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004560 block_rsv->reserved = block_rsv->size;
4561 block_rsv->full = 1;
4562 }
David Sterba182608c2011-05-05 13:13:16 +02004563
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004564 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004565 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004566}
4567
Yan, Zhengf0486c62010-05-16 10:46:25 -04004568static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4569{
4570 struct btrfs_space_info *space_info;
4571
4572 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4573 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004574
4575 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004576 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004577 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004578 fs_info->trans_block_rsv.space_info = space_info;
4579 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004580 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004581
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004582 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4583 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4584 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4585 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004586 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004587
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004588 update_global_block_rsv(fs_info);
4589}
4590
4591static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4592{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004593 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4594 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004595 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4596 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4597 WARN_ON(fs_info->trans_block_rsv.size > 0);
4598 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4599 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4600 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004601 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4602 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004603}
4604
Yan, Zhenga22285a2010-05-16 10:48:46 -04004605void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4606 struct btrfs_root *root)
4607{
Josef Bacik0e721102012-06-26 16:13:18 -04004608 if (!trans->block_rsv)
4609 return;
4610
Yan, Zhenga22285a2010-05-16 10:48:46 -04004611 if (!trans->bytes_reserved)
4612 return;
4613
Chris Masone77266e2012-02-24 10:39:05 -05004614 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004615 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004616 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004617 trans->bytes_reserved = 0;
4618}
4619
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004620/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004621int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4622 struct inode *inode)
4623{
4624 struct btrfs_root *root = BTRFS_I(inode)->root;
4625 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4626 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4627
4628 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004629 * We need to hold space in order to delete our orphan item once we've
4630 * added it, so this takes the reservation so we can release it later
4631 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004632 */
Chris Masonff5714c2011-05-28 07:00:39 -04004633 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004634 trace_btrfs_space_reservation(root->fs_info, "orphan",
4635 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004636 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4637}
4638
4639void btrfs_orphan_release_metadata(struct inode *inode)
4640{
4641 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004642 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004643 trace_btrfs_space_reservation(root->fs_info, "orphan",
4644 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004645 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4646}
4647
Miao Xied5c12072013-02-28 10:04:33 +00004648/*
4649 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4650 * root: the root of the parent directory
4651 * rsv: block reservation
4652 * items: the number of items that we need do reservation
4653 * qgroup_reserved: used to return the reserved size in qgroup
4654 *
4655 * This function is used to reserve the space for snapshot/subvolume
4656 * creation and deletion. Those operations are different with the
4657 * common file/directory operations, they change two fs/file trees
4658 * and root tree, the number of items that the qgroup reserves is
4659 * different with the free space reservation. So we can not use
4660 * the space reseravtion mechanism in start_transaction().
4661 */
4662int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4663 struct btrfs_block_rsv *rsv,
4664 int items,
4665 u64 *qgroup_reserved)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004666{
Miao Xied5c12072013-02-28 10:04:33 +00004667 u64 num_bytes;
4668 int ret;
4669
4670 if (root->fs_info->quota_enabled) {
4671 /* One for parent inode, two for dir entries */
4672 num_bytes = 3 * root->leafsize;
4673 ret = btrfs_qgroup_reserve(root, num_bytes);
4674 if (ret)
4675 return ret;
4676 } else {
4677 num_bytes = 0;
4678 }
4679
4680 *qgroup_reserved = num_bytes;
4681
4682 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4683 rsv->space_info = __find_space_info(root->fs_info,
4684 BTRFS_BLOCK_GROUP_METADATA);
4685 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4686 BTRFS_RESERVE_FLUSH_ALL);
4687 if (ret) {
4688 if (*qgroup_reserved)
4689 btrfs_qgroup_free(root, *qgroup_reserved);
4690 }
4691
4692 return ret;
4693}
4694
4695void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4696 struct btrfs_block_rsv *rsv,
4697 u64 qgroup_reserved)
4698{
4699 btrfs_block_rsv_release(root, rsv, (u64)-1);
4700 if (qgroup_reserved)
4701 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004702}
4703
Josef Bacik7709cde2011-08-04 10:25:02 -04004704/**
4705 * drop_outstanding_extent - drop an outstanding extent
4706 * @inode: the inode we're dropping the extent for
4707 *
4708 * This is called when we are freeing up an outstanding extent, either called
4709 * after an error or after an extent is written. This will return the number of
4710 * reserved extents that need to be freed. This must be called with
4711 * BTRFS_I(inode)->lock held.
4712 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004713static unsigned drop_outstanding_extent(struct inode *inode)
4714{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004715 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004716 unsigned dropped_extents = 0;
4717
Josef Bacik9e0baf62011-07-15 15:16:44 +00004718 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4719 BTRFS_I(inode)->outstanding_extents--;
4720
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004721 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004722 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4723 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004724 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004725
Josef Bacik9e0baf62011-07-15 15:16:44 +00004726 /*
4727 * If we have more or the same amount of outsanding extents than we have
4728 * reserved then we need to leave the reserved extents count alone.
4729 */
4730 if (BTRFS_I(inode)->outstanding_extents >=
4731 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004732 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004733
4734 dropped_extents = BTRFS_I(inode)->reserved_extents -
4735 BTRFS_I(inode)->outstanding_extents;
4736 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004737 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004738}
4739
Josef Bacik7709cde2011-08-04 10:25:02 -04004740/**
4741 * calc_csum_metadata_size - return the amount of metada space that must be
4742 * reserved/free'd for the given bytes.
4743 * @inode: the inode we're manipulating
4744 * @num_bytes: the number of bytes in question
4745 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4746 *
4747 * This adjusts the number of csum_bytes in the inode and then returns the
4748 * correct amount of metadata that must either be reserved or freed. We
4749 * calculate how many checksums we can fit into one leaf and then divide the
4750 * number of bytes that will need to be checksumed by this value to figure out
4751 * how many checksums will be required. If we are adding bytes then the number
4752 * may go up and we will return the number of additional bytes that must be
4753 * reserved. If it is going down we will return the number of bytes that must
4754 * be freed.
4755 *
4756 * This must be called with BTRFS_I(inode)->lock held.
4757 */
4758static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4759 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004760{
Josef Bacik7709cde2011-08-04 10:25:02 -04004761 struct btrfs_root *root = BTRFS_I(inode)->root;
4762 u64 csum_size;
4763 int num_csums_per_leaf;
4764 int num_csums;
4765 int old_csums;
4766
4767 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4768 BTRFS_I(inode)->csum_bytes == 0)
4769 return 0;
4770
4771 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4772 if (reserve)
4773 BTRFS_I(inode)->csum_bytes += num_bytes;
4774 else
4775 BTRFS_I(inode)->csum_bytes -= num_bytes;
4776 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4777 num_csums_per_leaf = (int)div64_u64(csum_size,
4778 sizeof(struct btrfs_csum_item) +
4779 sizeof(struct btrfs_disk_key));
4780 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4781 num_csums = num_csums + num_csums_per_leaf - 1;
4782 num_csums = num_csums / num_csums_per_leaf;
4783
4784 old_csums = old_csums + num_csums_per_leaf - 1;
4785 old_csums = old_csums / num_csums_per_leaf;
4786
4787 /* No change, no need to reserve more */
4788 if (old_csums == num_csums)
4789 return 0;
4790
4791 if (reserve)
4792 return btrfs_calc_trans_metadata_size(root,
4793 num_csums - old_csums);
4794
4795 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004796}
4797
4798int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4799{
4800 struct btrfs_root *root = BTRFS_I(inode)->root;
4801 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004802 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004803 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004804 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004805 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004806 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004807 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004808 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004809 u64 to_free = 0;
4810 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004811
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004812 /* If we are a free space inode we need to not flush since we will be in
4813 * the middle of a transaction commit. We also don't need the delalloc
4814 * mutex since we won't race with anybody. We need this mostly to make
4815 * lockdep shut its filthy mouth.
4816 */
4817 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004818 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004819 delalloc_lock = false;
4820 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004821
Miao Xie08e007d2012-10-16 11:33:38 +00004822 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4823 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004824 schedule_timeout(1);
4825
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004826 if (delalloc_lock)
4827 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4828
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004829 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004830
Josef Bacik9e0baf62011-07-15 15:16:44 +00004831 spin_lock(&BTRFS_I(inode)->lock);
4832 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004833
Josef Bacik9e0baf62011-07-15 15:16:44 +00004834 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004835 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004836 nr_extents = BTRFS_I(inode)->outstanding_extents -
4837 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004838
4839 /*
4840 * Add an item to reserve for updating the inode when we complete the
4841 * delalloc io.
4842 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004843 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4844 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004845 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004846 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004847 }
4848
4849 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004850 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004851 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004852 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004853
Wang Shilong88e081bf2013-03-01 11:36:01 +00004854 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02004855 ret = btrfs_qgroup_reserve(root, num_bytes +
4856 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004857 if (ret)
4858 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00004859 }
Arne Jansenc5567232011-09-14 15:44:05 +02004860
Wang Shilong88e081bf2013-03-01 11:36:01 +00004861 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4862 if (unlikely(ret)) {
4863 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00004864 btrfs_qgroup_free(root, num_bytes +
4865 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004866 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004867 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004868
Josef Bacik660d3f62011-12-09 11:18:51 -05004869 spin_lock(&BTRFS_I(inode)->lock);
4870 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004871 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4872 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004873 nr_extents--;
4874 }
4875 BTRFS_I(inode)->reserved_extents += nr_extents;
4876 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004877
4878 if (delalloc_lock)
4879 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004880
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004881 if (to_reserve)
4882 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4883 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004884 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4885
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004886 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004887
4888out_fail:
4889 spin_lock(&BTRFS_I(inode)->lock);
4890 dropped = drop_outstanding_extent(inode);
4891 /*
4892 * If the inodes csum_bytes is the same as the original
4893 * csum_bytes then we know we haven't raced with any free()ers
4894 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00004895 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04004896 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00004897 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04004898 } else {
4899 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
4900 u64 bytes;
4901
4902 /*
4903 * This is tricky, but first we need to figure out how much we
4904 * free'd from any free-ers that occured during this
4905 * reservation, so we reset ->csum_bytes to the csum_bytes
4906 * before we dropped our lock, and then call the free for the
4907 * number of bytes that were freed while we were trying our
4908 * reservation.
4909 */
4910 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
4911 BTRFS_I(inode)->csum_bytes = csum_bytes;
4912 to_free = calc_csum_metadata_size(inode, bytes, 0);
4913
4914
4915 /*
4916 * Now we need to see how much we would have freed had we not
4917 * been making this reservation and our ->csum_bytes were not
4918 * artificially inflated.
4919 */
4920 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
4921 bytes = csum_bytes - orig_csum_bytes;
4922 bytes = calc_csum_metadata_size(inode, bytes, 0);
4923
4924 /*
4925 * Now reset ->csum_bytes to what it should be. If bytes is
4926 * more than to_free then we would have free'd more space had we
4927 * not had an artificially high ->csum_bytes, so we need to free
4928 * the remainder. If bytes is the same or less then we don't
4929 * need to do anything, the other free-ers did the correct
4930 * thing.
4931 */
4932 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
4933 if (bytes > to_free)
4934 to_free = bytes - to_free;
4935 else
4936 to_free = 0;
4937 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00004938 spin_unlock(&BTRFS_I(inode)->lock);
4939 if (dropped)
4940 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4941
4942 if (to_free) {
4943 btrfs_block_rsv_release(root, block_rsv, to_free);
4944 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4945 btrfs_ino(inode), to_free, 0);
4946 }
4947 if (delalloc_lock)
4948 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4949 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004950}
4951
Josef Bacik7709cde2011-08-04 10:25:02 -04004952/**
4953 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4954 * @inode: the inode to release the reservation for
4955 * @num_bytes: the number of bytes we're releasing
4956 *
4957 * This will release the metadata reservation for an inode. This can be called
4958 * once we complete IO for a given set of bytes to release their metadata
4959 * reservations.
4960 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004961void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4962{
4963 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004964 u64 to_free = 0;
4965 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004966
4967 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004968 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004969 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004970
Miao Xie09348562013-02-07 10:12:07 +00004971 if (num_bytes)
4972 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04004973 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004974 if (dropped > 0)
4975 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004976
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004977 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4978 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004979 if (root->fs_info->quota_enabled) {
4980 btrfs_qgroup_free(root, num_bytes +
4981 dropped * root->leafsize);
4982 }
4983
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004984 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4985 to_free);
4986}
4987
Josef Bacik7709cde2011-08-04 10:25:02 -04004988/**
4989 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4990 * @inode: inode we're writing to
4991 * @num_bytes: the number of bytes we want to allocate
4992 *
4993 * This will do the following things
4994 *
4995 * o reserve space in the data space info for num_bytes
4996 * o reserve space in the metadata space info based on number of outstanding
4997 * extents and how much csums will be needed
4998 * o add to the inodes ->delalloc_bytes
4999 * o add it to the fs_info's delalloc inodes list.
5000 *
5001 * This will return 0 for success and -ENOSPC if there is no space left.
5002 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005003int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5004{
5005 int ret;
5006
5007 ret = btrfs_check_data_free_space(inode, num_bytes);
5008 if (ret)
5009 return ret;
5010
5011 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5012 if (ret) {
5013 btrfs_free_reserved_data_space(inode, num_bytes);
5014 return ret;
5015 }
5016
5017 return 0;
5018}
5019
Josef Bacik7709cde2011-08-04 10:25:02 -04005020/**
5021 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5022 * @inode: inode we're releasing space for
5023 * @num_bytes: the number of bytes we want to free up
5024 *
5025 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5026 * called in the case that we don't need the metadata AND data reservations
5027 * anymore. So if there is an error or we insert an inline extent.
5028 *
5029 * This function will release the metadata space that was not used and will
5030 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5031 * list if there are no delalloc bytes left.
5032 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005033void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5034{
5035 btrfs_delalloc_release_metadata(inode, num_bytes);
5036 btrfs_free_reserved_data_space(inode, num_bytes);
5037}
5038
Liu Boc53d6132012-12-27 09:01:19 +00005039static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005040 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005041{
Josef Bacik0af3d002010-06-21 14:48:16 -04005042 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005043 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005044 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005045 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005046 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005047 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005048
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005049 /* block accounting for super block */
5050 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02005051 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005052 if (alloc)
5053 old_val += num_bytes;
5054 else
5055 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005056 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005057 spin_unlock(&info->delalloc_lock);
5058
Chris Masond3977122009-01-05 21:25:51 -05005059 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005060 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005061 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005062 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005063 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5064 BTRFS_BLOCK_GROUP_RAID1 |
5065 BTRFS_BLOCK_GROUP_RAID10))
5066 factor = 2;
5067 else
5068 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005069 /*
5070 * If this block group has free space cache written out, we
5071 * need to make sure to load it if we are removing space. This
5072 * is because we need the unpinning stage to actually add the
5073 * space back to the block group, otherwise we will leak space.
5074 */
5075 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005076 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005077
Chris Masondb945352007-10-15 16:15:53 -04005078 byte_in_group = bytenr - cache->key.objectid;
5079 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005080
Josef Bacik25179202008-10-29 14:49:05 -04005081 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005082 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005083
Josef Bacik73bc1872011-10-03 14:07:49 -04005084 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005085 cache->disk_cache_state < BTRFS_DC_CLEAR)
5086 cache->disk_cache_state = BTRFS_DC_CLEAR;
5087
Josef Bacik0f9dd462008-09-23 13:14:11 -04005088 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005089 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005090 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005091 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005092 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005093 btrfs_set_block_group_used(&cache->item, old_val);
5094 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005095 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005096 cache->space_info->bytes_used += num_bytes;
5097 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005098 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005099 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005100 } else {
Chris Masondb945352007-10-15 16:15:53 -04005101 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005102 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005103 cache->pinned += num_bytes;
5104 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005105 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005106 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005107 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005108 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005109
Yan, Zhengf0486c62010-05-16 10:46:25 -04005110 set_extent_dirty(info->pinned_extents,
5111 bytenr, bytenr + num_bytes - 1,
5112 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005113 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005114 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005115 total -= num_bytes;
5116 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005117 }
5118 return 0;
5119}
Chris Mason6324fbf2008-03-24 15:01:59 -04005120
Chris Masona061fc82008-05-07 11:43:44 -04005121static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5122{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005123 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005124 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005125
Liu Boa1897fd2012-12-27 09:01:23 +00005126 spin_lock(&root->fs_info->block_group_cache_lock);
5127 bytenr = root->fs_info->first_logical_byte;
5128 spin_unlock(&root->fs_info->block_group_cache_lock);
5129
5130 if (bytenr < (u64)-1)
5131 return bytenr;
5132
Josef Bacik0f9dd462008-09-23 13:14:11 -04005133 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5134 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005135 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005136
Yan Zhengd2fb3432008-12-11 16:30:39 -05005137 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005138 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005139
5140 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005141}
5142
Yan, Zhengf0486c62010-05-16 10:46:25 -04005143static int pin_down_extent(struct btrfs_root *root,
5144 struct btrfs_block_group_cache *cache,
5145 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005146{
Yan Zheng11833d62009-09-11 16:11:19 -04005147 spin_lock(&cache->space_info->lock);
5148 spin_lock(&cache->lock);
5149 cache->pinned += num_bytes;
5150 cache->space_info->bytes_pinned += num_bytes;
5151 if (reserved) {
5152 cache->reserved -= num_bytes;
5153 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005154 }
Yan Zheng11833d62009-09-11 16:11:19 -04005155 spin_unlock(&cache->lock);
5156 spin_unlock(&cache->space_info->lock);
5157
Yan, Zhengf0486c62010-05-16 10:46:25 -04005158 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5159 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005160 return 0;
5161}
Chris Mason9078a3e2007-04-26 16:46:15 -04005162
Yan, Zhengf0486c62010-05-16 10:46:25 -04005163/*
5164 * this function must be called within transaction
5165 */
5166int btrfs_pin_extent(struct btrfs_root *root,
5167 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005168{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005169 struct btrfs_block_group_cache *cache;
5170
5171 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005172 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005173
5174 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5175
5176 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005177 return 0;
5178}
Zheng Yane8569812008-09-26 10:05:48 -04005179
Yan, Zhengf0486c62010-05-16 10:46:25 -04005180/*
Chris Masone688b7252011-10-31 20:52:39 -04005181 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005182 */
Liu Bodcfac412012-12-27 09:01:20 +00005183int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005184 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005185{
Chris Masone688b7252011-10-31 20:52:39 -04005186 struct btrfs_block_group_cache *cache;
5187
5188 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005189 BUG_ON(!cache); /* Logic error */
Chris Masone688b7252011-10-31 20:52:39 -04005190
5191 /*
5192 * pull in the free space cache (if any) so that our pin
5193 * removes the free space from the cache. We have load_only set
5194 * to one because the slow code to read in the free extents does check
5195 * the pinned extents.
5196 */
Liu Bof6373bf2012-12-27 09:01:18 +00005197 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005198
5199 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5200
5201 /* remove us from the free space cache (if we're there at all) */
5202 btrfs_remove_free_space(cache, bytenr, num_bytes);
5203 btrfs_put_block_group(cache);
5204 return 0;
5205}
5206
Josef Bacikfb25e912011-07-26 17:00:46 -04005207/**
5208 * btrfs_update_reserved_bytes - update the block_group and space info counters
5209 * @cache: The cache we are manipulating
5210 * @num_bytes: The number of bytes in question
5211 * @reserve: One of the reservation enums
5212 *
5213 * This is called by the allocator when it reserves space, or by somebody who is
5214 * freeing space that was never actually used on disk. For example if you
5215 * reserve some space for a new leaf in transaction A and before transaction A
5216 * commits you free that leaf, you call this with reserve set to 0 in order to
5217 * clear the reservation.
5218 *
5219 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5220 * ENOSPC accounting. For data we handle the reservation through clearing the
5221 * delalloc bits in the io_tree. We have to do this since we could end up
5222 * allocating less disk space for the amount of data we have reserved in the
5223 * case of compression.
5224 *
5225 * If this is a reservation and the block group has become read only we cannot
5226 * make the reservation and return -EAGAIN, otherwise this function always
5227 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005228 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005229static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5230 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005231{
Josef Bacikfb25e912011-07-26 17:00:46 -04005232 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005233 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005234
Josef Bacikfb25e912011-07-26 17:00:46 -04005235 spin_lock(&space_info->lock);
5236 spin_lock(&cache->lock);
5237 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005238 if (cache->ro) {
5239 ret = -EAGAIN;
5240 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005241 cache->reserved += num_bytes;
5242 space_info->bytes_reserved += num_bytes;
5243 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005244 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005245 "space_info", space_info->flags,
5246 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005247 space_info->bytes_may_use -= num_bytes;
5248 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005249 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005250 } else {
5251 if (cache->ro)
5252 space_info->bytes_readonly += num_bytes;
5253 cache->reserved -= num_bytes;
5254 space_info->bytes_reserved -= num_bytes;
5255 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005256 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005257 spin_unlock(&cache->lock);
5258 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005259 return ret;
5260}
5261
Jeff Mahoney143bede2012-03-01 14:56:26 +01005262void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005263 struct btrfs_root *root)
5264{
5265 struct btrfs_fs_info *fs_info = root->fs_info;
5266 struct btrfs_caching_control *next;
5267 struct btrfs_caching_control *caching_ctl;
5268 struct btrfs_block_group_cache *cache;
5269
5270 down_write(&fs_info->extent_commit_sem);
5271
5272 list_for_each_entry_safe(caching_ctl, next,
5273 &fs_info->caching_block_groups, list) {
5274 cache = caching_ctl->block_group;
5275 if (block_group_cache_done(cache)) {
5276 cache->last_byte_to_unpin = (u64)-1;
5277 list_del_init(&caching_ctl->list);
5278 put_caching_control(caching_ctl);
5279 } else {
5280 cache->last_byte_to_unpin = caching_ctl->progress;
5281 }
5282 }
5283
5284 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5285 fs_info->pinned_extents = &fs_info->freed_extents[1];
5286 else
5287 fs_info->pinned_extents = &fs_info->freed_extents[0];
5288
5289 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005290
5291 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005292}
5293
5294static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5295{
5296 struct btrfs_fs_info *fs_info = root->fs_info;
5297 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005298 struct btrfs_space_info *space_info;
5299 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005300 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005301 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005302
5303 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005304 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005305 if (!cache ||
5306 start >= cache->key.objectid + cache->key.offset) {
5307 if (cache)
5308 btrfs_put_block_group(cache);
5309 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005310 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005311 }
5312
5313 len = cache->key.objectid + cache->key.offset - start;
5314 len = min(len, end + 1 - start);
5315
5316 if (start < cache->last_byte_to_unpin) {
5317 len = min(len, cache->last_byte_to_unpin - start);
5318 btrfs_add_free_space(cache, start, len);
5319 }
Josef Bacik25179202008-10-29 14:49:05 -04005320
Yan, Zhengf0486c62010-05-16 10:46:25 -04005321 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005322 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005323
Josef Bacik7b398f82012-10-22 15:52:28 -04005324 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005325 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005326 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005327 space_info->bytes_pinned -= len;
5328 if (cache->ro) {
5329 space_info->bytes_readonly += len;
5330 readonly = true;
5331 }
Josef Bacik25179202008-10-29 14:49:05 -04005332 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005333 if (!readonly && global_rsv->space_info == space_info) {
5334 spin_lock(&global_rsv->lock);
5335 if (!global_rsv->full) {
5336 len = min(len, global_rsv->size -
5337 global_rsv->reserved);
5338 global_rsv->reserved += len;
5339 space_info->bytes_may_use += len;
5340 if (global_rsv->reserved >= global_rsv->size)
5341 global_rsv->full = 1;
5342 }
5343 spin_unlock(&global_rsv->lock);
5344 }
5345 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005346 }
5347
5348 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005349 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005350 return 0;
5351}
5352
5353int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005354 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005355{
Yan Zheng11833d62009-09-11 16:11:19 -04005356 struct btrfs_fs_info *fs_info = root->fs_info;
5357 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005358 u64 start;
5359 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005360 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005361
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005362 if (trans->aborted)
5363 return 0;
5364
Yan Zheng11833d62009-09-11 16:11:19 -04005365 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5366 unpin = &fs_info->freed_extents[1];
5367 else
5368 unpin = &fs_info->freed_extents[0];
5369
Chris Masond3977122009-01-05 21:25:51 -05005370 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005371 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005372 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005373 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005374 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005375
Li Dongyang5378e602011-03-24 10:24:27 +00005376 if (btrfs_test_opt(root, DISCARD))
5377 ret = btrfs_discard_extent(root, start,
5378 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005379
Chris Mason1a5bc162007-10-15 16:15:26 -04005380 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005381 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005382 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005383 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005384
Chris Masone20d96d2007-03-22 12:13:20 -04005385 return 0;
5386}
5387
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005388static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5389 struct btrfs_root *root,
5390 u64 bytenr, u64 num_bytes, u64 parent,
5391 u64 root_objectid, u64 owner_objectid,
5392 u64 owner_offset, int refs_to_drop,
5393 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005394{
Chris Masone2fa7222007-03-12 16:22:34 -04005395 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005396 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005397 struct btrfs_fs_info *info = root->fs_info;
5398 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005399 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005400 struct btrfs_extent_item *ei;
5401 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005402 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005403 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005404 int extent_slot = 0;
5405 int found_extent = 0;
5406 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005407 u32 item_size;
5408 u64 refs;
Josef Bacik3173a182013-03-07 14:22:04 -05005409 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5410 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005411
Chris Mason5caf2a02007-04-02 11:20:42 -04005412 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005413 if (!path)
5414 return -ENOMEM;
5415
Chris Mason3c12ac72008-04-21 12:01:38 -04005416 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005417 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005418
5419 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5420 BUG_ON(!is_data && refs_to_drop != 1);
5421
Josef Bacik3173a182013-03-07 14:22:04 -05005422 if (is_data)
5423 skinny_metadata = 0;
5424
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005425 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5426 bytenr, num_bytes, parent,
5427 root_objectid, owner_objectid,
5428 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005429 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005430 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005431 while (extent_slot >= 0) {
5432 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005433 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005434 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005435 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005436 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5437 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005438 found_extent = 1;
5439 break;
5440 }
Josef Bacik3173a182013-03-07 14:22:04 -05005441 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5442 key.offset == owner_objectid) {
5443 found_extent = 1;
5444 break;
5445 }
Chris Mason952fcca2008-02-18 16:33:44 -05005446 if (path->slots[0] - extent_slot > 5)
5447 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005448 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005449 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005450#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5451 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5452 if (found_extent && item_size < sizeof(*ei))
5453 found_extent = 0;
5454#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005455 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005456 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005457 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005458 NULL, refs_to_drop,
5459 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005460 if (ret) {
5461 btrfs_abort_transaction(trans, extent_root, ret);
5462 goto out;
5463 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005464 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005465 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005466
5467 key.objectid = bytenr;
5468 key.type = BTRFS_EXTENT_ITEM_KEY;
5469 key.offset = num_bytes;
5470
Josef Bacik3173a182013-03-07 14:22:04 -05005471 if (!is_data && skinny_metadata) {
5472 key.type = BTRFS_METADATA_ITEM_KEY;
5473 key.offset = owner_objectid;
5474 }
5475
Zheng Yan31840ae2008-09-23 13:14:14 -04005476 ret = btrfs_search_slot(trans, extent_root,
5477 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005478 if (ret > 0 && skinny_metadata && path->slots[0]) {
5479 /*
5480 * Couldn't find our skinny metadata item,
5481 * see if we have ye olde extent item.
5482 */
5483 path->slots[0]--;
5484 btrfs_item_key_to_cpu(path->nodes[0], &key,
5485 path->slots[0]);
5486 if (key.objectid == bytenr &&
5487 key.type == BTRFS_EXTENT_ITEM_KEY &&
5488 key.offset == num_bytes)
5489 ret = 0;
5490 }
5491
5492 if (ret > 0 && skinny_metadata) {
5493 skinny_metadata = false;
5494 key.type = BTRFS_EXTENT_ITEM_KEY;
5495 key.offset = num_bytes;
5496 btrfs_release_path(path);
5497 ret = btrfs_search_slot(trans, extent_root,
5498 &key, path, -1, 1);
5499 }
5500
Josef Bacikf3465ca2008-11-12 14:19:50 -05005501 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005502 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5503 ret, (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005504 if (ret > 0)
5505 btrfs_print_leaf(extent_root,
5506 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005507 }
David Sterba005d6422012-09-18 07:52:32 -06005508 if (ret < 0) {
5509 btrfs_abort_transaction(trans, extent_root, ret);
5510 goto out;
5511 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005512 extent_slot = path->slots[0];
5513 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005514 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005515 btrfs_print_leaf(extent_root, path->nodes[0]);
5516 WARN_ON(1);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005517 btrfs_err(info,
5518 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5519 (unsigned long long)bytenr,
5520 (unsigned long long)parent,
5521 (unsigned long long)root_objectid,
5522 (unsigned long long)owner_objectid,
5523 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005524 } else {
David Sterba005d6422012-09-18 07:52:32 -06005525 btrfs_abort_transaction(trans, extent_root, ret);
5526 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005527 }
Chris Mason5f39d392007-10-15 16:14:19 -04005528
5529 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005530 item_size = btrfs_item_size_nr(leaf, extent_slot);
5531#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5532 if (item_size < sizeof(*ei)) {
5533 BUG_ON(found_extent || extent_slot != path->slots[0]);
5534 ret = convert_extent_item_v0(trans, extent_root, path,
5535 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005536 if (ret < 0) {
5537 btrfs_abort_transaction(trans, extent_root, ret);
5538 goto out;
5539 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005540
David Sterbab3b4aa72011-04-21 01:20:15 +02005541 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005542 path->leave_spinning = 1;
5543
5544 key.objectid = bytenr;
5545 key.type = BTRFS_EXTENT_ITEM_KEY;
5546 key.offset = num_bytes;
5547
5548 ret = btrfs_search_slot(trans, extent_root, &key, path,
5549 -1, 1);
5550 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005551 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5552 ret, (unsigned long long)bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005553 btrfs_print_leaf(extent_root, path->nodes[0]);
5554 }
David Sterba005d6422012-09-18 07:52:32 -06005555 if (ret < 0) {
5556 btrfs_abort_transaction(trans, extent_root, ret);
5557 goto out;
5558 }
5559
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560 extent_slot = path->slots[0];
5561 leaf = path->nodes[0];
5562 item_size = btrfs_item_size_nr(leaf, extent_slot);
5563 }
5564#endif
5565 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005566 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005567 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005568 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5569 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 struct btrfs_tree_block_info *bi;
5571 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5572 bi = (struct btrfs_tree_block_info *)(ei + 1);
5573 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005574 }
5575
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 refs = btrfs_extent_refs(leaf, ei);
5577 BUG_ON(refs < refs_to_drop);
5578 refs -= refs_to_drop;
5579
5580 if (refs > 0) {
5581 if (extent_op)
5582 __run_delayed_extent_op(extent_op, leaf, ei);
5583 /*
5584 * In the case of inline back ref, reference count will
5585 * be updated by remove_extent_backref
5586 */
5587 if (iref) {
5588 BUG_ON(!found_extent);
5589 } else {
5590 btrfs_set_extent_refs(leaf, ei, refs);
5591 btrfs_mark_buffer_dirty(leaf);
5592 }
5593 if (found_extent) {
5594 ret = remove_extent_backref(trans, extent_root, path,
5595 iref, refs_to_drop,
5596 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005597 if (ret) {
5598 btrfs_abort_transaction(trans, extent_root, ret);
5599 goto out;
5600 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 }
5602 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 if (found_extent) {
5604 BUG_ON(is_data && refs_to_drop !=
5605 extent_data_ref_count(root, path, iref));
5606 if (iref) {
5607 BUG_ON(path->slots[0] != extent_slot);
5608 } else {
5609 BUG_ON(path->slots[0] != extent_slot + 1);
5610 path->slots[0] = extent_slot;
5611 num_to_del = 2;
5612 }
Chris Mason78fae272007-03-25 11:35:08 -04005613 }
Chris Masonb9473432009-03-13 11:00:37 -04005614
Chris Mason952fcca2008-02-18 16:33:44 -05005615 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5616 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005617 if (ret) {
5618 btrfs_abort_transaction(trans, extent_root, ret);
5619 goto out;
5620 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005621 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005622
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005623 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005624 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005625 if (ret) {
5626 btrfs_abort_transaction(trans, extent_root, ret);
5627 goto out;
5628 }
Chris Mason459931e2008-12-10 09:10:46 -05005629 }
5630
Liu Boc53d6132012-12-27 09:01:19 +00005631 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005632 if (ret) {
5633 btrfs_abort_transaction(trans, extent_root, ret);
5634 goto out;
5635 }
Chris Masona28ec192007-03-06 20:08:01 -05005636 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005637out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005638 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005639 return ret;
5640}
5641
5642/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005643 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005644 * delayed ref for that extent as well. This searches the delayed ref tree for
5645 * a given extent, and if there are no other delayed refs to be processed, it
5646 * removes it from the tree.
5647 */
5648static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5649 struct btrfs_root *root, u64 bytenr)
5650{
5651 struct btrfs_delayed_ref_head *head;
5652 struct btrfs_delayed_ref_root *delayed_refs;
5653 struct btrfs_delayed_ref_node *ref;
5654 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005655 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005656
5657 delayed_refs = &trans->transaction->delayed_refs;
5658 spin_lock(&delayed_refs->lock);
5659 head = btrfs_find_delayed_ref_head(trans, bytenr);
5660 if (!head)
5661 goto out;
5662
5663 node = rb_prev(&head->node.rb_node);
5664 if (!node)
5665 goto out;
5666
5667 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5668
5669 /* there are still entries for this ref, we can't drop it */
5670 if (ref->bytenr == bytenr)
5671 goto out;
5672
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005673 if (head->extent_op) {
5674 if (!head->must_insert_reserved)
5675 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005676 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005677 head->extent_op = NULL;
5678 }
5679
Chris Mason1887be62009-03-13 10:11:24 -04005680 /*
5681 * waiting for the lock here would deadlock. If someone else has it
5682 * locked they are already in the process of dropping it anyway
5683 */
5684 if (!mutex_trylock(&head->mutex))
5685 goto out;
5686
5687 /*
5688 * at this point we have a head with no other entries. Go
5689 * ahead and process it.
5690 */
5691 head->node.in_tree = 0;
5692 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005693
Chris Mason1887be62009-03-13 10:11:24 -04005694 delayed_refs->num_entries--;
5695
5696 /*
5697 * we don't take a ref on the node because we're removing it from the
5698 * tree, so we just steal the ref the tree was holding.
5699 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005700 delayed_refs->num_heads--;
5701 if (list_empty(&head->cluster))
5702 delayed_refs->num_heads_ready--;
5703
5704 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005705 spin_unlock(&delayed_refs->lock);
5706
Yan, Zhengf0486c62010-05-16 10:46:25 -04005707 BUG_ON(head->extent_op);
5708 if (head->must_insert_reserved)
5709 ret = 1;
5710
5711 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005712 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005713 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005714out:
5715 spin_unlock(&delayed_refs->lock);
5716 return 0;
5717}
5718
Yan, Zhengf0486c62010-05-16 10:46:25 -04005719void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5720 struct btrfs_root *root,
5721 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005722 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005723{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005724 struct btrfs_block_group_cache *cache = NULL;
5725 int ret;
5726
5727 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005728 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5729 buf->start, buf->len,
5730 parent, root->root_key.objectid,
5731 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005732 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005733 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005734 }
5735
5736 if (!last_ref)
5737 return;
5738
Yan, Zhengf0486c62010-05-16 10:46:25 -04005739 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005740
5741 if (btrfs_header_generation(buf) == trans->transid) {
5742 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5743 ret = check_ref_cleanup(trans, root, buf->start);
5744 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005745 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005746 }
5747
5748 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5749 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005750 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005751 }
5752
5753 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5754
5755 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005756 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005757 }
5758out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005759 /*
5760 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5761 * anymore.
5762 */
5763 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005764 btrfs_put_block_group(cache);
5765}
5766
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005767/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005768int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5769 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5770 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005771{
5772 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005773 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005774
Chris Mason56bec292009-03-13 10:10:06 -04005775 /*
5776 * tree log blocks never actually go into the extent allocation
5777 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005778 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005779 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5780 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005781 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005782 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005783 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005784 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005785 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5786 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005787 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005788 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005789 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005790 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5791 num_bytes,
5792 parent, root_objectid, owner,
5793 offset, BTRFS_DROP_DELAYED_REF,
5794 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005795 }
Chris Mason925baed2008-06-25 16:01:30 -04005796 return ret;
5797}
5798
David Woodhouse53b381b2013-01-29 18:40:14 -05005799static u64 stripe_align(struct btrfs_root *root,
5800 struct btrfs_block_group_cache *cache,
5801 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05005802{
Qu Wenruofda28322013-02-26 08:10:22 +00005803 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05005804 return ret;
5805}
5806
Chris Masonfec577f2007-02-26 10:40:21 -05005807/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005808 * when we wait for progress in the block group caching, its because
5809 * our allocation attempt failed at least once. So, we must sleep
5810 * and let some progress happen before we try again.
5811 *
5812 * This function will sleep at least once waiting for new free space to
5813 * show up, and then it will check the block group free space numbers
5814 * for our min num_bytes. Another option is to have it go ahead
5815 * and look in the rbtree for a free extent of a given size, but this
5816 * is a good start.
5817 */
5818static noinline int
5819wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5820 u64 num_bytes)
5821{
Yan Zheng11833d62009-09-11 16:11:19 -04005822 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005823
Yan Zheng11833d62009-09-11 16:11:19 -04005824 caching_ctl = get_caching_control(cache);
5825 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005826 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005827
Yan Zheng11833d62009-09-11 16:11:19 -04005828 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005829 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005830
5831 put_caching_control(caching_ctl);
5832 return 0;
5833}
5834
5835static noinline int
5836wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5837{
5838 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04005839
5840 caching_ctl = get_caching_control(cache);
5841 if (!caching_ctl)
5842 return 0;
5843
5844 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5845
5846 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005847 return 0;
5848}
5849
Liu Bo31e50222012-11-21 14:18:10 +00005850int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005851{
Ilya Dryomov7738a532012-03-27 17:09:17 +03005852 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00005853 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005854 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00005855 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005856 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00005857 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005858 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00005859 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005860 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05005861 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05005862 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05005863 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005864
Chris Masone942f882013-02-20 14:06:05 -05005865 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04005866}
5867
Ilya Dryomov7738a532012-03-27 17:09:17 +03005868static int get_block_group_index(struct btrfs_block_group_cache *cache)
5869{
Liu Bo31e50222012-11-21 14:18:10 +00005870 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005871}
5872
Josef Bacik817d52f2009-07-13 21:29:25 -04005873enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005874 LOOP_CACHING_NOWAIT = 0,
5875 LOOP_CACHING_WAIT = 1,
5876 LOOP_ALLOC_CHUNK = 2,
5877 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005878};
5879
5880/*
Chris Masonfec577f2007-02-26 10:40:21 -05005881 * walks the btree of allocated extents and find a hole of a given size.
5882 * The key ins is changed to record the hole:
5883 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005884 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005885 * ins->offset == number of blocks
5886 * Any available blocks before search_start are skipped.
5887 */
Chris Masond3977122009-01-05 21:25:51 -05005888static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005889 struct btrfs_root *orig_root,
5890 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005891 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005892 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005893{
Josef Bacik80eb2342008-10-29 14:49:05 -04005894 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005895 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005896 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005897 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005898 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005899 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005900 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005901 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005902 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005903 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005904 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5905 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005906 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005907 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005908 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005909 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005910 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005911
Chris Masondb945352007-10-15 16:15:53 -04005912 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005913 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005914 ins->objectid = 0;
5915 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005916
Josef Bacik3f7de032011-11-10 08:29:20 -05005917 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5918
Josef Bacik2552d172009-04-03 10:14:19 -04005919 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005920 if (!space_info) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005921 btrfs_err(root->fs_info, "No space info for %llu", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005922 return -ENOSPC;
5923 }
Josef Bacik2552d172009-04-03 10:14:19 -04005924
Josef Bacik67377732010-09-16 16:19:09 -04005925 /*
5926 * If the space info is for both data and metadata it means we have a
5927 * small filesystem and we can't use the clustering stuff.
5928 */
5929 if (btrfs_mixed_space_info(space_info))
5930 use_cluster = false;
5931
Josef Bacik67377732010-09-16 16:19:09 -04005932 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005933 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005934 if (!btrfs_test_opt(root, SSD))
5935 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005936 }
5937
Josef Bacik67377732010-09-16 16:19:09 -04005938 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5939 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005940 last_ptr = &root->fs_info->data_alloc_cluster;
5941 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005942
Chris Mason239b14b2008-03-24 15:02:07 -04005943 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005944 spin_lock(&last_ptr->lock);
5945 if (last_ptr->block_group)
5946 hint_byte = last_ptr->window_start;
5947 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005948 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005949
Chris Masona061fc82008-05-07 11:43:44 -04005950 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005951 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005952
Josef Bacik817d52f2009-07-13 21:29:25 -04005953 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005954 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005955
Josef Bacik2552d172009-04-03 10:14:19 -04005956 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005957 block_group = btrfs_lookup_block_group(root->fs_info,
5958 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005959 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005960 /*
5961 * we don't want to use the block group if it doesn't match our
5962 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005963 *
5964 * However if we are re-searching with an ideal block group
5965 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005966 */
5967 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005968 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005969 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005970 if (list_empty(&block_group->list) ||
5971 block_group->ro) {
5972 /*
5973 * someone is removing this block group,
5974 * we can't jump into the have_block_group
5975 * target because our list pointers are not
5976 * valid
5977 */
5978 btrfs_put_block_group(block_group);
5979 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005980 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005981 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005982 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005983 }
Josef Bacik2552d172009-04-03 10:14:19 -04005984 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005985 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005986 }
Chris Mason42e70e72008-11-07 18:17:11 -05005987 }
Josef Bacik2552d172009-04-03 10:14:19 -04005988search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005989 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005990 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005991 list_for_each_entry(block_group, &space_info->block_groups[index],
5992 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005993 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005994 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005995
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005996 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005997 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005998 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005999
Chris Mason83a50de2010-12-13 15:06:46 -05006000 /*
6001 * this can happen if we end up cycling through all the
6002 * raid types, but we want to make sure we only allocate
6003 * for the proper type.
6004 */
6005 if (!block_group_bits(block_group, data)) {
6006 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6007 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006008 BTRFS_BLOCK_GROUP_RAID5 |
6009 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006010 BTRFS_BLOCK_GROUP_RAID10;
6011
6012 /*
6013 * if they asked for extra copies and this block group
6014 * doesn't provide them, bail. This does allow us to
6015 * fill raid0 from raid1.
6016 */
6017 if ((data & extra) && !(block_group->flags & extra))
6018 goto loop;
6019 }
6020
Josef Bacik2552d172009-04-03 10:14:19 -04006021have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006022 cached = block_group_cache_done(block_group);
6023 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05006024 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00006025 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006026 BUG_ON(ret < 0);
6027 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006028 }
6029
Josef Bacikea6a4782008-11-20 12:16:16 -05006030 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006031 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006032
Josef Bacik0a243252009-09-11 16:11:20 -04006033 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006034 * Ok we want to try and use the cluster allocator, so
6035 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006036 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006037 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05006038 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006039 /*
6040 * the refill lock keeps out other
6041 * people trying to start a new cluster
6042 */
6043 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006044 used_block_group = last_ptr->block_group;
6045 if (used_block_group != block_group &&
6046 (!used_block_group ||
6047 used_block_group->ro ||
6048 !block_group_bits(used_block_group, data))) {
6049 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006050 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006051 }
Chris Mason44fb5512009-06-04 15:34:51 -04006052
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006053 if (used_block_group != block_group)
6054 btrfs_get_block_group(used_block_group);
6055
6056 offset = btrfs_alloc_from_cluster(used_block_group,
6057 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006058 if (offset) {
6059 /* we have a block, we're done */
6060 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006061 trace_btrfs_reserve_extent_cluster(root,
6062 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006063 goto checks;
6064 }
6065
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006066 WARN_ON(last_ptr->block_group != used_block_group);
6067 if (used_block_group != block_group) {
6068 btrfs_put_block_group(used_block_group);
6069 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006070 }
Chris Mason44fb5512009-06-04 15:34:51 -04006071refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006072 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006073 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6074 * set up a new clusters, so lets just skip it
6075 * and let the allocator find whatever block
6076 * it can find. If we reach this point, we
6077 * will have tried the cluster allocator
6078 * plenty of times and not have found
6079 * anything, so we are likely way too
6080 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006081 * anything.
6082 *
6083 * However, if the cluster is taken from the
6084 * current block group, release the cluster
6085 * first, so that we stand a better chance of
6086 * succeeding in the unclustered
6087 * allocation. */
6088 if (loop >= LOOP_NO_EMPTY_SIZE &&
6089 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006090 spin_unlock(&last_ptr->refill_lock);
6091 goto unclustered_alloc;
6092 }
6093
Chris Masonfa9c0d792009-04-03 09:47:43 -04006094 /*
6095 * this cluster didn't work out, free it and
6096 * start over
6097 */
6098 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6099
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006100 if (loop >= LOOP_NO_EMPTY_SIZE) {
6101 spin_unlock(&last_ptr->refill_lock);
6102 goto unclustered_alloc;
6103 }
6104
Chris Mason8de972b2013-01-04 15:39:43 -05006105 aligned_cluster = max_t(unsigned long,
6106 empty_cluster + empty_size,
6107 block_group->full_stripe_len);
6108
Chris Masonfa9c0d792009-04-03 09:47:43 -04006109 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04006110 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04006111 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05006112 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05006113 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006114 if (ret == 0) {
6115 /*
6116 * now pull our allocation out of this
6117 * cluster
6118 */
6119 offset = btrfs_alloc_from_cluster(block_group,
6120 last_ptr, num_bytes,
6121 search_start);
6122 if (offset) {
6123 /* we found one, proceed */
6124 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006125 trace_btrfs_reserve_extent_cluster(root,
6126 block_group, search_start,
6127 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006128 goto checks;
6129 }
Josef Bacik0a243252009-09-11 16:11:20 -04006130 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6131 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006132 spin_unlock(&last_ptr->refill_lock);
6133
Josef Bacik0a243252009-09-11 16:11:20 -04006134 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006135 wait_block_group_cache_progress(block_group,
6136 num_bytes + empty_cluster + empty_size);
6137 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006138 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006139
Chris Masonfa9c0d792009-04-03 09:47:43 -04006140 /*
6141 * at this point we either didn't find a cluster
6142 * or we weren't able to allocate a block from our
6143 * cluster. Free the cluster we've been trying
6144 * to use, and go to the next block group
6145 */
Josef Bacik0a243252009-09-11 16:11:20 -04006146 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006147 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006148 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006149 }
6150
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006151unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006152 spin_lock(&block_group->free_space_ctl->tree_lock);
6153 if (cached &&
6154 block_group->free_space_ctl->free_space <
6155 num_bytes + empty_cluster + empty_size) {
6156 spin_unlock(&block_group->free_space_ctl->tree_lock);
6157 goto loop;
6158 }
6159 spin_unlock(&block_group->free_space_ctl->tree_lock);
6160
Josef Bacik6226cb02009-04-03 10:14:18 -04006161 offset = btrfs_find_space_for_alloc(block_group, search_start,
6162 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006163 /*
6164 * If we didn't find a chunk, and we haven't failed on this
6165 * block group before, and this block group is in the middle of
6166 * caching and we are ok with waiting, then go ahead and wait
6167 * for progress to be made, and set failed_alloc to true.
6168 *
6169 * If failed_alloc is true then we've already waited on this
6170 * block group once and should move on to the next block group.
6171 */
6172 if (!offset && !failed_alloc && !cached &&
6173 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006174 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006175 num_bytes + empty_size);
6176 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006177 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006178 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006179 if (!cached)
6180 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006181 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006182 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006183checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006184 search_start = stripe_align(root, used_block_group,
6185 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006186
Josef Bacik2552d172009-04-03 10:14:19 -04006187 /* move on to the next group */
6188 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006189 used_block_group->key.objectid + used_block_group->key.offset) {
6190 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006191 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04006192 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006193
Josef Bacik6226cb02009-04-03 10:14:18 -04006194 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006195 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04006196 search_start - offset);
6197 BUG_ON(offset > search_start);
6198
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006199 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006200 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006201 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006202 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006203 goto loop;
6204 }
Yan Zheng11833d62009-09-11 16:11:19 -04006205
Josef Bacik2552d172009-04-03 10:14:19 -04006206 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006207 ins->objectid = search_start;
6208 ins->offset = num_bytes;
6209
Josef Bacik3f7de032011-11-10 08:29:20 -05006210 trace_btrfs_reserve_extent(orig_root, block_group,
6211 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006212 if (used_block_group != block_group)
6213 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006214 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006215 break;
6216loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006217 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006218 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006219 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006220 if (used_block_group != block_group)
6221 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006222 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006223 }
6224 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006225
Miao Xie60d2adb2011-09-09 17:34:35 +08006226 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6227 goto search;
6228
Yan, Zhengb742bb82010-05-16 10:46:24 -04006229 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6230 goto search;
6231
Josef Bacik285ff5a2012-01-13 15:27:45 -05006232 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006233 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6234 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006235 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6236 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6237 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6238 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006239 */
Josef Bacik723bda22011-05-27 16:11:38 -04006240 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006241 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006242 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006243 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04006244 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04006245 CHUNK_ALLOC_FORCE);
6246 /*
6247 * Do not bail out on ENOSPC since we
6248 * can do more things.
6249 */
6250 if (ret < 0 && ret != -ENOSPC) {
6251 btrfs_abort_transaction(trans,
6252 root, ret);
6253 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006254 }
Josef Bacik723bda22011-05-27 16:11:38 -04006255 }
6256
6257 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006258 empty_size = 0;
6259 empty_cluster = 0;
6260 }
Chris Mason42e70e72008-11-07 18:17:11 -05006261
Josef Bacik723bda22011-05-27 16:11:38 -04006262 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006263 } else if (!ins->objectid) {
6264 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006265 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006266 ret = 0;
6267 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006268out:
Chris Mason0b86a832008-03-24 15:01:56 -04006269
Chris Mason0f70abe2007-02-28 16:46:22 -05006270 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006271}
Chris Masonec44a352008-04-28 15:29:52 -04006272
Josef Bacik9ed74f22009-09-11 16:12:44 -04006273static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6274 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006275{
6276 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006277 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006278
Josef Bacik9ed74f22009-09-11 16:12:44 -04006279 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006280 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6281 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006282 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006283 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006284 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006285 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006286 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6287 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006288 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006289 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006290 (unsigned long long)info->bytes_pinned,
6291 (unsigned long long)info->bytes_reserved,
6292 (unsigned long long)info->bytes_may_use,
6293 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006294 spin_unlock(&info->lock);
6295
6296 if (!dump_block_groups)
6297 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006298
Josef Bacik80eb2342008-10-29 14:49:05 -04006299 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006300again:
6301 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006302 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006303 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006304 (unsigned long long)cache->key.objectid,
6305 (unsigned long long)cache->key.offset,
6306 (unsigned long long)btrfs_block_group_used(&cache->item),
6307 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006308 (unsigned long long)cache->reserved,
6309 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006310 btrfs_dump_free_space(cache, bytes);
6311 spin_unlock(&cache->lock);
6312 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006313 if (++index < BTRFS_NR_RAID_TYPES)
6314 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006315 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006316}
Zheng Yane8569812008-09-26 10:05:48 -04006317
Yan Zheng11833d62009-09-11 16:11:19 -04006318int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6319 struct btrfs_root *root,
6320 u64 num_bytes, u64 min_alloc_size,
6321 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006322 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006323{
Miao Xie9e622d62012-01-26 15:01:12 -05006324 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006325 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006326
Josef Bacik6a632092009-02-20 11:00:09 -05006327 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006328again:
Chris Masondb945352007-10-15 16:15:53 -04006329 WARN_ON(num_bytes < root->sectorsize);
6330 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006331 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006332
Miao Xie9e622d62012-01-26 15:01:12 -05006333 if (ret == -ENOSPC) {
6334 if (!final_tried) {
6335 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006336 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006337 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006338 if (num_bytes == min_alloc_size)
6339 final_tried = true;
6340 goto again;
6341 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6342 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006343
Miao Xie9e622d62012-01-26 15:01:12 -05006344 sinfo = __find_space_info(root->fs_info, data);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006345 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
6346 (unsigned long long)data,
6347 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006348 if (sinfo)
6349 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006350 }
Chris Mason925baed2008-06-25 16:01:30 -04006351 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006352
liubo1abe9b82011-03-24 11:18:59 +00006353 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6354
Josef Bacik0f9dd462008-09-23 13:14:11 -04006355 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006356}
6357
Chris Masone688b7252011-10-31 20:52:39 -04006358static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6359 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006360{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006361 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006362 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006363
Josef Bacik0f9dd462008-09-23 13:14:11 -04006364 cache = btrfs_lookup_block_group(root->fs_info, start);
6365 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006366 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6367 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006368 return -ENOSPC;
6369 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006370
Li Dongyang5378e602011-03-24 10:24:27 +00006371 if (btrfs_test_opt(root, DISCARD))
6372 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006373
Chris Masone688b7252011-10-31 20:52:39 -04006374 if (pin)
6375 pin_down_extent(root, cache, start, len, 1);
6376 else {
6377 btrfs_add_free_space(cache, start, len);
6378 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6379 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006380 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006381
liubo1abe9b82011-03-24 11:18:59 +00006382 trace_btrfs_reserved_extent_free(root, start, len);
6383
Chris Masone6dcd2d2008-07-17 12:53:50 -04006384 return ret;
6385}
6386
Chris Masone688b7252011-10-31 20:52:39 -04006387int btrfs_free_reserved_extent(struct btrfs_root *root,
6388 u64 start, u64 len)
6389{
6390 return __btrfs_free_reserved_extent(root, start, len, 0);
6391}
6392
6393int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6394 u64 start, u64 len)
6395{
6396 return __btrfs_free_reserved_extent(root, start, len, 1);
6397}
6398
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006399static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6400 struct btrfs_root *root,
6401 u64 parent, u64 root_objectid,
6402 u64 flags, u64 owner, u64 offset,
6403 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006404{
6405 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006406 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006407 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006408 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006409 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006410 struct extent_buffer *leaf;
6411 int type;
6412 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006413
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006414 if (parent > 0)
6415 type = BTRFS_SHARED_DATA_REF_KEY;
6416 else
6417 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006418
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006419 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006420
6421 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006422 if (!path)
6423 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006424
Chris Masonb9473432009-03-13 11:00:37 -04006425 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006426 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6427 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006428 if (ret) {
6429 btrfs_free_path(path);
6430 return ret;
6431 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006433 leaf = path->nodes[0];
6434 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006435 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006436 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6437 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6438 btrfs_set_extent_flags(leaf, extent_item,
6439 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006441 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6442 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6443 if (parent > 0) {
6444 struct btrfs_shared_data_ref *ref;
6445 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6446 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6447 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6448 } else {
6449 struct btrfs_extent_data_ref *ref;
6450 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6451 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6452 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6453 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6454 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6455 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006456
6457 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006458 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006459
Liu Boc53d6132012-12-27 09:01:19 +00006460 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006461 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006462 btrfs_err(fs_info, "update block group failed for %llu %llu",
6463 (unsigned long long)ins->objectid,
6464 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006465 BUG();
6466 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006467 return ret;
6468}
6469
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006470static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6471 struct btrfs_root *root,
6472 u64 parent, u64 root_objectid,
6473 u64 flags, struct btrfs_disk_key *key,
6474 int level, struct btrfs_key *ins)
6475{
6476 int ret;
6477 struct btrfs_fs_info *fs_info = root->fs_info;
6478 struct btrfs_extent_item *extent_item;
6479 struct btrfs_tree_block_info *block_info;
6480 struct btrfs_extent_inline_ref *iref;
6481 struct btrfs_path *path;
6482 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006483 u32 size = sizeof(*extent_item) + sizeof(*iref);
6484 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6485 SKINNY_METADATA);
6486
6487 if (!skinny_metadata)
6488 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006489
6490 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006491 if (!path)
6492 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006493
6494 path->leave_spinning = 1;
6495 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6496 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006497 if (ret) {
6498 btrfs_free_path(path);
6499 return ret;
6500 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006501
6502 leaf = path->nodes[0];
6503 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6504 struct btrfs_extent_item);
6505 btrfs_set_extent_refs(leaf, extent_item, 1);
6506 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6507 btrfs_set_extent_flags(leaf, extent_item,
6508 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006509
Josef Bacik3173a182013-03-07 14:22:04 -05006510 if (skinny_metadata) {
6511 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6512 } else {
6513 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6514 btrfs_set_tree_block_key(leaf, block_info, key);
6515 btrfs_set_tree_block_level(leaf, block_info, level);
6516 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6517 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006518
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006519 if (parent > 0) {
6520 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6521 btrfs_set_extent_inline_ref_type(leaf, iref,
6522 BTRFS_SHARED_BLOCK_REF_KEY);
6523 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6524 } else {
6525 btrfs_set_extent_inline_ref_type(leaf, iref,
6526 BTRFS_TREE_BLOCK_REF_KEY);
6527 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6528 }
6529
6530 btrfs_mark_buffer_dirty(leaf);
6531 btrfs_free_path(path);
6532
Josef Bacik3173a182013-03-07 14:22:04 -05006533 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006534 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006535 btrfs_err(fs_info, "update block group failed for %llu %llu",
6536 (unsigned long long)ins->objectid,
6537 (unsigned long long)ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006538 BUG();
6539 }
6540 return ret;
6541}
6542
6543int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6544 struct btrfs_root *root,
6545 u64 root_objectid, u64 owner,
6546 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006547{
6548 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006549
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006550 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006551
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006552 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6553 ins->offset, 0,
6554 root_objectid, owner, offset,
6555 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006556 return ret;
6557}
Chris Masone02119d2008-09-05 16:13:11 -04006558
6559/*
6560 * this is used by the tree logging recovery code. It records that
6561 * an extent has been allocated and makes sure to clear the free
6562 * space cache bits as well
6563 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006564int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6565 struct btrfs_root *root,
6566 u64 root_objectid, u64 owner, u64 offset,
6567 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006568{
6569 int ret;
6570 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006571 struct btrfs_caching_control *caching_ctl;
6572 u64 start = ins->objectid;
6573 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006574
Chris Masone02119d2008-09-05 16:13:11 -04006575 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006576 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006577 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006578
Yan Zheng11833d62009-09-11 16:11:19 -04006579 if (!caching_ctl) {
6580 BUG_ON(!block_group_cache_done(block_group));
6581 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006582 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006583 } else {
6584 mutex_lock(&caching_ctl->mutex);
6585
6586 if (start >= caching_ctl->progress) {
6587 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006588 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006589 } else if (start + num_bytes <= caching_ctl->progress) {
6590 ret = btrfs_remove_free_space(block_group,
6591 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006592 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006593 } else {
6594 num_bytes = caching_ctl->progress - start;
6595 ret = btrfs_remove_free_space(block_group,
6596 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006597 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006598
6599 start = caching_ctl->progress;
6600 num_bytes = ins->objectid + ins->offset -
6601 caching_ctl->progress;
6602 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006603 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006604 }
6605
6606 mutex_unlock(&caching_ctl->mutex);
6607 put_caching_control(caching_ctl);
6608 }
6609
Josef Bacikfb25e912011-07-26 17:00:46 -04006610 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6611 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006612 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d792009-04-03 09:47:43 -04006613 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006614 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6615 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006616 return ret;
6617}
6618
Chris Mason65b51a02008-08-01 15:11:20 -04006619struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6620 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006621 u64 bytenr, u32 blocksize,
6622 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006623{
6624 struct extent_buffer *buf;
6625
6626 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6627 if (!buf)
6628 return ERR_PTR(-ENOMEM);
6629 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006630 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006631 btrfs_tree_lock(buf);
6632 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006633 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006634
6635 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006636 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006637
Chris Masond0c803c2008-09-11 16:17:57 -04006638 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006639 /*
6640 * we allow two log transactions at a time, use different
6641 * EXENT bit to differentiate dirty pages.
6642 */
6643 if (root->log_transid % 2 == 0)
6644 set_extent_dirty(&root->dirty_log_pages, buf->start,
6645 buf->start + buf->len - 1, GFP_NOFS);
6646 else
6647 set_extent_new(&root->dirty_log_pages, buf->start,
6648 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006649 } else {
6650 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6651 buf->start + buf->len - 1, GFP_NOFS);
6652 }
Chris Mason65b51a02008-08-01 15:11:20 -04006653 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006654 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006655 return buf;
6656}
6657
Yan, Zhengf0486c62010-05-16 10:46:25 -04006658static struct btrfs_block_rsv *
6659use_block_rsv(struct btrfs_trans_handle *trans,
6660 struct btrfs_root *root, u32 blocksize)
6661{
6662 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006663 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006664 int ret;
6665
6666 block_rsv = get_block_rsv(trans, root);
6667
6668 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006669 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6670 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006671 /*
6672 * If we couldn't reserve metadata bytes try and use some from
6673 * the global reserve.
6674 */
6675 if (ret && block_rsv != global_rsv) {
6676 ret = block_rsv_use_bytes(global_rsv, blocksize);
6677 if (!ret)
6678 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006679 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006680 } else if (ret) {
6681 return ERR_PTR(ret);
6682 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006683 return block_rsv;
6684 }
6685
6686 ret = block_rsv_use_bytes(block_rsv, blocksize);
6687 if (!ret)
6688 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006689 if (ret && !block_rsv->failfast) {
David Sterbab069e0c2013-02-08 21:28:17 +00006690 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6691 static DEFINE_RATELIMIT_STATE(_rs,
6692 DEFAULT_RATELIMIT_INTERVAL * 10,
6693 /*DEFAULT_RATELIMIT_BURST*/ 1);
6694 if (__ratelimit(&_rs))
6695 WARN(1, KERN_DEBUG
6696 "btrfs: block rsv returned %d\n", ret);
6697 }
Miao Xie08e007d2012-10-16 11:33:38 +00006698 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6699 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006700 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006701 return block_rsv;
6702 } else if (ret && block_rsv != global_rsv) {
6703 ret = block_rsv_use_bytes(global_rsv, blocksize);
6704 if (!ret)
6705 return global_rsv;
6706 }
6707 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006708
Yan, Zhengf0486c62010-05-16 10:46:25 -04006709 return ERR_PTR(-ENOSPC);
6710}
6711
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006712static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6713 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006714{
6715 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006716 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006717}
6718
Chris Masonfec577f2007-02-26 10:40:21 -05006719/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006720 * finds a free extent and does all the dirty work required for allocation
6721 * returns the key for the extent through ins, and a tree buffer for
6722 * the first block of the extent through buf.
6723 *
Chris Masonfec577f2007-02-26 10:40:21 -05006724 * returns the tree buffer or NULL.
6725 */
Chris Mason5f39d392007-10-15 16:14:19 -04006726struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006727 struct btrfs_root *root, u32 blocksize,
6728 u64 parent, u64 root_objectid,
6729 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006730 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006731{
Chris Masone2fa7222007-03-12 16:22:34 -04006732 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006733 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006734 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006735 u64 flags = 0;
6736 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05006737 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6738 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006739
6740 block_rsv = use_block_rsv(trans, root, blocksize);
6741 if (IS_ERR(block_rsv))
6742 return ERR_CAST(block_rsv);
6743
6744 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006745 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006746 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006747 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006748 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006749 }
Chris Mason55c69072008-01-09 15:55:33 -05006750
Chris Mason4008c042009-02-12 14:09:45 -05006751 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6752 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006753 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006754
6755 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6756 if (parent == 0)
6757 parent = ins.objectid;
6758 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6759 } else
6760 BUG_ON(parent > 0);
6761
6762 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6763 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006764 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006765 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006766 if (key)
6767 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6768 else
6769 memset(&extent_op->key, 0, sizeof(extent_op->key));
6770 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05006771 if (skinny_metadata)
6772 extent_op->update_key = 0;
6773 else
6774 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006775 extent_op->update_flags = 1;
6776 extent_op->is_data = 0;
6777
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006778 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6779 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006780 ins.offset, parent, root_objectid,
6781 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006782 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006783 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006784 }
Chris Masonfec577f2007-02-26 10:40:21 -05006785 return buf;
6786}
Chris Masona28ec192007-03-06 20:08:01 -05006787
Yan Zheng2c47e6052009-06-27 21:07:35 -04006788struct walk_control {
6789 u64 refs[BTRFS_MAX_LEVEL];
6790 u64 flags[BTRFS_MAX_LEVEL];
6791 struct btrfs_key update_progress;
6792 int stage;
6793 int level;
6794 int shared_level;
6795 int update_ref;
6796 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006797 int reada_slot;
6798 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006799 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006800};
6801
6802#define DROP_REFERENCE 1
6803#define UPDATE_BACKREF 2
6804
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006805static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6806 struct btrfs_root *root,
6807 struct walk_control *wc,
6808 struct btrfs_path *path)
6809{
6810 u64 bytenr;
6811 u64 generation;
6812 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006813 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006814 u32 nritems;
6815 u32 blocksize;
6816 struct btrfs_key key;
6817 struct extent_buffer *eb;
6818 int ret;
6819 int slot;
6820 int nread = 0;
6821
6822 if (path->slots[wc->level] < wc->reada_slot) {
6823 wc->reada_count = wc->reada_count * 2 / 3;
6824 wc->reada_count = max(wc->reada_count, 2);
6825 } else {
6826 wc->reada_count = wc->reada_count * 3 / 2;
6827 wc->reada_count = min_t(int, wc->reada_count,
6828 BTRFS_NODEPTRS_PER_BLOCK(root));
6829 }
6830
6831 eb = path->nodes[wc->level];
6832 nritems = btrfs_header_nritems(eb);
6833 blocksize = btrfs_level_size(root, wc->level - 1);
6834
6835 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6836 if (nread >= wc->reada_count)
6837 break;
6838
6839 cond_resched();
6840 bytenr = btrfs_node_blockptr(eb, slot);
6841 generation = btrfs_node_ptr_generation(eb, slot);
6842
6843 if (slot == path->slots[wc->level])
6844 goto reada;
6845
6846 if (wc->stage == UPDATE_BACKREF &&
6847 generation <= root->root_key.offset)
6848 continue;
6849
Yan, Zheng94fcca92009-10-09 09:25:16 -04006850 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05006851 ret = btrfs_lookup_extent_info(trans, root, bytenr,
6852 wc->level - 1, 1, &refs,
6853 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006854 /* We don't care about errors in readahead. */
6855 if (ret < 0)
6856 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006857 BUG_ON(refs == 0);
6858
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006859 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006860 if (refs == 1)
6861 goto reada;
6862
Yan, Zheng94fcca92009-10-09 09:25:16 -04006863 if (wc->level == 1 &&
6864 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6865 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006866 if (!wc->update_ref ||
6867 generation <= root->root_key.offset)
6868 continue;
6869 btrfs_node_key_to_cpu(eb, &key, slot);
6870 ret = btrfs_comp_cpu_keys(&key,
6871 &wc->update_progress);
6872 if (ret < 0)
6873 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006874 } else {
6875 if (wc->level == 1 &&
6876 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6877 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006878 }
6879reada:
6880 ret = readahead_tree_block(root, bytenr, blocksize,
6881 generation);
6882 if (ret)
6883 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006884 nread++;
6885 }
6886 wc->reada_slot = slot;
6887}
6888
Chris Mason9aca1d52007-03-13 11:09:37 -04006889/*
Liu Bo2c016dc2012-12-26 15:32:17 +08006890 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04006891 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006892 * when wc->stage == UPDATE_BACKREF, this function updates
6893 * back refs for pointers in the block.
6894 *
6895 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006896 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006897static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6898 struct btrfs_root *root,
6899 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006900 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006901{
6902 int level = wc->level;
6903 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006904 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6905 int ret;
6906
6907 if (wc->stage == UPDATE_BACKREF &&
6908 btrfs_header_owner(eb) != root->root_key.objectid)
6909 return 1;
6910
6911 /*
6912 * when reference count of tree block is 1, it won't increase
6913 * again. once full backref flag is set, we never clear it.
6914 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006915 if (lookup_info &&
6916 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6917 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006918 BUG_ON(!path->locks[level]);
6919 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05006920 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006921 &wc->refs[level],
6922 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006923 BUG_ON(ret == -ENOMEM);
6924 if (ret)
6925 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006926 BUG_ON(wc->refs[level] == 0);
6927 }
6928
Yan Zheng2c47e6052009-06-27 21:07:35 -04006929 if (wc->stage == DROP_REFERENCE) {
6930 if (wc->refs[level] > 1)
6931 return 1;
6932
6933 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006934 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006935 path->locks[level] = 0;
6936 }
6937 return 0;
6938 }
6939
6940 /* wc->stage == UPDATE_BACKREF */
6941 if (!(wc->flags[level] & flag)) {
6942 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006943 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006944 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006945 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006946 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006947 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6948 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006949 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006950 wc->flags[level] |= flag;
6951 }
6952
6953 /*
6954 * the block is shared by multiple trees, so it's not good to
6955 * keep the tree lock
6956 */
6957 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006958 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006959 path->locks[level] = 0;
6960 }
6961 return 0;
6962}
6963
6964/*
Liu Bo2c016dc2012-12-26 15:32:17 +08006965 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006966 *
6967 * when wc->stage == DROP_REFERENCE, this function checks
6968 * reference count of the block pointed to. if the block
6969 * is shared and we need update back refs for the subtree
6970 * rooted at the block, this function changes wc->stage to
6971 * UPDATE_BACKREF. if the block is shared and there is no
6972 * need to update back, this function drops the reference
6973 * to the block.
6974 *
6975 * NOTE: return value 1 means we should stop walking down.
6976 */
6977static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6978 struct btrfs_root *root,
6979 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006980 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006981{
6982 u64 bytenr;
6983 u64 generation;
6984 u64 parent;
6985 u32 blocksize;
6986 struct btrfs_key key;
6987 struct extent_buffer *next;
6988 int level = wc->level;
6989 int reada = 0;
6990 int ret = 0;
6991
6992 generation = btrfs_node_ptr_generation(path->nodes[level],
6993 path->slots[level]);
6994 /*
6995 * if the lower level block was created before the snapshot
6996 * was created, we know there is no need to update back refs
6997 * for the subtree
6998 */
6999 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007000 generation <= root->root_key.offset) {
7001 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007002 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007003 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007004
7005 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7006 blocksize = btrfs_level_size(root, level - 1);
7007
7008 next = btrfs_find_tree_block(root, bytenr, blocksize);
7009 if (!next) {
7010 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00007011 if (!next)
7012 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007013 reada = 1;
7014 }
7015 btrfs_tree_lock(next);
7016 btrfs_set_lock_blocking(next);
7017
Josef Bacik3173a182013-03-07 14:22:04 -05007018 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007019 &wc->refs[level - 1],
7020 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007021 if (ret < 0) {
7022 btrfs_tree_unlock(next);
7023 return ret;
7024 }
7025
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007026 if (unlikely(wc->refs[level - 1] == 0)) {
7027 btrfs_err(root->fs_info, "Missing references.");
7028 BUG();
7029 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007030 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007031
Yan, Zheng94fcca92009-10-09 09:25:16 -04007032 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007033 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007034 if (level == 1 &&
7035 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7036 goto skip;
7037
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007038 if (!wc->update_ref ||
7039 generation <= root->root_key.offset)
7040 goto skip;
7041
7042 btrfs_node_key_to_cpu(path->nodes[level], &key,
7043 path->slots[level]);
7044 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7045 if (ret < 0)
7046 goto skip;
7047
7048 wc->stage = UPDATE_BACKREF;
7049 wc->shared_level = level - 1;
7050 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007051 } else {
7052 if (level == 1 &&
7053 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7054 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007055 }
7056
Chris Masonb9fab912012-05-06 07:23:47 -04007057 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007058 btrfs_tree_unlock(next);
7059 free_extent_buffer(next);
7060 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007061 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007062 }
7063
7064 if (!next) {
7065 if (reada && level == 1)
7066 reada_walk_down(trans, root, wc, path);
7067 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007068 if (!next)
7069 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007070 btrfs_tree_lock(next);
7071 btrfs_set_lock_blocking(next);
7072 }
7073
7074 level--;
7075 BUG_ON(level != btrfs_header_level(next));
7076 path->nodes[level] = next;
7077 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007078 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007079 wc->level = level;
7080 if (wc->level == 1)
7081 wc->reada_slot = 0;
7082 return 0;
7083skip:
7084 wc->refs[level - 1] = 0;
7085 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007086 if (wc->stage == DROP_REFERENCE) {
7087 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7088 parent = path->nodes[level]->start;
7089 } else {
7090 BUG_ON(root->root_key.objectid !=
7091 btrfs_header_owner(path->nodes[level]));
7092 parent = 0;
7093 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007094
Yan, Zheng94fcca92009-10-09 09:25:16 -04007095 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007096 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007097 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007098 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007099 btrfs_tree_unlock(next);
7100 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007101 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007102 return 1;
7103}
7104
7105/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007106 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007107 *
7108 * when wc->stage == DROP_REFERENCE, this function drops
7109 * reference count on the block.
7110 *
7111 * when wc->stage == UPDATE_BACKREF, this function changes
7112 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7113 * to UPDATE_BACKREF previously while processing the block.
7114 *
7115 * NOTE: return value 1 means we should stop walking up.
7116 */
7117static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7118 struct btrfs_root *root,
7119 struct btrfs_path *path,
7120 struct walk_control *wc)
7121{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007122 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007123 int level = wc->level;
7124 struct extent_buffer *eb = path->nodes[level];
7125 u64 parent = 0;
7126
7127 if (wc->stage == UPDATE_BACKREF) {
7128 BUG_ON(wc->shared_level < level);
7129 if (level < wc->shared_level)
7130 goto out;
7131
Yan Zheng2c47e6052009-06-27 21:07:35 -04007132 ret = find_next_key(path, level + 1, &wc->update_progress);
7133 if (ret > 0)
7134 wc->update_ref = 0;
7135
7136 wc->stage = DROP_REFERENCE;
7137 wc->shared_level = -1;
7138 path->slots[level] = 0;
7139
7140 /*
7141 * check reference count again if the block isn't locked.
7142 * we should start walking down the tree again if reference
7143 * count is one.
7144 */
7145 if (!path->locks[level]) {
7146 BUG_ON(level == 0);
7147 btrfs_tree_lock(eb);
7148 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007149 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007150
7151 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007152 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007153 &wc->refs[level],
7154 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007155 if (ret < 0) {
7156 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007157 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007158 return ret;
7159 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007160 BUG_ON(wc->refs[level] == 0);
7161 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007162 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007163 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007164 return 1;
7165 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007166 }
7167 }
7168
7169 /* wc->stage == DROP_REFERENCE */
7170 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7171
7172 if (wc->refs[level] == 1) {
7173 if (level == 0) {
7174 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007175 ret = btrfs_dec_ref(trans, root, eb, 1,
7176 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007177 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007178 ret = btrfs_dec_ref(trans, root, eb, 0,
7179 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007180 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007181 }
7182 /* make block locked assertion in clean_tree_block happy */
7183 if (!path->locks[level] &&
7184 btrfs_header_generation(eb) == trans->transid) {
7185 btrfs_tree_lock(eb);
7186 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007187 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007188 }
7189 clean_tree_block(trans, root, eb);
7190 }
7191
7192 if (eb == root->node) {
7193 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7194 parent = eb->start;
7195 else
7196 BUG_ON(root->root_key.objectid !=
7197 btrfs_header_owner(eb));
7198 } else {
7199 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7200 parent = path->nodes[level + 1]->start;
7201 else
7202 BUG_ON(root->root_key.objectid !=
7203 btrfs_header_owner(path->nodes[level + 1]));
7204 }
7205
Jan Schmidt5581a512012-05-16 17:04:52 +02007206 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007207out:
7208 wc->refs[level] = 0;
7209 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007210 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007211}
7212
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007213static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7214 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007215 struct btrfs_path *path,
7216 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007217{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007218 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007219 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007220 int ret;
7221
Yan Zheng2c47e6052009-06-27 21:07:35 -04007222 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007223 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007224 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007225 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007226
Yan Zheng2c47e6052009-06-27 21:07:35 -04007227 if (level == 0)
7228 break;
7229
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007230 if (path->slots[level] >=
7231 btrfs_header_nritems(path->nodes[level]))
7232 break;
7233
Yan, Zheng94fcca92009-10-09 09:25:16 -04007234 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007235 if (ret > 0) {
7236 path->slots[level]++;
7237 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00007238 } else if (ret < 0)
7239 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007240 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007241 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007242 return 0;
7243}
7244
Chris Masond3977122009-01-05 21:25:51 -05007245static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007246 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007247 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007248 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007249{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007250 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007251 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007252
Yan Zheng2c47e6052009-06-27 21:07:35 -04007253 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7254 while (level < max_level && path->nodes[level]) {
7255 wc->level = level;
7256 if (path->slots[level] + 1 <
7257 btrfs_header_nritems(path->nodes[level])) {
7258 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007259 return 0;
7260 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007261 ret = walk_up_proc(trans, root, path, wc);
7262 if (ret > 0)
7263 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007264
Yan Zheng2c47e6052009-06-27 21:07:35 -04007265 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007266 btrfs_tree_unlock_rw(path->nodes[level],
7267 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007268 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007269 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007270 free_extent_buffer(path->nodes[level]);
7271 path->nodes[level] = NULL;
7272 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007273 }
7274 }
7275 return 1;
7276}
7277
Chris Mason9aca1d52007-03-13 11:09:37 -04007278/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007279 * drop a subvolume tree.
7280 *
7281 * this function traverses the tree freeing any blocks that only
7282 * referenced by the tree.
7283 *
7284 * when a shared tree block is found. this function decreases its
7285 * reference count by one. if update_ref is true, this function
7286 * also make sure backrefs for the shared block and all lower level
7287 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007288 *
7289 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007290 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007291int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007292 struct btrfs_block_rsv *block_rsv, int update_ref,
7293 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007294{
Chris Mason5caf2a02007-04-02 11:20:42 -04007295 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007296 struct btrfs_trans_handle *trans;
7297 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007298 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007299 struct walk_control *wc;
7300 struct btrfs_key key;
7301 int err = 0;
7302 int ret;
7303 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007304
Chris Mason5caf2a02007-04-02 11:20:42 -04007305 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007306 if (!path) {
7307 err = -ENOMEM;
7308 goto out;
7309 }
Chris Mason20524f02007-03-10 06:35:47 -05007310
Yan Zheng2c47e6052009-06-27 21:07:35 -04007311 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007312 if (!wc) {
7313 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007314 err = -ENOMEM;
7315 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007316 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007317
Yan, Zhenga22285a2010-05-16 10:48:46 -04007318 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007319 if (IS_ERR(trans)) {
7320 err = PTR_ERR(trans);
7321 goto out_free;
7322 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007323
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007324 if (block_rsv)
7325 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007326
Chris Mason9f3a7422007-08-07 15:52:19 -04007327 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007328 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007329 path->nodes[level] = btrfs_lock_root_node(root);
7330 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007331 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007332 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007333 memset(&wc->update_progress, 0,
7334 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007335 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007336 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007337 memcpy(&wc->update_progress, &key,
7338 sizeof(wc->update_progress));
7339
Chris Mason6702ed42007-08-07 16:15:09 -04007340 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007341 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007342 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007343 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7344 path->lowest_level = 0;
7345 if (ret < 0) {
7346 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007347 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007348 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007349 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007350
Chris Mason7d9eb122008-07-08 14:19:17 -04007351 /*
7352 * unlock our path, this is safe because only this
7353 * function is allowed to delete this snapshot
7354 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007355 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007356
Yan Zheng2c47e6052009-06-27 21:07:35 -04007357 level = btrfs_header_level(root->node);
7358 while (1) {
7359 btrfs_tree_lock(path->nodes[level]);
7360 btrfs_set_lock_blocking(path->nodes[level]);
7361
7362 ret = btrfs_lookup_extent_info(trans, root,
7363 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007364 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007365 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007366 if (ret < 0) {
7367 err = ret;
7368 goto out_end_trans;
7369 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007370 BUG_ON(wc->refs[level] == 0);
7371
7372 if (level == root_item->drop_level)
7373 break;
7374
7375 btrfs_tree_unlock(path->nodes[level]);
7376 WARN_ON(wc->refs[level] != 1);
7377 level--;
7378 }
7379 }
7380
7381 wc->level = level;
7382 wc->shared_level = -1;
7383 wc->stage = DROP_REFERENCE;
7384 wc->update_ref = update_ref;
7385 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007386 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007387 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007388
7389 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007390 if (!for_reloc && btrfs_fs_closing(root->fs_info)) {
7391 pr_debug("btrfs: drop snapshot early exit\n");
7392 err = -EAGAIN;
7393 goto out_end_trans;
7394 }
7395
Yan Zheng2c47e6052009-06-27 21:07:35 -04007396 ret = walk_down_tree(trans, root, path, wc);
7397 if (ret < 0) {
7398 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007399 break;
7400 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007401
7402 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7403 if (ret < 0) {
7404 err = ret;
7405 break;
7406 }
7407
7408 if (ret > 0) {
7409 BUG_ON(wc->stage != DROP_REFERENCE);
7410 break;
7411 }
7412
7413 if (wc->stage == DROP_REFERENCE) {
7414 level = wc->level;
7415 btrfs_node_key(path->nodes[level],
7416 &root_item->drop_progress,
7417 path->slots[level]);
7418 root_item->drop_level = level;
7419 }
7420
7421 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007422 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007423 ret = btrfs_update_root(trans, tree_root,
7424 &root->root_key,
7425 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007426 if (ret) {
7427 btrfs_abort_transaction(trans, tree_root, ret);
7428 err = ret;
7429 goto out_end_trans;
7430 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007431
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007432 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007433 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007434 if (IS_ERR(trans)) {
7435 err = PTR_ERR(trans);
7436 goto out_free;
7437 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007438 if (block_rsv)
7439 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007440 }
Chris Mason20524f02007-03-10 06:35:47 -05007441 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007442 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007443 if (err)
7444 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007445
7446 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007447 if (ret) {
7448 btrfs_abort_transaction(trans, tree_root, ret);
7449 goto out_end_trans;
7450 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007451
Yan, Zheng76dda932009-09-21 16:00:26 -04007452 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7453 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7454 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007455 if (ret < 0) {
7456 btrfs_abort_transaction(trans, tree_root, ret);
7457 err = ret;
7458 goto out_end_trans;
7459 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007460 /* if we fail to delete the orphan item this time
7461 * around, it'll get picked up the next time.
7462 *
7463 * The most common failure here is just -ENOENT.
7464 */
7465 btrfs_del_orphan_item(trans, tree_root,
7466 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007467 }
7468 }
7469
7470 if (root->in_radix) {
7471 btrfs_free_fs_root(tree_root->fs_info, root);
7472 } else {
7473 free_extent_buffer(root->node);
7474 free_extent_buffer(root->commit_root);
7475 kfree(root);
7476 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007477out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007478 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007479out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007480 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007481 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007482out:
7483 if (err)
7484 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007485 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007486}
Chris Mason9078a3e2007-04-26 16:46:15 -04007487
Yan Zheng2c47e6052009-06-27 21:07:35 -04007488/*
7489 * drop subtree rooted at tree block 'node'.
7490 *
7491 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007492 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007493 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007494int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7495 struct btrfs_root *root,
7496 struct extent_buffer *node,
7497 struct extent_buffer *parent)
7498{
7499 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007500 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007501 int level;
7502 int parent_level;
7503 int ret = 0;
7504 int wret;
7505
Yan Zheng2c47e6052009-06-27 21:07:35 -04007506 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7507
Yan Zhengf82d02d2008-10-29 14:49:05 -04007508 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007509 if (!path)
7510 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007511
Yan Zheng2c47e6052009-06-27 21:07:35 -04007512 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007513 if (!wc) {
7514 btrfs_free_path(path);
7515 return -ENOMEM;
7516 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007517
Chris Masonb9447ef2009-03-09 11:45:38 -04007518 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007519 parent_level = btrfs_header_level(parent);
7520 extent_buffer_get(parent);
7521 path->nodes[parent_level] = parent;
7522 path->slots[parent_level] = btrfs_header_nritems(parent);
7523
Chris Masonb9447ef2009-03-09 11:45:38 -04007524 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007525 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007526 path->nodes[level] = node;
7527 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007528 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007529
7530 wc->refs[parent_level] = 1;
7531 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7532 wc->level = level;
7533 wc->shared_level = -1;
7534 wc->stage = DROP_REFERENCE;
7535 wc->update_ref = 0;
7536 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007537 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007538 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007539
7540 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007541 wret = walk_down_tree(trans, root, path, wc);
7542 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007543 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007544 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007545 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007546
Yan Zheng2c47e6052009-06-27 21:07:35 -04007547 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007548 if (wret < 0)
7549 ret = wret;
7550 if (wret != 0)
7551 break;
7552 }
7553
Yan Zheng2c47e6052009-06-27 21:07:35 -04007554 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007555 btrfs_free_path(path);
7556 return ret;
7557}
7558
Chris Masonec44a352008-04-28 15:29:52 -04007559static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7560{
7561 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007562 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007563
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007564 /*
7565 * if restripe for this chunk_type is on pick target profile and
7566 * return, otherwise do the usual balance
7567 */
7568 stripped = get_restripe_target(root->fs_info, flags);
7569 if (stripped)
7570 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007571
Chris Masoncd02dca2010-12-13 14:56:23 -05007572 /*
7573 * we add in the count of missing devices because we want
7574 * to make sure that any RAID levels on a degraded FS
7575 * continue to be honored.
7576 */
7577 num_devices = root->fs_info->fs_devices->rw_devices +
7578 root->fs_info->fs_devices->missing_devices;
7579
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007580 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007581 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007582 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7583
Chris Masonec44a352008-04-28 15:29:52 -04007584 if (num_devices == 1) {
7585 stripped |= BTRFS_BLOCK_GROUP_DUP;
7586 stripped = flags & ~stripped;
7587
7588 /* turn raid0 into single device chunks */
7589 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7590 return stripped;
7591
7592 /* turn mirroring into duplication */
7593 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7594 BTRFS_BLOCK_GROUP_RAID10))
7595 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007596 } else {
7597 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007598 if (flags & stripped)
7599 return flags;
7600
7601 stripped |= BTRFS_BLOCK_GROUP_DUP;
7602 stripped = flags & ~stripped;
7603
7604 /* switch duplicated blocks with raid1 */
7605 if (flags & BTRFS_BLOCK_GROUP_DUP)
7606 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7607
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007608 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007609 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007610
Chris Masonec44a352008-04-28 15:29:52 -04007611 return flags;
7612}
7613
Miao Xie199c36e2011-07-15 10:34:36 +00007614static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007615{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007616 struct btrfs_space_info *sinfo = cache->space_info;
7617 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007618 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007619 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007620
Chris Masonc286ac42008-07-22 23:06:41 -04007621
Miao Xie199c36e2011-07-15 10:34:36 +00007622 /*
7623 * We need some metadata space and system metadata space for
7624 * allocating chunks in some corner cases until we force to set
7625 * it to be readonly.
7626 */
7627 if ((sinfo->flags &
7628 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7629 !force)
7630 min_allocable_bytes = 1 * 1024 * 1024;
7631 else
7632 min_allocable_bytes = 0;
7633
Yan, Zhengf0486c62010-05-16 10:46:25 -04007634 spin_lock(&sinfo->lock);
7635 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007636
7637 if (cache->ro) {
7638 ret = 0;
7639 goto out;
7640 }
7641
Yan, Zhengf0486c62010-05-16 10:46:25 -04007642 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7643 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007644
Yan, Zhengf0486c62010-05-16 10:46:25 -04007645 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007646 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7647 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007648 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007649 cache->ro = 1;
7650 ret = 0;
7651 }
WuBo61cfea92011-07-26 03:30:11 +00007652out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007653 spin_unlock(&cache->lock);
7654 spin_unlock(&sinfo->lock);
7655 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007656}
7657
Yan, Zhengf0486c62010-05-16 10:46:25 -04007658int btrfs_set_block_group_ro(struct btrfs_root *root,
7659 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007660
7661{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007662 struct btrfs_trans_handle *trans;
7663 u64 alloc_flags;
7664 int ret;
7665
7666 BUG_ON(cache->ro);
7667
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007668 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007669 if (IS_ERR(trans))
7670 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007671
7672 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007673 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007674 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007675 CHUNK_ALLOC_FORCE);
7676 if (ret < 0)
7677 goto out;
7678 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007679
Miao Xie199c36e2011-07-15 10:34:36 +00007680 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007681 if (!ret)
7682 goto out;
7683 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007684 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007685 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007686 if (ret < 0)
7687 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007688 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007689out:
7690 btrfs_end_transaction(trans, root);
7691 return ret;
7692}
7693
Chris Masonc87f08c2011-02-16 13:57:04 -05007694int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7695 struct btrfs_root *root, u64 type)
7696{
7697 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007698 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007699 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007700}
7701
Miao Xie6d07bce2011-01-05 10:07:31 +00007702/*
7703 * helper to account the unused space of all the readonly block group in the
7704 * list. takes mirrors into account.
7705 */
7706static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7707{
7708 struct btrfs_block_group_cache *block_group;
7709 u64 free_bytes = 0;
7710 int factor;
7711
7712 list_for_each_entry(block_group, groups_list, list) {
7713 spin_lock(&block_group->lock);
7714
7715 if (!block_group->ro) {
7716 spin_unlock(&block_group->lock);
7717 continue;
7718 }
7719
7720 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7721 BTRFS_BLOCK_GROUP_RAID10 |
7722 BTRFS_BLOCK_GROUP_DUP))
7723 factor = 2;
7724 else
7725 factor = 1;
7726
7727 free_bytes += (block_group->key.offset -
7728 btrfs_block_group_used(&block_group->item)) *
7729 factor;
7730
7731 spin_unlock(&block_group->lock);
7732 }
7733
7734 return free_bytes;
7735}
7736
7737/*
7738 * helper to account the unused space of all the readonly block group in the
7739 * space_info. takes mirrors into account.
7740 */
7741u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7742{
7743 int i;
7744 u64 free_bytes = 0;
7745
7746 spin_lock(&sinfo->lock);
7747
7748 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7749 if (!list_empty(&sinfo->block_groups[i]))
7750 free_bytes += __btrfs_get_ro_block_group_free_space(
7751 &sinfo->block_groups[i]);
7752
7753 spin_unlock(&sinfo->lock);
7754
7755 return free_bytes;
7756}
7757
Jeff Mahoney143bede2012-03-01 14:56:26 +01007758void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007759 struct btrfs_block_group_cache *cache)
7760{
7761 struct btrfs_space_info *sinfo = cache->space_info;
7762 u64 num_bytes;
7763
7764 BUG_ON(!cache->ro);
7765
7766 spin_lock(&sinfo->lock);
7767 spin_lock(&cache->lock);
7768 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7769 cache->bytes_super - btrfs_block_group_used(&cache->item);
7770 sinfo->bytes_readonly -= num_bytes;
7771 cache->ro = 0;
7772 spin_unlock(&cache->lock);
7773 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007774}
7775
Josef Bacikba1bf482009-09-11 16:11:19 -04007776/*
7777 * checks to see if its even possible to relocate this block group.
7778 *
7779 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7780 * ok to go ahead and try.
7781 */
7782int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007783{
Zheng Yan1a40e232008-09-26 10:09:34 -04007784 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007785 struct btrfs_space_info *space_info;
7786 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7787 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007788 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007789 u64 dev_min = 1;
7790 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007791 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007792 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007793 int full = 0;
7794 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007795
Josef Bacikba1bf482009-09-11 16:11:19 -04007796 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007797
Josef Bacikba1bf482009-09-11 16:11:19 -04007798 /* odd, couldn't find the block group, leave it alone */
7799 if (!block_group)
7800 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007801
liubocdcb7252011-08-03 10:15:25 +00007802 min_free = btrfs_block_group_used(&block_group->item);
7803
Josef Bacikba1bf482009-09-11 16:11:19 -04007804 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007805 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007806 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007807
Josef Bacikba1bf482009-09-11 16:11:19 -04007808 space_info = block_group->space_info;
7809 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007810
Josef Bacikba1bf482009-09-11 16:11:19 -04007811 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007812
Josef Bacikba1bf482009-09-11 16:11:19 -04007813 /*
7814 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007815 * relocate it unless we're able to allocate a new chunk below.
7816 *
7817 * Otherwise, we need to make sure we have room in the space to handle
7818 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007819 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007820 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007821 (space_info->bytes_used + space_info->bytes_reserved +
7822 space_info->bytes_pinned + space_info->bytes_readonly +
7823 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007824 spin_unlock(&space_info->lock);
7825 goto out;
7826 }
7827 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007828
Josef Bacikba1bf482009-09-11 16:11:19 -04007829 /*
7830 * ok we don't have enough space, but maybe we have free space on our
7831 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007832 * alloc devices and guess if we have enough space. if this block
7833 * group is going to be restriped, run checks against the target
7834 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007835 */
7836 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007837
liubocdcb7252011-08-03 10:15:25 +00007838 /*
7839 * index:
7840 * 0: raid10
7841 * 1: raid1
7842 * 2: dup
7843 * 3: raid0
7844 * 4: single
7845 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007846 target = get_restripe_target(root->fs_info, block_group->flags);
7847 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007848 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007849 } else {
7850 /*
7851 * this is just a balance, so if we were marked as full
7852 * we know there is no space for a new chunk
7853 */
7854 if (full)
7855 goto out;
7856
7857 index = get_block_group_index(block_group);
7858 }
7859
Miao Xiee6ec7162013-01-17 05:38:51 +00007860 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00007861 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007862 /* Divide by 2 */
7863 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007864 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00007865 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00007866 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04007867 /* Multiply by 2 */
7868 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007869 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00007870 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007871 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007872 }
7873
Josef Bacikba1bf482009-09-11 16:11:19 -04007874 mutex_lock(&root->fs_info->chunk_mutex);
7875 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007876 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007877
Josef Bacikba1bf482009-09-11 16:11:19 -04007878 /*
7879 * check to make sure we can actually find a chunk with enough
7880 * space to fit our block group in.
7881 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007882 if (device->total_bytes > device->bytes_used + min_free &&
7883 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007884 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007885 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007886 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007887 dev_nr++;
7888
7889 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007890 break;
liubocdcb7252011-08-03 10:15:25 +00007891
Josef Bacikba1bf482009-09-11 16:11:19 -04007892 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007893 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007894 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007895 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007896out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007897 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007898 return ret;
7899}
7900
Christoph Hellwigb2950862008-12-02 09:54:17 -05007901static int find_first_block_group(struct btrfs_root *root,
7902 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007903{
Chris Mason925baed2008-06-25 16:01:30 -04007904 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007905 struct btrfs_key found_key;
7906 struct extent_buffer *leaf;
7907 int slot;
7908
7909 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7910 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007911 goto out;
7912
Chris Masond3977122009-01-05 21:25:51 -05007913 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007914 slot = path->slots[0];
7915 leaf = path->nodes[0];
7916 if (slot >= btrfs_header_nritems(leaf)) {
7917 ret = btrfs_next_leaf(root, path);
7918 if (ret == 0)
7919 continue;
7920 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007921 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007922 break;
7923 }
7924 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7925
7926 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007927 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7928 ret = 0;
7929 goto out;
7930 }
Chris Mason0b86a832008-03-24 15:01:56 -04007931 path->slots[0]++;
7932 }
Chris Mason925baed2008-06-25 16:01:30 -04007933out:
Chris Mason0b86a832008-03-24 15:01:56 -04007934 return ret;
7935}
7936
Josef Bacik0af3d002010-06-21 14:48:16 -04007937void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7938{
7939 struct btrfs_block_group_cache *block_group;
7940 u64 last = 0;
7941
7942 while (1) {
7943 struct inode *inode;
7944
7945 block_group = btrfs_lookup_first_block_group(info, last);
7946 while (block_group) {
7947 spin_lock(&block_group->lock);
7948 if (block_group->iref)
7949 break;
7950 spin_unlock(&block_group->lock);
7951 block_group = next_block_group(info->tree_root,
7952 block_group);
7953 }
7954 if (!block_group) {
7955 if (last == 0)
7956 break;
7957 last = 0;
7958 continue;
7959 }
7960
7961 inode = block_group->inode;
7962 block_group->iref = 0;
7963 block_group->inode = NULL;
7964 spin_unlock(&block_group->lock);
7965 iput(inode);
7966 last = block_group->key.objectid + block_group->key.offset;
7967 btrfs_put_block_group(block_group);
7968 }
7969}
7970
Zheng Yan1a40e232008-09-26 10:09:34 -04007971int btrfs_free_block_groups(struct btrfs_fs_info *info)
7972{
7973 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007974 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007975 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007976 struct rb_node *n;
7977
Yan Zheng11833d62009-09-11 16:11:19 -04007978 down_write(&info->extent_commit_sem);
7979 while (!list_empty(&info->caching_block_groups)) {
7980 caching_ctl = list_entry(info->caching_block_groups.next,
7981 struct btrfs_caching_control, list);
7982 list_del(&caching_ctl->list);
7983 put_caching_control(caching_ctl);
7984 }
7985 up_write(&info->extent_commit_sem);
7986
Zheng Yan1a40e232008-09-26 10:09:34 -04007987 spin_lock(&info->block_group_cache_lock);
7988 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7989 block_group = rb_entry(n, struct btrfs_block_group_cache,
7990 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007991 rb_erase(&block_group->cache_node,
7992 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007993 spin_unlock(&info->block_group_cache_lock);
7994
Josef Bacik80eb2342008-10-29 14:49:05 -04007995 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007996 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007997 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007998
Josef Bacik817d52f2009-07-13 21:29:25 -04007999 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008000 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008001
Josef Bacik3c148742011-02-02 15:53:47 +00008002 /*
8003 * We haven't cached this block group, which means we could
8004 * possibly have excluded extents on this block group.
8005 */
8006 if (block_group->cached == BTRFS_CACHE_NO)
8007 free_excluded_extents(info->extent_root, block_group);
8008
Josef Bacik817d52f2009-07-13 21:29:25 -04008009 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008010 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008011
8012 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008013 }
8014 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008015
8016 /* now that all the block groups are freed, go through and
8017 * free all the space_info structs. This is only called during
8018 * the final stages of unmount, and so we know nobody is
8019 * using them. We call synchronize_rcu() once before we start,
8020 * just to be on the safe side.
8021 */
8022 synchronize_rcu();
8023
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008024 release_global_block_rsv(info);
8025
Chris Mason4184ea72009-03-10 12:39:20 -04008026 while(!list_empty(&info->space_info)) {
8027 space_info = list_entry(info->space_info.next,
8028 struct btrfs_space_info,
8029 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008030 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8031 if (space_info->bytes_pinned > 0 ||
8032 space_info->bytes_reserved > 0 ||
8033 space_info->bytes_may_use > 0) {
8034 WARN_ON(1);
8035 dump_space_info(space_info, 0, 0);
8036 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008037 }
Chris Mason4184ea72009-03-10 12:39:20 -04008038 list_del(&space_info->list);
8039 kfree(space_info);
8040 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008041 return 0;
8042}
8043
Yan, Zhengb742bb82010-05-16 10:46:24 -04008044static void __link_block_group(struct btrfs_space_info *space_info,
8045 struct btrfs_block_group_cache *cache)
8046{
8047 int index = get_block_group_index(cache);
8048
8049 down_write(&space_info->groups_sem);
8050 list_add_tail(&cache->list, &space_info->block_groups[index]);
8051 up_write(&space_info->groups_sem);
8052}
8053
Chris Mason9078a3e2007-04-26 16:46:15 -04008054int btrfs_read_block_groups(struct btrfs_root *root)
8055{
8056 struct btrfs_path *path;
8057 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008058 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008059 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008060 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008061 struct btrfs_key key;
8062 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008063 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008064 int need_clear = 0;
8065 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008066
Chris Masonbe744172007-05-06 10:15:01 -04008067 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008068 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008069 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008070 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008071 path = btrfs_alloc_path();
8072 if (!path)
8073 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008074 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008075
David Sterba6c417612011-04-13 15:41:04 +02008076 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008077 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008078 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008079 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008080 if (btrfs_test_opt(root, CLEAR_CACHE))
8081 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008082
Chris Masond3977122009-01-05 21:25:51 -05008083 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008084 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008085 if (ret > 0)
8086 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008087 if (ret != 0)
8088 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008089 leaf = path->nodes[0];
8090 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008091 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008092 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008093 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008094 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008095 }
Li Zefan34d52cb2011-03-29 13:46:06 +08008096 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8097 GFP_NOFS);
8098 if (!cache->free_space_ctl) {
8099 kfree(cache);
8100 ret = -ENOMEM;
8101 goto error;
8102 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008103
Yan Zhengd2fb3432008-12-11 16:30:39 -05008104 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008105 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008106 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008107 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008108 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008109
Liu Bocf7c1ef2012-07-06 03:31:34 -06008110 if (need_clear) {
8111 /*
8112 * When we mount with old space cache, we need to
8113 * set BTRFS_DC_CLEAR and set dirty flag.
8114 *
8115 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8116 * truncate the old free space cache inode and
8117 * setup a new one.
8118 * b) Setting 'dirty flag' makes sure that we flush
8119 * the new space cache info onto disk.
8120 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008121 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008122 if (btrfs_test_opt(root, SPACE_CACHE))
8123 cache->dirty = 1;
8124 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008125
Chris Mason5f39d392007-10-15 16:14:19 -04008126 read_extent_buffer(leaf, &cache->item,
8127 btrfs_item_ptr_offset(leaf, path->slots[0]),
8128 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008129 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008130
Chris Mason9078a3e2007-04-26 16:46:15 -04008131 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008132 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008133 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008134 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05008135 cache->full_stripe_len = btrfs_full_stripe_len(root,
8136 &root->fs_info->mapping_tree,
8137 found_key.objectid);
Li Zefan34d52cb2011-03-29 13:46:06 +08008138 btrfs_init_free_space_ctl(cache);
8139
Josef Bacik817d52f2009-07-13 21:29:25 -04008140 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008141 * We need to exclude the super stripes now so that the space
8142 * info has super bytes accounted for, otherwise we'll think
8143 * we have more space than we actually do.
8144 */
Josef Bacik835d9742013-03-19 12:13:25 -04008145 ret = exclude_super_stripes(root, cache);
8146 if (ret) {
8147 /*
8148 * We may have excluded something, so call this just in
8149 * case.
8150 */
8151 free_excluded_extents(root, cache);
8152 kfree(cache->free_space_ctl);
8153 kfree(cache);
8154 goto error;
8155 }
Josef Bacik3c148742011-02-02 15:53:47 +00008156
8157 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008158 * check for two cases, either we are full, and therefore
8159 * don't need to bother with the caching work since we won't
8160 * find any space, or we are empty, and we can just add all
8161 * the space in and be done with it. This saves us _alot_ of
8162 * time, particularly in the full case.
8163 */
8164 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008165 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008166 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008167 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008168 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008169 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008170 cache->cached = BTRFS_CACHE_FINISHED;
8171 add_new_free_space(cache, root->fs_info,
8172 found_key.objectid,
8173 found_key.objectid +
8174 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008175 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008176 }
Chris Mason96b51792007-10-15 16:15:19 -04008177
Josef Bacik8c579fe2013-04-02 12:40:42 -04008178 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8179 if (ret) {
8180 btrfs_remove_free_space_cache(cache);
8181 btrfs_put_block_group(cache);
8182 goto error;
8183 }
8184
Chris Mason6324fbf2008-03-24 15:01:59 -04008185 ret = update_space_info(info, cache->flags, found_key.offset,
8186 btrfs_block_group_used(&cache->item),
8187 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008188 if (ret) {
8189 btrfs_remove_free_space_cache(cache);
8190 spin_lock(&info->block_group_cache_lock);
8191 rb_erase(&cache->cache_node,
8192 &info->block_group_cache_tree);
8193 spin_unlock(&info->block_group_cache_lock);
8194 btrfs_put_block_group(cache);
8195 goto error;
8196 }
8197
Chris Mason6324fbf2008-03-24 15:01:59 -04008198 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008199 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008200 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008201 spin_unlock(&cache->space_info->lock);
8202
Yan, Zhengb742bb82010-05-16 10:46:24 -04008203 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008204
Chris Mason75ccf472008-09-30 19:24:06 -04008205 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008206 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008207 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008208 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008209
8210 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8211 if (!(get_alloc_profile(root, space_info->flags) &
8212 (BTRFS_BLOCK_GROUP_RAID10 |
8213 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008214 BTRFS_BLOCK_GROUP_RAID5 |
8215 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008216 BTRFS_BLOCK_GROUP_DUP)))
8217 continue;
8218 /*
8219 * avoid allocating from un-mirrored block group if there are
8220 * mirrored block groups.
8221 */
8222 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008223 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008224 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008225 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008226 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008227
8228 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008229 ret = 0;
8230error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008231 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008232 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008233}
Chris Mason6324fbf2008-03-24 15:01:59 -04008234
Josef Bacikea658ba2012-09-11 16:57:25 -04008235void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8236 struct btrfs_root *root)
8237{
8238 struct btrfs_block_group_cache *block_group, *tmp;
8239 struct btrfs_root *extent_root = root->fs_info->extent_root;
8240 struct btrfs_block_group_item item;
8241 struct btrfs_key key;
8242 int ret = 0;
8243
8244 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8245 new_bg_list) {
8246 list_del_init(&block_group->new_bg_list);
8247
8248 if (ret)
8249 continue;
8250
8251 spin_lock(&block_group->lock);
8252 memcpy(&item, &block_group->item, sizeof(item));
8253 memcpy(&key, &block_group->key, sizeof(key));
8254 spin_unlock(&block_group->lock);
8255
8256 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8257 sizeof(item));
8258 if (ret)
8259 btrfs_abort_transaction(trans, extent_root, ret);
8260 }
8261}
8262
Chris Mason6324fbf2008-03-24 15:01:59 -04008263int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8264 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008265 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008266 u64 size)
8267{
8268 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008269 struct btrfs_root *extent_root;
8270 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008271
8272 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008273
Chris Mason12fcfd22009-03-24 10:24:20 -04008274 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008275
Chris Mason8f18cf12008-04-25 16:53:30 -04008276 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008277 if (!cache)
8278 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008279 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8280 GFP_NOFS);
8281 if (!cache->free_space_ctl) {
8282 kfree(cache);
8283 return -ENOMEM;
8284 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008285
Chris Masone17cade2008-04-15 15:41:47 -04008286 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008287 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008288 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008289 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008290 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008291 cache->full_stripe_len = btrfs_full_stripe_len(root,
8292 &root->fs_info->mapping_tree,
8293 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008294
Yan Zhengd2fb3432008-12-11 16:30:39 -05008295 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008296 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008297 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008298 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008299 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008300
Li Zefan34d52cb2011-03-29 13:46:06 +08008301 btrfs_init_free_space_ctl(cache);
8302
Chris Mason6324fbf2008-03-24 15:01:59 -04008303 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008304 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8305 cache->flags = type;
8306 btrfs_set_block_group_flags(&cache->item, type);
8307
Yan Zheng11833d62009-09-11 16:11:19 -04008308 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008309 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008310 ret = exclude_super_stripes(root, cache);
8311 if (ret) {
8312 /*
8313 * We may have excluded something, so call this just in
8314 * case.
8315 */
8316 free_excluded_extents(root, cache);
8317 kfree(cache->free_space_ctl);
8318 kfree(cache);
8319 return ret;
8320 }
Josef Bacik96303082009-07-13 21:29:25 -04008321
Josef Bacik817d52f2009-07-13 21:29:25 -04008322 add_new_free_space(cache, root->fs_info, chunk_offset,
8323 chunk_offset + size);
8324
Yan Zheng11833d62009-09-11 16:11:19 -04008325 free_excluded_extents(root, cache);
8326
Josef Bacik8c579fe2013-04-02 12:40:42 -04008327 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8328 if (ret) {
8329 btrfs_remove_free_space_cache(cache);
8330 btrfs_put_block_group(cache);
8331 return ret;
8332 }
8333
Chris Mason6324fbf2008-03-24 15:01:59 -04008334 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8335 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008336 if (ret) {
8337 btrfs_remove_free_space_cache(cache);
8338 spin_lock(&root->fs_info->block_group_cache_lock);
8339 rb_erase(&cache->cache_node,
8340 &root->fs_info->block_group_cache_tree);
8341 spin_unlock(&root->fs_info->block_group_cache_lock);
8342 btrfs_put_block_group(cache);
8343 return ret;
8344 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008345 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008346
8347 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008348 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008349 spin_unlock(&cache->space_info->lock);
8350
Yan, Zhengb742bb82010-05-16 10:46:24 -04008351 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008352
Josef Bacikea658ba2012-09-11 16:57:25 -04008353 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008354
Chris Masond18a2c42008-04-04 15:40:00 -04008355 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008356
Chris Mason6324fbf2008-03-24 15:01:59 -04008357 return 0;
8358}
Zheng Yan1a40e232008-09-26 10:09:34 -04008359
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008360static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8361{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008362 u64 extra_flags = chunk_to_extended(flags) &
8363 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008364
Miao Xiede98ced2013-01-29 10:13:12 +00008365 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008366 if (flags & BTRFS_BLOCK_GROUP_DATA)
8367 fs_info->avail_data_alloc_bits &= ~extra_flags;
8368 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8369 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8370 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8371 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008372 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008373}
8374
Zheng Yan1a40e232008-09-26 10:09:34 -04008375int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8376 struct btrfs_root *root, u64 group_start)
8377{
8378 struct btrfs_path *path;
8379 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008380 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008381 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008382 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008383 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008384 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008385 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008386 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008387
Zheng Yan1a40e232008-09-26 10:09:34 -04008388 root = root->fs_info->extent_root;
8389
8390 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8391 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008392 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008393
liubo9f7c43c2011-03-07 02:13:33 +00008394 /*
8395 * Free the reserved super bytes from this block group before
8396 * remove it.
8397 */
8398 free_excluded_extents(root, block_group);
8399
Zheng Yan1a40e232008-09-26 10:09:34 -04008400 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008401 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008402 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8403 BTRFS_BLOCK_GROUP_RAID1 |
8404 BTRFS_BLOCK_GROUP_RAID10))
8405 factor = 2;
8406 else
8407 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008408
Chris Mason44fb5512009-06-04 15:34:51 -04008409 /* make sure this block group isn't part of an allocation cluster */
8410 cluster = &root->fs_info->data_alloc_cluster;
8411 spin_lock(&cluster->refill_lock);
8412 btrfs_return_cluster_to_free_space(block_group, cluster);
8413 spin_unlock(&cluster->refill_lock);
8414
8415 /*
8416 * make sure this block group isn't part of a metadata
8417 * allocation cluster
8418 */
8419 cluster = &root->fs_info->meta_alloc_cluster;
8420 spin_lock(&cluster->refill_lock);
8421 btrfs_return_cluster_to_free_space(block_group, cluster);
8422 spin_unlock(&cluster->refill_lock);
8423
Zheng Yan1a40e232008-09-26 10:09:34 -04008424 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008425 if (!path) {
8426 ret = -ENOMEM;
8427 goto out;
8428 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008429
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008430 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008431 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008432 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008433 if (ret) {
8434 btrfs_add_delayed_iput(inode);
8435 goto out;
8436 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008437 clear_nlink(inode);
8438 /* One for the block groups ref */
8439 spin_lock(&block_group->lock);
8440 if (block_group->iref) {
8441 block_group->iref = 0;
8442 block_group->inode = NULL;
8443 spin_unlock(&block_group->lock);
8444 iput(inode);
8445 } else {
8446 spin_unlock(&block_group->lock);
8447 }
8448 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008449 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008450 }
8451
8452 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8453 key.offset = block_group->key.objectid;
8454 key.type = 0;
8455
8456 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8457 if (ret < 0)
8458 goto out;
8459 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008460 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008461 if (ret == 0) {
8462 ret = btrfs_del_item(trans, tree_root, path);
8463 if (ret)
8464 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008465 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008466 }
8467
Yan Zheng3dfdb932009-01-21 10:49:16 -05008468 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008469 rb_erase(&block_group->cache_node,
8470 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008471
8472 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8473 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008474 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008475
Josef Bacik80eb2342008-10-29 14:49:05 -04008476 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008477 /*
8478 * we must use list_del_init so people can check to see if they
8479 * are still on the list after taking the semaphore
8480 */
8481 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008482 if (list_empty(&block_group->space_info->block_groups[index]))
8483 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008484 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008485
Josef Bacik817d52f2009-07-13 21:29:25 -04008486 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008487 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008488
8489 btrfs_remove_free_space_cache(block_group);
8490
Yan Zhengc146afa2008-11-12 14:34:12 -05008491 spin_lock(&block_group->space_info->lock);
8492 block_group->space_info->total_bytes -= block_group->key.offset;
8493 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008494 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008495 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008496
Josef Bacik0af3d002010-06-21 14:48:16 -04008497 memcpy(&key, &block_group->key, sizeof(key));
8498
Chris Mason283bb192009-07-24 16:30:55 -04008499 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008500
Chris Masonfa9c0d792009-04-03 09:47:43 -04008501 btrfs_put_block_group(block_group);
8502 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008503
8504 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8505 if (ret > 0)
8506 ret = -EIO;
8507 if (ret < 0)
8508 goto out;
8509
8510 ret = btrfs_del_item(trans, root, path);
8511out:
8512 btrfs_free_path(path);
8513 return ret;
8514}
liuboacce9522011-01-06 19:30:25 +08008515
liuboc59021f2011-03-07 02:13:14 +00008516int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8517{
8518 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008519 struct btrfs_super_block *disk_super;
8520 u64 features;
8521 u64 flags;
8522 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008523 int ret;
8524
David Sterba6c417612011-04-13 15:41:04 +02008525 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008526 if (!btrfs_super_root(disk_super))
8527 return 1;
liuboc59021f2011-03-07 02:13:14 +00008528
liubo1aba86d2011-04-08 08:44:37 +00008529 features = btrfs_super_incompat_flags(disk_super);
8530 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8531 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008532
liubo1aba86d2011-04-08 08:44:37 +00008533 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8534 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008535 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008536 goto out;
liuboc59021f2011-03-07 02:13:14 +00008537
liubo1aba86d2011-04-08 08:44:37 +00008538 if (mixed) {
8539 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8540 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8541 } else {
8542 flags = BTRFS_BLOCK_GROUP_METADATA;
8543 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8544 if (ret)
8545 goto out;
8546
8547 flags = BTRFS_BLOCK_GROUP_DATA;
8548 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8549 }
8550out:
liuboc59021f2011-03-07 02:13:14 +00008551 return ret;
8552}
8553
liuboacce9522011-01-06 19:30:25 +08008554int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8555{
8556 return unpin_extent_range(root, start, end);
8557}
8558
8559int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008560 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008561{
Li Dongyang5378e602011-03-24 10:24:27 +00008562 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008563}
Li Dongyangf7039b12011-03-24 10:24:28 +00008564
8565int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8566{
8567 struct btrfs_fs_info *fs_info = root->fs_info;
8568 struct btrfs_block_group_cache *cache = NULL;
8569 u64 group_trimmed;
8570 u64 start;
8571 u64 end;
8572 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008573 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008574 int ret = 0;
8575
Liu Bo2cac13e2012-02-09 18:17:41 +08008576 /*
8577 * try to trim all FS space, our block group may start from non-zero.
8578 */
8579 if (range->len == total_bytes)
8580 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8581 else
8582 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008583
8584 while (cache) {
8585 if (cache->key.objectid >= (range->start + range->len)) {
8586 btrfs_put_block_group(cache);
8587 break;
8588 }
8589
8590 start = max(range->start, cache->key.objectid);
8591 end = min(range->start + range->len,
8592 cache->key.objectid + cache->key.offset);
8593
8594 if (end - start >= range->minlen) {
8595 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008596 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008597 if (!ret)
8598 wait_block_group_cache_done(cache);
8599 }
8600 ret = btrfs_trim_block_group(cache,
8601 &group_trimmed,
8602 start,
8603 end,
8604 range->minlen);
8605
8606 trimmed += group_trimmed;
8607 if (ret) {
8608 btrfs_put_block_group(cache);
8609 break;
8610 }
8611 }
8612
8613 cache = next_block_group(fs_info->tree_root, cache);
8614 }
8615
8616 range->len = trimmed;
8617 return ret;
8618}