blob: 8464d369e478ec15d43bf7c307f5af85a5f6815f [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
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001662void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001663 struct btrfs_path *path,
1664 struct btrfs_extent_inline_ref *iref,
1665 u64 parent, u64 root_objectid,
1666 u64 owner, u64 offset, int refs_to_add,
1667 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668{
1669 struct extent_buffer *leaf;
1670 struct btrfs_extent_item *ei;
1671 unsigned long ptr;
1672 unsigned long end;
1673 unsigned long item_offset;
1674 u64 refs;
1675 int size;
1676 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001677
1678 leaf = path->nodes[0];
1679 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1680 item_offset = (unsigned long)iref - (unsigned long)ei;
1681
1682 type = extent_ref_type(parent, owner);
1683 size = btrfs_extent_inline_ref_size(type);
1684
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001685 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686
1687 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1688 refs = btrfs_extent_refs(leaf, ei);
1689 refs += refs_to_add;
1690 btrfs_set_extent_refs(leaf, ei, refs);
1691 if (extent_op)
1692 __run_delayed_extent_op(extent_op, leaf, ei);
1693
1694 ptr = (unsigned long)ei + item_offset;
1695 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1696 if (ptr < end - size)
1697 memmove_extent_buffer(leaf, ptr + size, ptr,
1698 end - size - ptr);
1699
1700 iref = (struct btrfs_extent_inline_ref *)ptr;
1701 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1702 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1703 struct btrfs_extent_data_ref *dref;
1704 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1705 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1706 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1707 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1708 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1709 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1710 struct btrfs_shared_data_ref *sref;
1711 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1712 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1713 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1714 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1715 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1716 } else {
1717 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1718 }
1719 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001720}
1721
1722static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1723 struct btrfs_root *root,
1724 struct btrfs_path *path,
1725 struct btrfs_extent_inline_ref **ref_ret,
1726 u64 bytenr, u64 num_bytes, u64 parent,
1727 u64 root_objectid, u64 owner, u64 offset)
1728{
1729 int ret;
1730
1731 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1732 bytenr, num_bytes, parent,
1733 root_objectid, owner, offset, 0);
1734 if (ret != -ENOENT)
1735 return ret;
1736
David Sterbab3b4aa72011-04-21 01:20:15 +02001737 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001738 *ref_ret = NULL;
1739
1740 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1741 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1742 root_objectid);
1743 } else {
1744 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1745 root_objectid, owner, offset);
1746 }
1747 return ret;
1748}
1749
1750/*
1751 * helper to update/remove inline back ref
1752 */
1753static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001754void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001755 struct btrfs_path *path,
1756 struct btrfs_extent_inline_ref *iref,
1757 int refs_to_mod,
1758 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001759{
1760 struct extent_buffer *leaf;
1761 struct btrfs_extent_item *ei;
1762 struct btrfs_extent_data_ref *dref = NULL;
1763 struct btrfs_shared_data_ref *sref = NULL;
1764 unsigned long ptr;
1765 unsigned long end;
1766 u32 item_size;
1767 int size;
1768 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001769 u64 refs;
1770
1771 leaf = path->nodes[0];
1772 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1773 refs = btrfs_extent_refs(leaf, ei);
1774 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1775 refs += refs_to_mod;
1776 btrfs_set_extent_refs(leaf, ei, refs);
1777 if (extent_op)
1778 __run_delayed_extent_op(extent_op, leaf, ei);
1779
1780 type = btrfs_extent_inline_ref_type(leaf, iref);
1781
1782 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1783 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1784 refs = btrfs_extent_data_ref_count(leaf, dref);
1785 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1786 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1787 refs = btrfs_shared_data_ref_count(leaf, sref);
1788 } else {
1789 refs = 1;
1790 BUG_ON(refs_to_mod != -1);
1791 }
1792
1793 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1794 refs += refs_to_mod;
1795
1796 if (refs > 0) {
1797 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1798 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1799 else
1800 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1801 } else {
1802 size = btrfs_extent_inline_ref_size(type);
1803 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1804 ptr = (unsigned long)iref;
1805 end = (unsigned long)ei + item_size;
1806 if (ptr + size < end)
1807 memmove_extent_buffer(leaf, ptr, ptr + size,
1808 end - ptr - size);
1809 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001810 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001811 }
1812 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001813}
1814
1815static noinline_for_stack
1816int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1817 struct btrfs_root *root,
1818 struct btrfs_path *path,
1819 u64 bytenr, u64 num_bytes, u64 parent,
1820 u64 root_objectid, u64 owner,
1821 u64 offset, int refs_to_add,
1822 struct btrfs_delayed_extent_op *extent_op)
1823{
1824 struct btrfs_extent_inline_ref *iref;
1825 int ret;
1826
1827 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1828 bytenr, num_bytes, parent,
1829 root_objectid, owner, offset, 1);
1830 if (ret == 0) {
1831 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001832 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001833 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001834 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001835 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001836 root_objectid, owner, offset,
1837 refs_to_add, extent_op);
1838 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001839 }
1840 return ret;
1841}
1842
1843static int insert_extent_backref(struct btrfs_trans_handle *trans,
1844 struct btrfs_root *root,
1845 struct btrfs_path *path,
1846 u64 bytenr, u64 parent, u64 root_objectid,
1847 u64 owner, u64 offset, int refs_to_add)
1848{
1849 int ret;
1850 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1851 BUG_ON(refs_to_add != 1);
1852 ret = insert_tree_block_ref(trans, root, path, bytenr,
1853 parent, root_objectid);
1854 } else {
1855 ret = insert_extent_data_ref(trans, root, path, bytenr,
1856 parent, root_objectid,
1857 owner, offset, refs_to_add);
1858 }
1859 return ret;
1860}
1861
1862static int remove_extent_backref(struct btrfs_trans_handle *trans,
1863 struct btrfs_root *root,
1864 struct btrfs_path *path,
1865 struct btrfs_extent_inline_ref *iref,
1866 int refs_to_drop, int is_data)
1867{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001868 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001869
1870 BUG_ON(!is_data && refs_to_drop != 1);
1871 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001872 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001873 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001874 } else if (is_data) {
1875 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1876 } else {
1877 ret = btrfs_del_item(trans, root, path);
1878 }
1879 return ret;
1880}
1881
Li Dongyang5378e602011-03-24 10:24:27 +00001882static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001883 u64 start, u64 len)
1884{
Li Dongyang5378e602011-03-24 10:24:27 +00001885 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001886}
Chris Mason15916de2008-11-19 21:17:22 -05001887
Liu Hui1f3c79a2009-01-05 15:57:51 -05001888static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001889 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001890{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001891 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001892 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001893 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001894
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001895
Liu Hui1f3c79a2009-01-05 15:57:51 -05001896 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001897 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001898 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001899 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001900 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001901 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001902 int i;
1903
Liu Hui1f3c79a2009-01-05 15:57:51 -05001904
Jan Schmidta1d3c472011-08-04 17:15:33 +02001905 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001906 if (!stripe->dev->can_discard)
1907 continue;
1908
Li Dongyang5378e602011-03-24 10:24:27 +00001909 ret = btrfs_issue_discard(stripe->dev->bdev,
1910 stripe->physical,
1911 stripe->length);
1912 if (!ret)
1913 discarded_bytes += stripe->length;
1914 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001915 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001916
1917 /*
1918 * Just in case we get back EOPNOTSUPP for some reason,
1919 * just ignore the return value so we don't screw up
1920 * people calling discard_extent.
1921 */
1922 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001923 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001924 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001925 }
Li Dongyang5378e602011-03-24 10:24:27 +00001926
1927 if (actual_bytes)
1928 *actual_bytes = discarded_bytes;
1929
Liu Hui1f3c79a2009-01-05 15:57:51 -05001930
David Woodhouse53b381b2013-01-29 18:40:14 -05001931 if (ret == -EOPNOTSUPP)
1932 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001933 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001934}
1935
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001936/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001937int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1938 struct btrfs_root *root,
1939 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001940 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001941{
1942 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001943 struct btrfs_fs_info *fs_info = root->fs_info;
1944
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001945 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1946 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001947
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001948 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001949 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1950 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001951 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001952 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001953 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001954 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1955 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001956 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001957 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001958 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001959 return ret;
1960}
1961
Chris Mason925baed2008-06-25 16:01:30 -04001962static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001963 struct btrfs_root *root,
1964 u64 bytenr, u64 num_bytes,
1965 u64 parent, u64 root_objectid,
1966 u64 owner, u64 offset, int refs_to_add,
1967 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001968{
Chris Mason5caf2a02007-04-02 11:20:42 -04001969 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001970 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001971 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001972 u64 refs;
1973 int ret;
1974 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001975
Chris Mason5caf2a02007-04-02 11:20:42 -04001976 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001977 if (!path)
1978 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001979
Chris Mason3c12ac72008-04-21 12:01:38 -04001980 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001981 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001982 /* this will setup the path even if it fails to insert the back ref */
1983 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1984 path, bytenr, num_bytes, parent,
1985 root_objectid, owner, offset,
1986 refs_to_add, extent_op);
1987 if (ret == 0)
1988 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001990 if (ret != -EAGAIN) {
1991 err = ret;
1992 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001993 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001994
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001995 leaf = path->nodes[0];
1996 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1997 refs = btrfs_extent_refs(leaf, item);
1998 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1999 if (extent_op)
2000 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002001
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002002 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002003 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002004
Chris Mason3c12ac72008-04-21 12:01:38 -04002005 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002006 path->leave_spinning = 1;
2007
Chris Mason56bec292009-03-13 10:10:06 -04002008 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002009 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002010 path, bytenr, parent, root_objectid,
2011 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002012 if (ret)
2013 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002014out:
Chris Mason74493f72007-12-11 09:25:06 -05002015 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002016 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05002017}
2018
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002019static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2020 struct btrfs_root *root,
2021 struct btrfs_delayed_ref_node *node,
2022 struct btrfs_delayed_extent_op *extent_op,
2023 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002024{
Chris Mason56bec292009-03-13 10:10:06 -04002025 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002026 struct btrfs_delayed_data_ref *ref;
2027 struct btrfs_key ins;
2028 u64 parent = 0;
2029 u64 ref_root = 0;
2030 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002031
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002032 ins.objectid = node->bytenr;
2033 ins.offset = node->num_bytes;
2034 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002035
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002036 ref = btrfs_delayed_node_to_data_ref(node);
2037 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2038 parent = ref->parent;
2039 else
2040 ref_root = ref->root;
2041
2042 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002043 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002045 ret = alloc_reserved_file_extent(trans, root,
2046 parent, ref_root, flags,
2047 ref->objectid, ref->offset,
2048 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002049 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2050 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2051 node->num_bytes, parent,
2052 ref_root, ref->objectid,
2053 ref->offset, node->ref_mod,
2054 extent_op);
2055 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2056 ret = __btrfs_free_extent(trans, root, node->bytenr,
2057 node->num_bytes, parent,
2058 ref_root, ref->objectid,
2059 ref->offset, node->ref_mod,
2060 extent_op);
2061 } else {
2062 BUG();
2063 }
Chris Mason56bec292009-03-13 10:10:06 -04002064 return ret;
2065}
2066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002067static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2068 struct extent_buffer *leaf,
2069 struct btrfs_extent_item *ei)
2070{
2071 u64 flags = btrfs_extent_flags(leaf, ei);
2072 if (extent_op->update_flags) {
2073 flags |= extent_op->flags_to_set;
2074 btrfs_set_extent_flags(leaf, ei, flags);
2075 }
2076
2077 if (extent_op->update_key) {
2078 struct btrfs_tree_block_info *bi;
2079 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2080 bi = (struct btrfs_tree_block_info *)(ei + 1);
2081 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2082 }
2083}
2084
2085static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2086 struct btrfs_root *root,
2087 struct btrfs_delayed_ref_node *node,
2088 struct btrfs_delayed_extent_op *extent_op)
2089{
2090 struct btrfs_key key;
2091 struct btrfs_path *path;
2092 struct btrfs_extent_item *ei;
2093 struct extent_buffer *leaf;
2094 u32 item_size;
2095 int ret;
2096 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002097 int metadata = (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2098 node->type == BTRFS_SHARED_BLOCK_REF_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002099
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002100 if (trans->aborted)
2101 return 0;
2102
Josef Bacik3173a182013-03-07 14:22:04 -05002103 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2104 metadata = 0;
2105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002106 path = btrfs_alloc_path();
2107 if (!path)
2108 return -ENOMEM;
2109
2110 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002111
Josef Bacik3173a182013-03-07 14:22:04 -05002112 if (metadata) {
2113 struct btrfs_delayed_tree_ref *tree_ref;
2114
2115 tree_ref = btrfs_delayed_node_to_tree_ref(node);
2116 key.type = BTRFS_METADATA_ITEM_KEY;
2117 key.offset = tree_ref->level;
2118 } else {
2119 key.type = BTRFS_EXTENT_ITEM_KEY;
2120 key.offset = node->num_bytes;
2121 }
2122
2123again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002124 path->reada = 1;
2125 path->leave_spinning = 1;
2126 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2127 path, 0, 1);
2128 if (ret < 0) {
2129 err = ret;
2130 goto out;
2131 }
2132 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002133 if (metadata) {
2134 btrfs_release_path(path);
2135 metadata = 0;
2136
2137 key.offset = node->num_bytes;
2138 key.type = BTRFS_EXTENT_ITEM_KEY;
2139 goto again;
2140 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002141 err = -EIO;
2142 goto out;
2143 }
2144
2145 leaf = path->nodes[0];
2146 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2147#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2148 if (item_size < sizeof(*ei)) {
2149 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2150 path, (u64)-1, 0);
2151 if (ret < 0) {
2152 err = ret;
2153 goto out;
2154 }
2155 leaf = path->nodes[0];
2156 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2157 }
2158#endif
2159 BUG_ON(item_size < sizeof(*ei));
2160 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2161 __run_delayed_extent_op(extent_op, leaf, ei);
2162
2163 btrfs_mark_buffer_dirty(leaf);
2164out:
2165 btrfs_free_path(path);
2166 return err;
2167}
2168
2169static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root,
2171 struct btrfs_delayed_ref_node *node,
2172 struct btrfs_delayed_extent_op *extent_op,
2173 int insert_reserved)
2174{
2175 int ret = 0;
2176 struct btrfs_delayed_tree_ref *ref;
2177 struct btrfs_key ins;
2178 u64 parent = 0;
2179 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002180 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2181 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182
2183 ref = btrfs_delayed_node_to_tree_ref(node);
2184 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2185 parent = ref->parent;
2186 else
2187 ref_root = ref->root;
2188
Josef Bacik3173a182013-03-07 14:22:04 -05002189 ins.objectid = node->bytenr;
2190 if (skinny_metadata) {
2191 ins.offset = ref->level;
2192 ins.type = BTRFS_METADATA_ITEM_KEY;
2193 } else {
2194 ins.offset = node->num_bytes;
2195 ins.type = BTRFS_EXTENT_ITEM_KEY;
2196 }
2197
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 BUG_ON(node->ref_mod != 1);
2199 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002200 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 ret = alloc_reserved_tree_block(trans, root,
2202 parent, ref_root,
2203 extent_op->flags_to_set,
2204 &extent_op->key,
2205 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002206 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2207 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2208 node->num_bytes, parent, ref_root,
2209 ref->level, 0, 1, extent_op);
2210 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2211 ret = __btrfs_free_extent(trans, root, node->bytenr,
2212 node->num_bytes, parent, ref_root,
2213 ref->level, 0, 1, extent_op);
2214 } else {
2215 BUG();
2216 }
2217 return ret;
2218}
2219
Chris Mason56bec292009-03-13 10:10:06 -04002220/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2222 struct btrfs_root *root,
2223 struct btrfs_delayed_ref_node *node,
2224 struct btrfs_delayed_extent_op *extent_op,
2225 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002226{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002227 int ret = 0;
2228
2229 if (trans->aborted)
2230 return 0;
2231
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002232 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002233 struct btrfs_delayed_ref_head *head;
2234 /*
2235 * we've hit the end of the chain and we were supposed
2236 * to insert this extent into the tree. But, it got
2237 * deleted before we ever needed to insert it, so all
2238 * we have to do is clean up the accounting
2239 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240 BUG_ON(extent_op);
2241 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002242 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002243 btrfs_pin_extent(root, node->bytenr,
2244 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002245 if (head->is_data) {
2246 ret = btrfs_del_csums(trans, root,
2247 node->bytenr,
2248 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002249 }
Chris Mason56bec292009-03-13 10:10:06 -04002250 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002251 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002252 }
Josef Bacikeb099672009-02-12 09:27:38 -05002253
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002254 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2255 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2256 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2257 insert_reserved);
2258 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2259 node->type == BTRFS_SHARED_DATA_REF_KEY)
2260 ret = run_delayed_data_ref(trans, root, node, extent_op,
2261 insert_reserved);
2262 else
2263 BUG();
2264 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002265}
2266
Chris Mason56bec292009-03-13 10:10:06 -04002267static noinline struct btrfs_delayed_ref_node *
2268select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002269{
Chris Mason56bec292009-03-13 10:10:06 -04002270 struct rb_node *node;
2271 struct btrfs_delayed_ref_node *ref;
2272 int action = BTRFS_ADD_DELAYED_REF;
2273again:
2274 /*
2275 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2276 * this prevents ref count from going down to zero when
2277 * there still are pending delayed ref.
2278 */
2279 node = rb_prev(&head->node.rb_node);
2280 while (1) {
2281 if (!node)
2282 break;
2283 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2284 rb_node);
2285 if (ref->bytenr != head->node.bytenr)
2286 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002287 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002288 return ref;
2289 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002290 }
Chris Mason56bec292009-03-13 10:10:06 -04002291 if (action == BTRFS_ADD_DELAYED_REF) {
2292 action = BTRFS_DROP_DELAYED_REF;
2293 goto again;
2294 }
2295 return NULL;
2296}
2297
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002298/*
2299 * Returns 0 on success or if called with an already aborted transaction.
2300 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2301 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002302static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2303 struct btrfs_root *root,
2304 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002305{
Chris Mason56bec292009-03-13 10:10:06 -04002306 struct btrfs_delayed_ref_root *delayed_refs;
2307 struct btrfs_delayed_ref_node *ref;
2308 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002309 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002310 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002311 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002312 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002313 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002314
2315 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002316 while (1) {
2317 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002318 /* pick a new head ref from the cluster list */
2319 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002320 break;
Chris Mason56bec292009-03-13 10:10:06 -04002321
Chris Masonc3e69d52009-03-13 10:17:05 -04002322 locked_ref = list_entry(cluster->next,
2323 struct btrfs_delayed_ref_head, cluster);
2324
2325 /* grab the lock that says we are going to process
2326 * all the refs for this head */
2327 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2328
2329 /*
2330 * we may have dropped the spin lock to get the head
2331 * mutex lock, and that might have given someone else
2332 * time to free the head. If that's true, it has been
2333 * removed from our list and we can move on.
2334 */
2335 if (ret == -EAGAIN) {
2336 locked_ref = NULL;
2337 count++;
2338 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002339 }
2340 }
2341
2342 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002343 * We need to try and merge add/drops of the same ref since we
2344 * can run into issues with relocate dropping the implicit ref
2345 * and then it being added back again before the drop can
2346 * finish. If we merged anything we need to re-loop so we can
2347 * get a good ref.
2348 */
2349 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2350 locked_ref);
2351
2352 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002353 * locked_ref is the head node, so we have to go one
2354 * node back for any delayed ref updates
2355 */
2356 ref = select_delayed_ref(locked_ref);
2357
2358 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002359 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002360 /*
2361 * there are still refs with lower seq numbers in the
2362 * process of being added. Don't run this ref yet.
2363 */
2364 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002365 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002366 locked_ref = NULL;
2367 delayed_refs->num_heads_ready++;
2368 spin_unlock(&delayed_refs->lock);
2369 cond_resched();
2370 spin_lock(&delayed_refs->lock);
2371 continue;
2372 }
2373
2374 /*
Chris Mason56bec292009-03-13 10:10:06 -04002375 * record the must insert reserved flag before we
2376 * drop the spin lock.
2377 */
2378 must_insert_reserved = locked_ref->must_insert_reserved;
2379 locked_ref->must_insert_reserved = 0;
2380
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002381 extent_op = locked_ref->extent_op;
2382 locked_ref->extent_op = NULL;
2383
Chris Mason56bec292009-03-13 10:10:06 -04002384 if (!ref) {
2385 /* All delayed refs have been processed, Go ahead
2386 * and send the head node to run_one_delayed_ref,
2387 * so that any accounting fixes can happen
2388 */
2389 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002390
2391 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002392 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002393 extent_op = NULL;
2394 }
2395
2396 if (extent_op) {
2397 spin_unlock(&delayed_refs->lock);
2398
2399 ret = run_delayed_extent_op(trans, root,
2400 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002401 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002402
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002403 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002404 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002405 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002406 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002407 return ret;
2408 }
2409
Chris Mason203bf282012-01-06 15:23:57 -05002410 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002411 }
Chris Mason56bec292009-03-13 10:10:06 -04002412 }
2413
2414 ref->in_tree = 0;
2415 rb_erase(&ref->rb_node, &delayed_refs->root);
2416 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002417 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002418 /*
2419 * when we play the delayed ref, also correct the
2420 * ref_mod on head
2421 */
2422 switch (ref->action) {
2423 case BTRFS_ADD_DELAYED_REF:
2424 case BTRFS_ADD_DELAYED_EXTENT:
2425 locked_ref->node.ref_mod -= ref->ref_mod;
2426 break;
2427 case BTRFS_DROP_DELAYED_REF:
2428 locked_ref->node.ref_mod += ref->ref_mod;
2429 break;
2430 default:
2431 WARN_ON(1);
2432 }
2433 }
Chris Mason56bec292009-03-13 10:10:06 -04002434 spin_unlock(&delayed_refs->lock);
2435
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002436 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002437 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002438
Miao Xie78a61842012-11-21 02:21:28 +00002439 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002440 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002441 btrfs_delayed_ref_unlock(locked_ref);
2442 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002443 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002444 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002445 return ret;
2446 }
2447
Miao Xie093486c2012-12-19 08:10:10 +00002448 /*
2449 * If this node is a head, that means all the refs in this head
2450 * have been dealt with, and we will pick the next head to deal
2451 * with, so we must unlock the head and drop it from the cluster
2452 * list before we release it.
2453 */
2454 if (btrfs_delayed_ref_is_head(ref)) {
2455 list_del_init(&locked_ref->cluster);
2456 btrfs_delayed_ref_unlock(locked_ref);
2457 locked_ref = NULL;
2458 }
2459 btrfs_put_delayed_ref(ref);
2460 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002461next:
Chris Mason1887be62009-03-13 10:11:24 -04002462 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002463 spin_lock(&delayed_refs->lock);
2464 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002465 return count;
2466}
2467
Arne Jansen709c0482011-09-12 12:22:57 +02002468#ifdef SCRAMBLE_DELAYED_REFS
2469/*
2470 * Normally delayed refs get processed in ascending bytenr order. This
2471 * correlates in most cases to the order added. To expose dependencies on this
2472 * order, we start to process the tree in the middle instead of the beginning
2473 */
2474static u64 find_middle(struct rb_root *root)
2475{
2476 struct rb_node *n = root->rb_node;
2477 struct btrfs_delayed_ref_node *entry;
2478 int alt = 1;
2479 u64 middle;
2480 u64 first = 0, last = 0;
2481
2482 n = rb_first(root);
2483 if (n) {
2484 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2485 first = entry->bytenr;
2486 }
2487 n = rb_last(root);
2488 if (n) {
2489 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2490 last = entry->bytenr;
2491 }
2492 n = root->rb_node;
2493
2494 while (n) {
2495 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2496 WARN_ON(!entry->in_tree);
2497
2498 middle = entry->bytenr;
2499
2500 if (alt)
2501 n = n->rb_left;
2502 else
2503 n = n->rb_right;
2504
2505 alt = 1 - alt;
2506 }
2507 return middle;
2508}
2509#endif
2510
Arne Jansenbed92ea2012-06-28 18:03:02 +02002511int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2512 struct btrfs_fs_info *fs_info)
2513{
2514 struct qgroup_update *qgroup_update;
2515 int ret = 0;
2516
2517 if (list_empty(&trans->qgroup_ref_list) !=
2518 !trans->delayed_ref_elem.seq) {
2519 /* list without seq or seq without list */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002520 btrfs_err(fs_info,
2521 "qgroup accounting update error, list is%s empty, seq is %llu",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002522 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2523 trans->delayed_ref_elem.seq);
2524 BUG();
2525 }
2526
2527 if (!trans->delayed_ref_elem.seq)
2528 return 0;
2529
2530 while (!list_empty(&trans->qgroup_ref_list)) {
2531 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2532 struct qgroup_update, list);
2533 list_del(&qgroup_update->list);
2534 if (!ret)
2535 ret = btrfs_qgroup_account_ref(
2536 trans, fs_info, qgroup_update->node,
2537 qgroup_update->extent_op);
2538 kfree(qgroup_update);
2539 }
2540
2541 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2542
2543 return ret;
2544}
2545
Chris Masonbb721702013-01-29 18:44:12 -05002546static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2547 int count)
2548{
2549 int val = atomic_read(&delayed_refs->ref_seq);
2550
2551 if (val < seq || val >= seq + count)
2552 return 1;
2553 return 0;
2554}
2555
Chris Masonc3e69d52009-03-13 10:17:05 -04002556/*
2557 * this starts processing the delayed reference count updates and
2558 * extent insertions we have queued up so far. count can be
2559 * 0, which means to process everything in the tree at the start
2560 * of the run (but not newly added entries), or it can be some target
2561 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002562 *
2563 * Returns 0 on success or if called with an aborted transaction
2564 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002565 */
2566int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2567 struct btrfs_root *root, unsigned long count)
2568{
2569 struct rb_node *node;
2570 struct btrfs_delayed_ref_root *delayed_refs;
2571 struct btrfs_delayed_ref_node *ref;
2572 struct list_head cluster;
2573 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002574 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002575 int run_all = count == (unsigned long)-1;
2576 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002577 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002578
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002579 /* We'll clean this up in btrfs_cleanup_transaction */
2580 if (trans->aborted)
2581 return 0;
2582
Chris Masonc3e69d52009-03-13 10:17:05 -04002583 if (root == root->fs_info->extent_root)
2584 root = root->fs_info->tree_root;
2585
Jan Schmidtedf39272012-06-28 18:04:55 +02002586 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2587
Chris Masonc3e69d52009-03-13 10:17:05 -04002588 delayed_refs = &trans->transaction->delayed_refs;
2589 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002590 if (count == 0) {
2591 count = delayed_refs->num_entries * 2;
2592 run_most = 1;
2593 }
2594
2595 if (!run_all && !run_most) {
2596 int old;
2597 int seq = atomic_read(&delayed_refs->ref_seq);
2598
2599progress:
2600 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2601 if (old) {
2602 DEFINE_WAIT(__wait);
2603 if (delayed_refs->num_entries < 16348)
2604 return 0;
2605
2606 prepare_to_wait(&delayed_refs->wait, &__wait,
2607 TASK_UNINTERRUPTIBLE);
2608
2609 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2610 if (old) {
2611 schedule();
2612 finish_wait(&delayed_refs->wait, &__wait);
2613
2614 if (!refs_newer(delayed_refs, seq, 256))
2615 goto progress;
2616 else
2617 return 0;
2618 } else {
2619 finish_wait(&delayed_refs->wait, &__wait);
2620 goto again;
2621 }
2622 }
2623
2624 } else {
2625 atomic_inc(&delayed_refs->procs_running_refs);
2626 }
2627
Chris Masonc3e69d52009-03-13 10:17:05 -04002628again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002629 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002630 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002631
Arne Jansen709c0482011-09-12 12:22:57 +02002632#ifdef SCRAMBLE_DELAYED_REFS
2633 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2634#endif
2635
Chris Masonc3e69d52009-03-13 10:17:05 -04002636 while (1) {
2637 if (!(run_all || run_most) &&
2638 delayed_refs->num_heads_ready < 64)
2639 break;
2640
2641 /*
2642 * go find something we can process in the rbtree. We start at
2643 * the beginning of the tree, and then build a cluster
2644 * of refs to process starting at the first one we are able to
2645 * lock
2646 */
Jan Schmidta1686502011-12-12 16:10:07 +01002647 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002648 ret = btrfs_find_ref_cluster(trans, &cluster,
2649 delayed_refs->run_delayed_start);
2650 if (ret)
2651 break;
2652
2653 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002654 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002655 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002656 spin_unlock(&delayed_refs->lock);
2657 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002658 atomic_dec(&delayed_refs->procs_running_refs);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002659 return ret;
2660 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002661
Chris Masonbb721702013-01-29 18:44:12 -05002662 atomic_add(ret, &delayed_refs->ref_seq);
2663
Chris Masonc3e69d52009-03-13 10:17:05 -04002664 count -= min_t(unsigned long, ret, count);
2665
2666 if (count == 0)
2667 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002668
Arne Jansen1fa11e22012-08-06 14:18:51 -06002669 if (delayed_start >= delayed_refs->run_delayed_start) {
2670 if (loops == 0) {
2671 /*
2672 * btrfs_find_ref_cluster looped. let's do one
2673 * more cycle. if we don't run any delayed ref
2674 * during that cycle (because we can't because
2675 * all of them are blocked), bail out.
2676 */
2677 loops = 1;
2678 } else {
2679 /*
2680 * no runnable refs left, stop trying
2681 */
2682 BUG_ON(run_all);
2683 break;
2684 }
2685 }
2686 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002687 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002688 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002689 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002690 }
2691
Chris Mason56bec292009-03-13 10:10:06 -04002692 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002693 if (!list_empty(&trans->new_bgs)) {
2694 spin_unlock(&delayed_refs->lock);
2695 btrfs_create_pending_block_groups(trans, root);
2696 spin_lock(&delayed_refs->lock);
2697 }
2698
Chris Mason56bec292009-03-13 10:10:06 -04002699 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002700 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002701 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002702 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002703
2704 while (node) {
2705 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2706 rb_node);
2707 if (btrfs_delayed_ref_is_head(ref)) {
2708 struct btrfs_delayed_ref_head *head;
2709
2710 head = btrfs_delayed_node_to_head(ref);
2711 atomic_inc(&ref->refs);
2712
2713 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002714 /*
2715 * Mutex was contended, block until it's
2716 * released and try again
2717 */
Chris Mason56bec292009-03-13 10:10:06 -04002718 mutex_lock(&head->mutex);
2719 mutex_unlock(&head->mutex);
2720
2721 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002722 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002723 goto again;
2724 }
2725 node = rb_next(node);
2726 }
2727 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002728 schedule_timeout(1);
2729 goto again;
2730 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002731out:
Chris Masonbb721702013-01-29 18:44:12 -05002732 atomic_dec(&delayed_refs->procs_running_refs);
2733 smp_mb();
2734 if (waitqueue_active(&delayed_refs->wait))
2735 wake_up(&delayed_refs->wait);
2736
Chris Masonc3e69d52009-03-13 10:17:05 -04002737 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002738 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002739 return 0;
2740}
2741
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002742int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2743 struct btrfs_root *root,
2744 u64 bytenr, u64 num_bytes, u64 flags,
2745 int is_data)
2746{
2747 struct btrfs_delayed_extent_op *extent_op;
2748 int ret;
2749
Miao Xie78a61842012-11-21 02:21:28 +00002750 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002751 if (!extent_op)
2752 return -ENOMEM;
2753
2754 extent_op->flags_to_set = flags;
2755 extent_op->update_flags = 1;
2756 extent_op->update_key = 0;
2757 extent_op->is_data = is_data ? 1 : 0;
2758
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002759 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2760 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002761 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002762 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002763 return ret;
2764}
2765
2766static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2767 struct btrfs_root *root,
2768 struct btrfs_path *path,
2769 u64 objectid, u64 offset, u64 bytenr)
2770{
2771 struct btrfs_delayed_ref_head *head;
2772 struct btrfs_delayed_ref_node *ref;
2773 struct btrfs_delayed_data_ref *data_ref;
2774 struct btrfs_delayed_ref_root *delayed_refs;
2775 struct rb_node *node;
2776 int ret = 0;
2777
2778 ret = -ENOENT;
2779 delayed_refs = &trans->transaction->delayed_refs;
2780 spin_lock(&delayed_refs->lock);
2781 head = btrfs_find_delayed_ref_head(trans, bytenr);
2782 if (!head)
2783 goto out;
2784
2785 if (!mutex_trylock(&head->mutex)) {
2786 atomic_inc(&head->node.refs);
2787 spin_unlock(&delayed_refs->lock);
2788
David Sterbab3b4aa72011-04-21 01:20:15 +02002789 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002790
David Sterba8cc33e52011-05-02 15:29:25 +02002791 /*
2792 * Mutex was contended, block until it's released and let
2793 * caller try again
2794 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002795 mutex_lock(&head->mutex);
2796 mutex_unlock(&head->mutex);
2797 btrfs_put_delayed_ref(&head->node);
2798 return -EAGAIN;
2799 }
2800
2801 node = rb_prev(&head->node.rb_node);
2802 if (!node)
2803 goto out_unlock;
2804
2805 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2806
2807 if (ref->bytenr != bytenr)
2808 goto out_unlock;
2809
2810 ret = 1;
2811 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2812 goto out_unlock;
2813
2814 data_ref = btrfs_delayed_node_to_data_ref(ref);
2815
2816 node = rb_prev(node);
2817 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002818 int seq = ref->seq;
2819
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002820 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002821 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002822 goto out_unlock;
2823 }
2824
2825 if (data_ref->root != root->root_key.objectid ||
2826 data_ref->objectid != objectid || data_ref->offset != offset)
2827 goto out_unlock;
2828
2829 ret = 0;
2830out_unlock:
2831 mutex_unlock(&head->mutex);
2832out:
2833 spin_unlock(&delayed_refs->lock);
2834 return ret;
2835}
2836
2837static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2838 struct btrfs_root *root,
2839 struct btrfs_path *path,
2840 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002841{
2842 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002843 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002844 struct btrfs_extent_data_ref *ref;
2845 struct btrfs_extent_inline_ref *iref;
2846 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002847 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002848 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002849 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002850
Chris Masonbe20aa92007-12-17 20:14:01 -05002851 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002852 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002853 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002854
Chris Masonbe20aa92007-12-17 20:14:01 -05002855 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2856 if (ret < 0)
2857 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002858 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002859
2860 ret = -ENOENT;
2861 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002862 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002863
Zheng Yan31840ae2008-09-23 13:14:14 -04002864 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002865 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002866 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002868 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002869 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002870
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002871 ret = 1;
2872 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2873#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2874 if (item_size < sizeof(*ei)) {
2875 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2876 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002877 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002878#endif
2879 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2880
2881 if (item_size != sizeof(*ei) +
2882 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2883 goto out;
2884
2885 if (btrfs_extent_generation(leaf, ei) <=
2886 btrfs_root_last_snapshot(&root->root_item))
2887 goto out;
2888
2889 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2890 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2891 BTRFS_EXTENT_DATA_REF_KEY)
2892 goto out;
2893
2894 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2895 if (btrfs_extent_refs(leaf, ei) !=
2896 btrfs_extent_data_ref_count(leaf, ref) ||
2897 btrfs_extent_data_ref_root(leaf, ref) !=
2898 root->root_key.objectid ||
2899 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2900 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2901 goto out;
2902
Yan Zhengf321e492008-07-30 09:26:11 -04002903 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002904out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002905 return ret;
2906}
2907
2908int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2909 struct btrfs_root *root,
2910 u64 objectid, u64 offset, u64 bytenr)
2911{
2912 struct btrfs_path *path;
2913 int ret;
2914 int ret2;
2915
2916 path = btrfs_alloc_path();
2917 if (!path)
2918 return -ENOENT;
2919
2920 do {
2921 ret = check_committed_ref(trans, root, path, objectid,
2922 offset, bytenr);
2923 if (ret && ret != -ENOENT)
2924 goto out;
2925
2926 ret2 = check_delayed_ref(trans, root, path, objectid,
2927 offset, bytenr);
2928 } while (ret2 == -EAGAIN);
2929
2930 if (ret2 && ret2 != -ENOENT) {
2931 ret = ret2;
2932 goto out;
2933 }
2934
2935 if (ret != -ENOENT || ret2 != -ENOENT)
2936 ret = 0;
2937out:
Yan Zhengf321e492008-07-30 09:26:11 -04002938 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002939 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2940 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002941 return ret;
2942}
2943
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002944static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002945 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002946 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002947 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002948{
2949 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002950 u64 num_bytes;
2951 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002952 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002953 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002954 struct btrfs_key key;
2955 struct btrfs_file_extent_item *fi;
2956 int i;
2957 int level;
2958 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002959 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002960 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002961
2962 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002963 nritems = btrfs_header_nritems(buf);
2964 level = btrfs_header_level(buf);
2965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002966 if (!root->ref_cows && level == 0)
2967 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002968
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002969 if (inc)
2970 process_func = btrfs_inc_extent_ref;
2971 else
2972 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002973
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002974 if (full_backref)
2975 parent = buf->start;
2976 else
2977 parent = 0;
2978
Zheng Yan31840ae2008-09-23 13:14:14 -04002979 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002980 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002981 btrfs_item_key_to_cpu(buf, &key, i);
2982 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002983 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002984 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002985 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002986 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002987 BTRFS_FILE_EXTENT_INLINE)
2988 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002989 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2990 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002991 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002992
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002993 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2994 key.offset -= btrfs_file_extent_offset(buf, fi);
2995 ret = process_func(trans, root, bytenr, num_bytes,
2996 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002997 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002998 if (ret)
2999 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003000 } else {
3001 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003002 num_bytes = btrfs_level_size(root, level - 1);
3003 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003004 parent, ref_root, level - 1, 0,
3005 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003006 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003007 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003008 }
3009 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003010 return 0;
3011fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003012 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003013}
3014
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003015int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003016 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04003017{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003018 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003019}
Zheng Yan31840ae2008-09-23 13:14:14 -04003020
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003021int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003022 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003023{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003024 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04003025}
3026
Chris Mason9078a3e2007-04-26 16:46:15 -04003027static int write_one_cache_group(struct btrfs_trans_handle *trans,
3028 struct btrfs_root *root,
3029 struct btrfs_path *path,
3030 struct btrfs_block_group_cache *cache)
3031{
3032 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003033 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003034 unsigned long bi;
3035 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003036
Chris Mason9078a3e2007-04-26 16:46:15 -04003037 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003038 if (ret < 0)
3039 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003040 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003041
3042 leaf = path->nodes[0];
3043 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3044 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3045 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003046 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003047fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003048 if (ret) {
3049 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003050 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003051 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003052 return 0;
3053
3054}
3055
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003056static struct btrfs_block_group_cache *
3057next_block_group(struct btrfs_root *root,
3058 struct btrfs_block_group_cache *cache)
3059{
3060 struct rb_node *node;
3061 spin_lock(&root->fs_info->block_group_cache_lock);
3062 node = rb_next(&cache->cache_node);
3063 btrfs_put_block_group(cache);
3064 if (node) {
3065 cache = rb_entry(node, struct btrfs_block_group_cache,
3066 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003067 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003068 } else
3069 cache = NULL;
3070 spin_unlock(&root->fs_info->block_group_cache_lock);
3071 return cache;
3072}
3073
Josef Bacik0af3d002010-06-21 14:48:16 -04003074static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3075 struct btrfs_trans_handle *trans,
3076 struct btrfs_path *path)
3077{
3078 struct btrfs_root *root = block_group->fs_info->tree_root;
3079 struct inode *inode = NULL;
3080 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003081 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003082 int num_pages = 0;
3083 int retries = 0;
3084 int ret = 0;
3085
3086 /*
3087 * If this block group is smaller than 100 megs don't bother caching the
3088 * block group.
3089 */
3090 if (block_group->key.offset < (100 * 1024 * 1024)) {
3091 spin_lock(&block_group->lock);
3092 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3093 spin_unlock(&block_group->lock);
3094 return 0;
3095 }
3096
3097again:
3098 inode = lookup_free_space_inode(root, block_group, path);
3099 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3100 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003101 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003102 goto out;
3103 }
3104
3105 if (IS_ERR(inode)) {
3106 BUG_ON(retries);
3107 retries++;
3108
3109 if (block_group->ro)
3110 goto out_free;
3111
3112 ret = create_free_space_inode(root, trans, block_group, path);
3113 if (ret)
3114 goto out_free;
3115 goto again;
3116 }
3117
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003118 /* We've already setup this transaction, go ahead and exit */
3119 if (block_group->cache_generation == trans->transid &&
3120 i_size_read(inode)) {
3121 dcs = BTRFS_DC_SETUP;
3122 goto out_put;
3123 }
3124
Josef Bacik0af3d002010-06-21 14:48:16 -04003125 /*
3126 * We want to set the generation to 0, that way if anything goes wrong
3127 * from here on out we know not to trust this cache when we load up next
3128 * time.
3129 */
3130 BTRFS_I(inode)->generation = 0;
3131 ret = btrfs_update_inode(trans, root, inode);
3132 WARN_ON(ret);
3133
3134 if (i_size_read(inode) > 0) {
3135 ret = btrfs_truncate_free_space_cache(root, trans, path,
3136 inode);
3137 if (ret)
3138 goto out_put;
3139 }
3140
3141 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003142 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3143 !btrfs_test_opt(root, SPACE_CACHE)) {
3144 /*
3145 * don't bother trying to write stuff out _if_
3146 * a) we're not cached,
3147 * b) we're with nospace_cache mount option.
3148 */
Josef Bacik2b209822010-12-03 13:17:53 -05003149 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003150 spin_unlock(&block_group->lock);
3151 goto out_put;
3152 }
3153 spin_unlock(&block_group->lock);
3154
Josef Bacik6fc823b2012-08-06 13:46:38 -06003155 /*
3156 * Try to preallocate enough space based on how big the block group is.
3157 * Keep in mind this has to include any pinned space which could end up
3158 * taking up quite a bit since it's not folded into the other space
3159 * cache.
3160 */
3161 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003162 if (!num_pages)
3163 num_pages = 1;
3164
Josef Bacik0af3d002010-06-21 14:48:16 -04003165 num_pages *= 16;
3166 num_pages *= PAGE_CACHE_SIZE;
3167
3168 ret = btrfs_check_data_free_space(inode, num_pages);
3169 if (ret)
3170 goto out_put;
3171
3172 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3173 num_pages, num_pages,
3174 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003175 if (!ret)
3176 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003177 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003178
Josef Bacik0af3d002010-06-21 14:48:16 -04003179out_put:
3180 iput(inode);
3181out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003182 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003183out:
3184 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003185 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003186 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003187 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003188 spin_unlock(&block_group->lock);
3189
3190 return ret;
3191}
3192
Chris Mason96b51792007-10-15 16:15:19 -04003193int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3194 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003195{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003196 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003197 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003198 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003199 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003200
3201 path = btrfs_alloc_path();
3202 if (!path)
3203 return -ENOMEM;
3204
Josef Bacik0af3d002010-06-21 14:48:16 -04003205again:
3206 while (1) {
3207 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3208 while (cache) {
3209 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3210 break;
3211 cache = next_block_group(root, cache);
3212 }
3213 if (!cache) {
3214 if (last == 0)
3215 break;
3216 last = 0;
3217 continue;
3218 }
3219 err = cache_save_setup(cache, trans, path);
3220 last = cache->key.objectid + cache->key.offset;
3221 btrfs_put_block_group(cache);
3222 }
3223
Chris Masond3977122009-01-05 21:25:51 -05003224 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003225 if (last == 0) {
3226 err = btrfs_run_delayed_refs(trans, root,
3227 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003228 if (err) /* File system offline */
3229 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003230 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003231
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003232 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3233 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003234 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3235 btrfs_put_block_group(cache);
3236 goto again;
3237 }
3238
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003239 if (cache->dirty)
3240 break;
3241 cache = next_block_group(root, cache);
3242 }
3243 if (!cache) {
3244 if (last == 0)
3245 break;
3246 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003247 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003248 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003249
Josef Bacik0cb59c92010-07-02 12:14:14 -04003250 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3251 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003252 cache->dirty = 0;
3253 last = cache->key.objectid + cache->key.offset;
3254
3255 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003256 if (err) /* File system offline */
3257 goto out;
3258
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003259 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003260 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003261
Josef Bacik0cb59c92010-07-02 12:14:14 -04003262 while (1) {
3263 /*
3264 * I don't think this is needed since we're just marking our
3265 * preallocated extent as written, but just in case it can't
3266 * hurt.
3267 */
3268 if (last == 0) {
3269 err = btrfs_run_delayed_refs(trans, root,
3270 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003271 if (err) /* File system offline */
3272 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003273 }
3274
3275 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3276 while (cache) {
3277 /*
3278 * Really this shouldn't happen, but it could if we
3279 * couldn't write the entire preallocated extent and
3280 * splitting the extent resulted in a new block.
3281 */
3282 if (cache->dirty) {
3283 btrfs_put_block_group(cache);
3284 goto again;
3285 }
3286 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3287 break;
3288 cache = next_block_group(root, cache);
3289 }
3290 if (!cache) {
3291 if (last == 0)
3292 break;
3293 last = 0;
3294 continue;
3295 }
3296
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003297 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003298
3299 /*
3300 * If we didn't have an error then the cache state is still
3301 * NEED_WRITE, so we can set it to WRITTEN.
3302 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003303 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003304 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3305 last = cache->key.objectid + cache->key.offset;
3306 btrfs_put_block_group(cache);
3307 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003308out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003309
Chris Mason9078a3e2007-04-26 16:46:15 -04003310 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003311 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003312}
3313
Yan Zhengd2fb3432008-12-11 16:30:39 -05003314int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3315{
3316 struct btrfs_block_group_cache *block_group;
3317 int readonly = 0;
3318
3319 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3320 if (!block_group || block_group->ro)
3321 readonly = 1;
3322 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003323 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003324 return readonly;
3325}
3326
Chris Mason593060d2008-03-25 16:50:33 -04003327static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3328 u64 total_bytes, u64 bytes_used,
3329 struct btrfs_space_info **space_info)
3330{
3331 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003332 int i;
3333 int factor;
3334
3335 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3336 BTRFS_BLOCK_GROUP_RAID10))
3337 factor = 2;
3338 else
3339 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003340
3341 found = __find_space_info(info, flags);
3342 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003343 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003344 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003345 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003346 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003347 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003348 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003349 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003350 *space_info = found;
3351 return 0;
3352 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003353 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003354 if (!found)
3355 return -ENOMEM;
3356
Yan, Zhengb742bb82010-05-16 10:46:24 -04003357 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3358 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003359 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003360 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003361 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003362 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003363 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003364 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003365 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003366 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003367 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003368 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003369 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003370 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003371 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003372 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003373 found->flush = 0;
3374 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003375 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003376 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003377 if (flags & BTRFS_BLOCK_GROUP_DATA)
3378 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003379 return 0;
3380}
3381
Chris Mason8790d502008-04-03 16:29:03 -04003382static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3383{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003384 u64 extra_flags = chunk_to_extended(flags) &
3385 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003386
Miao Xiede98ced2013-01-29 10:13:12 +00003387 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003388 if (flags & BTRFS_BLOCK_GROUP_DATA)
3389 fs_info->avail_data_alloc_bits |= extra_flags;
3390 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3391 fs_info->avail_metadata_alloc_bits |= extra_flags;
3392 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3393 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003394 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003395}
Chris Mason593060d2008-03-25 16:50:33 -04003396
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003397/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003398 * returns target flags in extended format or 0 if restripe for this
3399 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003400 *
3401 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003402 */
3403static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3404{
3405 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3406 u64 target = 0;
3407
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003408 if (!bctl)
3409 return 0;
3410
3411 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3412 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3413 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3414 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3415 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3416 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3417 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3418 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3419 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3420 }
3421
3422 return target;
3423}
3424
3425/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003426 * @flags: available profiles in extended format (see ctree.h)
3427 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003428 * Returns reduced profile in chunk format. If profile changing is in
3429 * progress (either running or paused) picks the target profile (if it's
3430 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003431 */
Yan Zheng2b820322008-11-17 21:11:30 -05003432u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003433{
Chris Masoncd02dca2010-12-13 14:56:23 -05003434 /*
3435 * we add in the count of missing devices because we want
3436 * to make sure that any RAID levels on a degraded FS
3437 * continue to be honored.
3438 */
3439 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3440 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003441 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003442 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003443
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003444 /*
3445 * see if restripe for this chunk_type is in progress, if so
3446 * try to reduce to the target profile
3447 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003448 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003449 target = get_restripe_target(root->fs_info, flags);
3450 if (target) {
3451 /* pick target profile only if it's already available */
3452 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003453 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003454 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003455 }
3456 }
3457 spin_unlock(&root->fs_info->balance_lock);
3458
David Woodhouse53b381b2013-01-29 18:40:14 -05003459 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003460 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003461 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3462 BTRFS_BLOCK_GROUP_RAID5);
3463 if (num_devices < 3)
3464 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003465 if (num_devices < 4)
3466 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3467
David Woodhouse53b381b2013-01-29 18:40:14 -05003468 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3469 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3470 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3471 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003472
David Woodhouse53b381b2013-01-29 18:40:14 -05003473 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3474 tmp = BTRFS_BLOCK_GROUP_RAID6;
3475 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3476 tmp = BTRFS_BLOCK_GROUP_RAID5;
3477 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3478 tmp = BTRFS_BLOCK_GROUP_RAID10;
3479 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3480 tmp = BTRFS_BLOCK_GROUP_RAID1;
3481 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3482 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003483
David Woodhouse53b381b2013-01-29 18:40:14 -05003484 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003485}
3486
Yan, Zhengb742bb82010-05-16 10:46:24 -04003487static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003488{
Miao Xiede98ced2013-01-29 10:13:12 +00003489 unsigned seq;
3490
3491 do {
3492 seq = read_seqbegin(&root->fs_info->profiles_lock);
3493
3494 if (flags & BTRFS_BLOCK_GROUP_DATA)
3495 flags |= root->fs_info->avail_data_alloc_bits;
3496 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3497 flags |= root->fs_info->avail_system_alloc_bits;
3498 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3499 flags |= root->fs_info->avail_metadata_alloc_bits;
3500 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003501
Yan, Zhengb742bb82010-05-16 10:46:24 -04003502 return btrfs_reduce_alloc_profile(root, flags);
3503}
Josef Bacik6a632092009-02-20 11:00:09 -05003504
Miao Xie6d07bce2011-01-05 10:07:31 +00003505u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003506{
3507 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003508 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003509
Yan, Zhengb742bb82010-05-16 10:46:24 -04003510 if (data)
3511 flags = BTRFS_BLOCK_GROUP_DATA;
3512 else if (root == root->fs_info->chunk_root)
3513 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3514 else
3515 flags = BTRFS_BLOCK_GROUP_METADATA;
3516
David Woodhouse53b381b2013-01-29 18:40:14 -05003517 ret = get_alloc_profile(root, flags);
3518 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003519}
3520
Josef Bacik6a632092009-02-20 11:00:09 -05003521/*
3522 * This will check the space that the inode allocates from to make sure we have
3523 * enough space for bytes.
3524 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003525int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003526{
3527 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003528 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003529 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003530 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003531 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003532
3533 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003534 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003535
Li Zefan82d59022011-04-20 10:33:24 +08003536 if (root == root->fs_info->tree_root ||
3537 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003538 alloc_chunk = 0;
3539 committed = 1;
3540 }
3541
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003542 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003543 if (!data_sinfo)
3544 goto alloc;
3545
Josef Bacik6a632092009-02-20 11:00:09 -05003546again:
3547 /* make sure we have enough space to handle the data first */
3548 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003549 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3550 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3551 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003552
3553 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003554 struct btrfs_trans_handle *trans;
3555
Josef Bacik6a632092009-02-20 11:00:09 -05003556 /*
3557 * if we don't have enough free bytes in this space then we need
3558 * to alloc a new chunk.
3559 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003560 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003561 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003562
Chris Mason0e4f8f82011-04-15 16:05:44 -04003563 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003564 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003565alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003566 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003567 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003568 if (IS_ERR(trans))
3569 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003570
3571 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003572 alloc_target,
3573 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003574 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003575 if (ret < 0) {
3576 if (ret != -ENOSPC)
3577 return ret;
3578 else
3579 goto commit_trans;
3580 }
Chris Mason33b4d472009-09-22 14:45:50 -04003581
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003582 if (!data_sinfo)
3583 data_sinfo = fs_info->data_sinfo;
3584
Josef Bacik6a632092009-02-20 11:00:09 -05003585 goto again;
3586 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003587
3588 /*
3589 * If we have less pinned bytes than we want to allocate then
3590 * don't bother committing the transaction, it won't help us.
3591 */
3592 if (data_sinfo->bytes_pinned < bytes)
3593 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003594 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003595
3596 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003597commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003598 if (!committed &&
3599 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003600 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003601 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003602 if (IS_ERR(trans))
3603 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003604 ret = btrfs_commit_transaction(trans, root);
3605 if (ret)
3606 return ret;
3607 goto again;
3608 }
3609
Josef Bacik6a632092009-02-20 11:00:09 -05003610 return -ENOSPC;
3611 }
3612 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003613 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003614 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003615 spin_unlock(&data_sinfo->lock);
3616
Josef Bacik9ed74f22009-09-11 16:12:44 -04003617 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003618}
3619
3620/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003621 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003622 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003623void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003624{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003625 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003626 struct btrfs_space_info *data_sinfo;
3627
3628 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003629 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003630
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003631 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003632 spin_lock(&data_sinfo->lock);
3633 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003634 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003635 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003636 spin_unlock(&data_sinfo->lock);
3637}
3638
Josef Bacik97e728d2009-04-21 17:40:57 -04003639static void force_metadata_allocation(struct btrfs_fs_info *info)
3640{
3641 struct list_head *head = &info->space_info;
3642 struct btrfs_space_info *found;
3643
3644 rcu_read_lock();
3645 list_for_each_entry_rcu(found, head, list) {
3646 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003647 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003648 }
3649 rcu_read_unlock();
3650}
3651
Chris Masone5bc2452010-10-26 13:37:56 -04003652static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003653 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003654{
Josef Bacikfb25e912011-07-26 17:00:46 -04003655 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003656 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003657 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003658 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003659
Chris Mason0e4f8f82011-04-15 16:05:44 -04003660 if (force == CHUNK_ALLOC_FORCE)
3661 return 1;
3662
3663 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003664 * We need to take into account the global rsv because for all intents
3665 * and purposes it's used space. Don't worry about locking the
3666 * global_rsv, it doesn't change except when the transaction commits.
3667 */
Josef Bacik54338b52012-08-14 16:20:52 -04003668 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3669 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003670
3671 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003672 * in limited mode, we want to have some free space up to
3673 * about 1% of the FS size.
3674 */
3675 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003676 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003677 thresh = max_t(u64, 64 * 1024 * 1024,
3678 div_factor_fine(thresh, 1));
3679
3680 if (num_bytes - num_allocated < thresh)
3681 return 1;
3682 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003683
Josef Bacik698d0082012-09-12 14:08:47 -04003684 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003685 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003686 return 1;
3687}
3688
Liu Bo15d1ff82012-03-29 09:57:44 -04003689static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3690{
3691 u64 num_dev;
3692
David Woodhouse53b381b2013-01-29 18:40:14 -05003693 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3694 BTRFS_BLOCK_GROUP_RAID0 |
3695 BTRFS_BLOCK_GROUP_RAID5 |
3696 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003697 num_dev = root->fs_info->fs_devices->rw_devices;
3698 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3699 num_dev = 2;
3700 else
3701 num_dev = 1; /* DUP or single */
3702
3703 /* metadata for updaing devices and chunk tree */
3704 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3705}
3706
3707static void check_system_chunk(struct btrfs_trans_handle *trans,
3708 struct btrfs_root *root, u64 type)
3709{
3710 struct btrfs_space_info *info;
3711 u64 left;
3712 u64 thresh;
3713
3714 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3715 spin_lock(&info->lock);
3716 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3717 info->bytes_reserved - info->bytes_readonly;
3718 spin_unlock(&info->lock);
3719
3720 thresh = get_system_chunk_thresh(root, type);
3721 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003722 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3723 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003724 dump_space_info(info, 0, 0);
3725 }
3726
3727 if (left < thresh) {
3728 u64 flags;
3729
3730 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3731 btrfs_alloc_chunk(trans, root, flags);
3732 }
3733}
3734
Chris Mason6324fbf2008-03-24 15:01:59 -04003735static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003736 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003737{
3738 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003739 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003740 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003741 int ret = 0;
3742
Josef Bacikc6b305a2012-12-18 09:16:16 -05003743 /* Don't re-enter if we're already allocating a chunk */
3744 if (trans->allocating_chunk)
3745 return -ENOSPC;
3746
Chris Mason6324fbf2008-03-24 15:01:59 -04003747 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003748 if (!space_info) {
3749 ret = update_space_info(extent_root->fs_info, flags,
3750 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003751 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003752 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003753 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003754
Josef Bacik6d741192011-04-11 20:20:11 -04003755again:
Josef Bacik25179202008-10-29 14:49:05 -04003756 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003757 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003758 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003759 if (space_info->full) {
3760 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003761 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003762 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003763
Josef Bacik698d0082012-09-12 14:08:47 -04003764 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003765 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003766 return 0;
3767 } else if (space_info->chunk_alloc) {
3768 wait_for_alloc = 1;
3769 } else {
3770 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003771 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003772
Josef Bacik25179202008-10-29 14:49:05 -04003773 spin_unlock(&space_info->lock);
3774
Josef Bacik6d741192011-04-11 20:20:11 -04003775 mutex_lock(&fs_info->chunk_mutex);
3776
3777 /*
3778 * The chunk_mutex is held throughout the entirety of a chunk
3779 * allocation, so once we've acquired the chunk_mutex we know that the
3780 * other guy is done and we need to recheck and see if we should
3781 * allocate.
3782 */
3783 if (wait_for_alloc) {
3784 mutex_unlock(&fs_info->chunk_mutex);
3785 wait_for_alloc = 0;
3786 goto again;
3787 }
3788
Josef Bacikc6b305a2012-12-18 09:16:16 -05003789 trans->allocating_chunk = true;
3790
Josef Bacik97e728d2009-04-21 17:40:57 -04003791 /*
Josef Bacik67377732010-09-16 16:19:09 -04003792 * If we have mixed data/metadata chunks we want to make sure we keep
3793 * allocating mixed chunks instead of individual chunks.
3794 */
3795 if (btrfs_mixed_space_info(space_info))
3796 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3797
3798 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003799 * if we're doing a data chunk, go ahead and make sure that
3800 * we keep a reasonable number of metadata chunks allocated in the
3801 * FS as well.
3802 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003803 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003804 fs_info->data_chunk_allocations++;
3805 if (!(fs_info->data_chunk_allocations %
3806 fs_info->metadata_ratio))
3807 force_metadata_allocation(fs_info);
3808 }
3809
Liu Bo15d1ff82012-03-29 09:57:44 -04003810 /*
3811 * Check if we have enough space in SYSTEM chunk because we may need
3812 * to update devices.
3813 */
3814 check_system_chunk(trans, extent_root, flags);
3815
Yan Zheng2b820322008-11-17 21:11:30 -05003816 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003817 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003818
Josef Bacik9ed74f22009-09-11 16:12:44 -04003819 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003820 if (ret < 0 && ret != -ENOSPC)
3821 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003822 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003823 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003824 else
3825 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003826
Chris Mason0e4f8f82011-04-15 16:05:44 -04003827 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003828out:
Josef Bacik6d741192011-04-11 20:20:11 -04003829 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003830 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003831 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003832 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003833}
3834
Josef Bacika80c8dc2012-09-06 16:59:33 -04003835static int can_overcommit(struct btrfs_root *root,
3836 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003837 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003838{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003839 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003840 u64 profile = btrfs_get_alloc_profile(root, 0);
Josef Bacik96f1bb52013-01-30 17:02:51 -05003841 u64 rsv_size = 0;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003842 u64 avail;
3843 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003844 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003845
3846 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003847 space_info->bytes_pinned + space_info->bytes_readonly;
3848
3849 spin_lock(&global_rsv->lock);
3850 rsv_size = global_rsv->size;
3851 spin_unlock(&global_rsv->lock);
3852
3853 /*
3854 * We only want to allow over committing if we have lots of actual space
3855 * free, but if we don't have enough space to handle the global reserve
3856 * space then we could end up having a real enospc problem when trying
3857 * to allocate a chunk or some other such important allocation.
3858 */
3859 rsv_size <<= 1;
3860 if (used + rsv_size >= space_info->total_bytes)
3861 return 0;
3862
3863 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003864
3865 spin_lock(&root->fs_info->free_chunk_lock);
3866 avail = root->fs_info->free_chunk_space;
3867 spin_unlock(&root->fs_info->free_chunk_lock);
3868
3869 /*
3870 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003871 * space is actually useable. For raid56, the space info used
3872 * doesn't include the parity drive, so we don't have to
3873 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003874 */
3875 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3876 BTRFS_BLOCK_GROUP_RAID1 |
3877 BTRFS_BLOCK_GROUP_RAID10))
3878 avail >>= 1;
3879
Josef Bacik70afa392013-02-06 13:53:19 -05003880 to_add = space_info->total_bytes;
3881
Josef Bacika80c8dc2012-09-06 16:59:33 -04003882 /*
Miao Xie561c2942012-10-16 11:32:18 +00003883 * If we aren't flushing all things, let us overcommit up to
3884 * 1/2th of the space. If we can flush, don't let us overcommit
3885 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003886 */
Miao Xie08e007d2012-10-16 11:33:38 +00003887 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003888 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003889 else
Josef Bacik70afa392013-02-06 13:53:19 -05003890 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003891
Josef Bacik70afa392013-02-06 13:53:19 -05003892 /*
3893 * Limit the overcommit to the amount of free space we could possibly
3894 * allocate for chunks.
3895 */
3896 to_add = min(avail, to_add);
3897
3898 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003899 return 1;
3900 return 0;
3901}
3902
Miao Xieda633a42012-12-20 11:19:09 +00003903void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3904 unsigned long nr_pages)
3905{
3906 struct super_block *sb = root->fs_info->sb;
3907 int started;
3908
3909 /* If we can not start writeback, just sync all the delalloc file. */
Linus Torvaldsb6951882013-03-02 16:41:54 -08003910 started = try_to_writeback_inodes_sb_nr(sb, nr_pages,
Miao Xieda633a42012-12-20 11:19:09 +00003911 WB_REASON_FS_FREE_SPACE);
3912 if (!started) {
3913 /*
3914 * We needn't worry the filesystem going from r/w to r/o though
3915 * we don't acquire ->s_umount mutex, because the filesystem
3916 * should guarantee the delalloc inodes list be empty after
3917 * the filesystem is readonly(all dirty pages are written to
3918 * the disk).
3919 */
3920 btrfs_start_delalloc_inodes(root, 0);
Josef Bacik98ad69c2013-04-04 11:55:49 -04003921 if (!current->journal_info)
3922 btrfs_wait_ordered_extents(root, 0);
Miao Xieda633a42012-12-20 11:19:09 +00003923 }
3924}
3925
Yan, Zheng5da9d012010-05-16 10:46:25 -04003926/*
3927 * shrink metadata reservation for delalloc
3928 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003929static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3930 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003931{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003932 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003933 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003934 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003935 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003936 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003937 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003938 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003939 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003940 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003941
Josef Bacik663350a2011-11-03 22:54:25 -04003942 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003943 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003944 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003945
3946 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003947 delalloc_bytes = percpu_counter_sum_positive(
3948 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003949 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003950 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003951 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003952 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003953 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003954 }
3955
Josef Bacikf4c738c2012-07-02 17:10:51 -04003956 while (delalloc_bytes && loops < 3) {
3957 max_reclaim = min(delalloc_bytes, to_reclaim);
3958 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003959 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003960 /*
3961 * We need to wait for the async pages to actually start before
3962 * we do anything.
3963 */
3964 wait_event(root->fs_info->async_submit_wait,
3965 !atomic_read(&root->fs_info->async_delalloc_pages));
3966
Miao Xie08e007d2012-10-16 11:33:38 +00003967 if (!trans)
3968 flush = BTRFS_RESERVE_FLUSH_ALL;
3969 else
3970 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003971 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003972 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003973 spin_unlock(&space_info->lock);
3974 break;
3975 }
Josef Bacik0019f102010-10-15 15:18:40 -04003976 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003977
Chris Mason36e39c42011-03-12 07:08:42 -05003978 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003979 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003980 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003981 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003982 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003983 if (time_left)
3984 break;
3985 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003986 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003987 delalloc_bytes = percpu_counter_sum_positive(
3988 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003989 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003990}
3991
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003992/**
Josef Bacik663350a2011-11-03 22:54:25 -04003993 * maybe_commit_transaction - possibly commit the transaction if its ok to
3994 * @root - the root we're allocating for
3995 * @bytes - the number of bytes we want to reserve
3996 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003997 *
Josef Bacik663350a2011-11-03 22:54:25 -04003998 * This will check to make sure that committing the transaction will actually
3999 * get us somewhere and then commit the transaction if it does. Otherwise it
4000 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004001 */
Josef Bacik663350a2011-11-03 22:54:25 -04004002static int may_commit_transaction(struct btrfs_root *root,
4003 struct btrfs_space_info *space_info,
4004 u64 bytes, int force)
4005{
4006 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4007 struct btrfs_trans_handle *trans;
4008
4009 trans = (struct btrfs_trans_handle *)current->journal_info;
4010 if (trans)
4011 return -EAGAIN;
4012
4013 if (force)
4014 goto commit;
4015
4016 /* See if there is enough pinned space to make this reservation */
4017 spin_lock(&space_info->lock);
4018 if (space_info->bytes_pinned >= bytes) {
4019 spin_unlock(&space_info->lock);
4020 goto commit;
4021 }
4022 spin_unlock(&space_info->lock);
4023
4024 /*
4025 * See if there is some space in the delayed insertion reservation for
4026 * this reservation.
4027 */
4028 if (space_info != delayed_rsv->space_info)
4029 return -ENOSPC;
4030
Liu Bod9b02182012-02-16 18:34:39 +08004031 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004032 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004033 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04004034 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004035 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004036 return -ENOSPC;
4037 }
4038 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004039 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004040
4041commit:
4042 trans = btrfs_join_transaction(root);
4043 if (IS_ERR(trans))
4044 return -ENOSPC;
4045
4046 return btrfs_commit_transaction(trans, root);
4047}
4048
Josef Bacik96c3f432012-06-21 14:05:49 -04004049enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004050 FLUSH_DELAYED_ITEMS_NR = 1,
4051 FLUSH_DELAYED_ITEMS = 2,
4052 FLUSH_DELALLOC = 3,
4053 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004054 ALLOC_CHUNK = 5,
4055 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004056};
4057
4058static int flush_space(struct btrfs_root *root,
4059 struct btrfs_space_info *space_info, u64 num_bytes,
4060 u64 orig_bytes, int state)
4061{
4062 struct btrfs_trans_handle *trans;
4063 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004064 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004065
4066 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004067 case FLUSH_DELAYED_ITEMS_NR:
4068 case FLUSH_DELAYED_ITEMS:
4069 if (state == FLUSH_DELAYED_ITEMS_NR) {
4070 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
4071
4072 nr = (int)div64_u64(num_bytes, bytes);
4073 if (!nr)
4074 nr = 1;
4075 nr *= 2;
4076 } else {
4077 nr = -1;
4078 }
4079 trans = btrfs_join_transaction(root);
4080 if (IS_ERR(trans)) {
4081 ret = PTR_ERR(trans);
4082 break;
4083 }
4084 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4085 btrfs_end_transaction(trans, root);
4086 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004087 case FLUSH_DELALLOC:
4088 case FLUSH_DELALLOC_WAIT:
4089 shrink_delalloc(root, num_bytes, orig_bytes,
4090 state == FLUSH_DELALLOC_WAIT);
4091 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004092 case ALLOC_CHUNK:
4093 trans = btrfs_join_transaction(root);
4094 if (IS_ERR(trans)) {
4095 ret = PTR_ERR(trans);
4096 break;
4097 }
4098 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004099 btrfs_get_alloc_profile(root, 0),
4100 CHUNK_ALLOC_NO_FORCE);
4101 btrfs_end_transaction(trans, root);
4102 if (ret == -ENOSPC)
4103 ret = 0;
4104 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004105 case COMMIT_TRANS:
4106 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4107 break;
4108 default:
4109 ret = -ENOSPC;
4110 break;
4111 }
4112
4113 return ret;
4114}
Josef Bacik663350a2011-11-03 22:54:25 -04004115/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004116 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4117 * @root - the root we're allocating for
4118 * @block_rsv - the block_rsv we're allocating for
4119 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004120 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004121 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004122 * This will reserve orgi_bytes number of bytes from the space info associated
4123 * with the block_rsv. If there is not enough space it will make an attempt to
4124 * flush out space to make room. It will do this by flushing delalloc if
4125 * possible or committing the transaction. If flush is 0 then no attempts to
4126 * regain reservations will be made and this will fail if there is not enough
4127 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004128 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004129static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004130 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004131 u64 orig_bytes,
4132 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004133{
4134 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004135 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004136 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004137 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004138 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004139 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004140
4141again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004142 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004143 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004144 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004145 * We only want to wait if somebody other than us is flushing and we
4146 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004147 */
Miao Xie08e007d2012-10-16 11:33:38 +00004148 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4149 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004150 spin_unlock(&space_info->lock);
4151 /*
4152 * If we have a trans handle we can't wait because the flusher
4153 * may have to commit the transaction, which would mean we would
4154 * deadlock since we are waiting for the flusher to finish, but
4155 * hold the current transaction open.
4156 */
Josef Bacik663350a2011-11-03 22:54:25 -04004157 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004158 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004159 ret = wait_event_killable(space_info->wait, !space_info->flush);
4160 /* Must have been killed, return */
4161 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004162 return -EINTR;
4163
4164 spin_lock(&space_info->lock);
4165 }
4166
4167 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004168 used = space_info->bytes_used + space_info->bytes_reserved +
4169 space_info->bytes_pinned + space_info->bytes_readonly +
4170 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004171
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004172 /*
4173 * The idea here is that we've not already over-reserved the block group
4174 * then we can go ahead and save our reservation first and then start
4175 * flushing if we need to. Otherwise if we've already overcommitted
4176 * lets start flushing stuff first and then come back and try to make
4177 * our reservation.
4178 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004179 if (used <= space_info->total_bytes) {
4180 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004181 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004182 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004183 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004184 ret = 0;
4185 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004186 /*
4187 * Ok set num_bytes to orig_bytes since we aren't
4188 * overocmmitted, this way we only try and reclaim what
4189 * we need.
4190 */
4191 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004192 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004193 } else {
4194 /*
4195 * Ok we're over committed, set num_bytes to the overcommitted
4196 * amount plus the amount of bytes that we need for this
4197 * reservation.
4198 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004199 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004200 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004201 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004202
Josef Bacik44734ed2012-09-28 16:04:19 -04004203 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4204 space_info->bytes_may_use += orig_bytes;
4205 trace_btrfs_space_reservation(root->fs_info, "space_info",
4206 space_info->flags, orig_bytes,
4207 1);
4208 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004209 }
4210
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004211 /*
4212 * Couldn't make our reservation, save our place so while we're trying
4213 * to reclaim space we can actually use it instead of somebody else
4214 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004215 *
4216 * We make the other tasks wait for the flush only when we can flush
4217 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004218 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004219 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004220 flushing = true;
4221 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004222 }
4223
Yan, Zhengf0486c62010-05-16 10:46:25 -04004224 spin_unlock(&space_info->lock);
4225
Miao Xie08e007d2012-10-16 11:33:38 +00004226 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004227 goto out;
4228
Josef Bacik96c3f432012-06-21 14:05:49 -04004229 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4230 flush_state);
4231 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004232
4233 /*
4234 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4235 * would happen. So skip delalloc flush.
4236 */
4237 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4238 (flush_state == FLUSH_DELALLOC ||
4239 flush_state == FLUSH_DELALLOC_WAIT))
4240 flush_state = ALLOC_CHUNK;
4241
Josef Bacik96c3f432012-06-21 14:05:49 -04004242 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004243 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004244 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4245 flush_state < COMMIT_TRANS)
4246 goto again;
4247 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4248 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004249 goto again;
4250
4251out:
Josef Bacik5d803662013-02-07 16:06:02 -05004252 if (ret == -ENOSPC &&
4253 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4254 struct btrfs_block_rsv *global_rsv =
4255 &root->fs_info->global_block_rsv;
4256
4257 if (block_rsv != global_rsv &&
4258 !block_rsv_use_bytes(global_rsv, orig_bytes))
4259 ret = 0;
4260 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004261 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004262 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004263 space_info->flush = 0;
4264 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004265 spin_unlock(&space_info->lock);
4266 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004267 return ret;
4268}
4269
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004270static struct btrfs_block_rsv *get_block_rsv(
4271 const struct btrfs_trans_handle *trans,
4272 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004273{
Josef Bacik4c13d752011-08-30 11:31:29 -04004274 struct btrfs_block_rsv *block_rsv = NULL;
4275
Josef Bacik0e721102012-06-26 16:13:18 -04004276 if (root->ref_cows)
4277 block_rsv = trans->block_rsv;
4278
4279 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004280 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004281
4282 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004283 block_rsv = root->block_rsv;
4284
4285 if (!block_rsv)
4286 block_rsv = &root->fs_info->empty_block_rsv;
4287
4288 return block_rsv;
4289}
4290
4291static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4292 u64 num_bytes)
4293{
4294 int ret = -ENOSPC;
4295 spin_lock(&block_rsv->lock);
4296 if (block_rsv->reserved >= num_bytes) {
4297 block_rsv->reserved -= num_bytes;
4298 if (block_rsv->reserved < block_rsv->size)
4299 block_rsv->full = 0;
4300 ret = 0;
4301 }
4302 spin_unlock(&block_rsv->lock);
4303 return ret;
4304}
4305
4306static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4307 u64 num_bytes, int update_size)
4308{
4309 spin_lock(&block_rsv->lock);
4310 block_rsv->reserved += num_bytes;
4311 if (update_size)
4312 block_rsv->size += num_bytes;
4313 else if (block_rsv->reserved >= block_rsv->size)
4314 block_rsv->full = 1;
4315 spin_unlock(&block_rsv->lock);
4316}
4317
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004318static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4319 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004320 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004321{
4322 struct btrfs_space_info *space_info = block_rsv->space_info;
4323
4324 spin_lock(&block_rsv->lock);
4325 if (num_bytes == (u64)-1)
4326 num_bytes = block_rsv->size;
4327 block_rsv->size -= num_bytes;
4328 if (block_rsv->reserved >= block_rsv->size) {
4329 num_bytes = block_rsv->reserved - block_rsv->size;
4330 block_rsv->reserved = block_rsv->size;
4331 block_rsv->full = 1;
4332 } else {
4333 num_bytes = 0;
4334 }
4335 spin_unlock(&block_rsv->lock);
4336
4337 if (num_bytes > 0) {
4338 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004339 spin_lock(&dest->lock);
4340 if (!dest->full) {
4341 u64 bytes_to_add;
4342
4343 bytes_to_add = dest->size - dest->reserved;
4344 bytes_to_add = min(num_bytes, bytes_to_add);
4345 dest->reserved += bytes_to_add;
4346 if (dest->reserved >= dest->size)
4347 dest->full = 1;
4348 num_bytes -= bytes_to_add;
4349 }
4350 spin_unlock(&dest->lock);
4351 }
4352 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004353 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004354 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004355 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004356 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004357 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004358 spin_unlock(&space_info->lock);
4359 }
4360 }
4361}
4362
4363static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4364 struct btrfs_block_rsv *dst, u64 num_bytes)
4365{
4366 int ret;
4367
4368 ret = block_rsv_use_bytes(src, num_bytes);
4369 if (ret)
4370 return ret;
4371
4372 block_rsv_add_bytes(dst, num_bytes, 1);
4373 return 0;
4374}
4375
Miao Xie66d8f3d2012-09-06 04:02:28 -06004376void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004377{
4378 memset(rsv, 0, sizeof(*rsv));
4379 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004380 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004381}
4382
Miao Xie66d8f3d2012-09-06 04:02:28 -06004383struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4384 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004385{
4386 struct btrfs_block_rsv *block_rsv;
4387 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004388
4389 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4390 if (!block_rsv)
4391 return NULL;
4392
Miao Xie66d8f3d2012-09-06 04:02:28 -06004393 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004394 block_rsv->space_info = __find_space_info(fs_info,
4395 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004396 return block_rsv;
4397}
4398
4399void btrfs_free_block_rsv(struct btrfs_root *root,
4400 struct btrfs_block_rsv *rsv)
4401{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004402 if (!rsv)
4403 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004404 btrfs_block_rsv_release(root, rsv, (u64)-1);
4405 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004406}
4407
Miao Xie08e007d2012-10-16 11:33:38 +00004408int btrfs_block_rsv_add(struct btrfs_root *root,
4409 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4410 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004411{
4412 int ret;
4413
4414 if (num_bytes == 0)
4415 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004416
Miao Xie61b520a2011-11-10 20:45:05 -05004417 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004418 if (!ret) {
4419 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4420 return 0;
4421 }
4422
Yan, Zhengf0486c62010-05-16 10:46:25 -04004423 return ret;
4424}
4425
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004426int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004427 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004428{
4429 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004430 int ret = -ENOSPC;
4431
4432 if (!block_rsv)
4433 return 0;
4434
4435 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004436 num_bytes = div_factor(block_rsv->size, min_factor);
4437 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004438 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004439 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004440
Josef Bacik36ba0222011-10-18 12:15:48 -04004441 return ret;
4442}
4443
Miao Xie08e007d2012-10-16 11:33:38 +00004444int btrfs_block_rsv_refill(struct btrfs_root *root,
4445 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4446 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004447{
4448 u64 num_bytes = 0;
4449 int ret = -ENOSPC;
4450
4451 if (!block_rsv)
4452 return 0;
4453
4454 spin_lock(&block_rsv->lock);
4455 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004456 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004457 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004458 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004459 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004460 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004461
Yan, Zhengf0486c62010-05-16 10:46:25 -04004462 if (!ret)
4463 return 0;
4464
Miao Xieaa38a712011-11-18 17:43:00 +08004465 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004466 if (!ret) {
4467 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004468 return 0;
4469 }
4470
Josef Bacik13553e52011-08-08 13:33:21 -04004471 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004472}
4473
4474int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4475 struct btrfs_block_rsv *dst_rsv,
4476 u64 num_bytes)
4477{
4478 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4479}
4480
4481void btrfs_block_rsv_release(struct btrfs_root *root,
4482 struct btrfs_block_rsv *block_rsv,
4483 u64 num_bytes)
4484{
4485 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4486 if (global_rsv->full || global_rsv == block_rsv ||
4487 block_rsv->space_info != global_rsv->space_info)
4488 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004489 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4490 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004491}
4492
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004493/*
4494 * helper to calculate size of global block reservation.
4495 * the desired value is sum of space used by extent tree,
4496 * checksum tree and root tree
4497 */
4498static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4499{
4500 struct btrfs_space_info *sinfo;
4501 u64 num_bytes;
4502 u64 meta_used;
4503 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004504 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004505
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004506 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4507 spin_lock(&sinfo->lock);
4508 data_used = sinfo->bytes_used;
4509 spin_unlock(&sinfo->lock);
4510
4511 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4512 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004513 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4514 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004515 meta_used = sinfo->bytes_used;
4516 spin_unlock(&sinfo->lock);
4517
4518 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4519 csum_size * 2;
4520 num_bytes += div64_u64(data_used + meta_used, 50);
4521
4522 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004523 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004524
4525 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4526}
4527
4528static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4529{
4530 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4531 struct btrfs_space_info *sinfo = block_rsv->space_info;
4532 u64 num_bytes;
4533
4534 num_bytes = calc_global_metadata_size(fs_info);
4535
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004536 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004537 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004538
Josef Bacikfdf30d12013-03-26 15:31:45 -04004539 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004540
4541 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004542 sinfo->bytes_reserved + sinfo->bytes_readonly +
4543 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004544
4545 if (sinfo->total_bytes > num_bytes) {
4546 num_bytes = sinfo->total_bytes - num_bytes;
4547 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004548 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004549 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004550 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004551 }
4552
4553 if (block_rsv->reserved >= block_rsv->size) {
4554 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004555 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004556 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004557 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004558 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004559 block_rsv->reserved = block_rsv->size;
4560 block_rsv->full = 1;
4561 }
David Sterba182608c2011-05-05 13:13:16 +02004562
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004563 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004564 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004565}
4566
Yan, Zhengf0486c62010-05-16 10:46:25 -04004567static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4568{
4569 struct btrfs_space_info *space_info;
4570
4571 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4572 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004573
4574 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004575 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004576 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004577 fs_info->trans_block_rsv.space_info = space_info;
4578 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004579 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004580
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004581 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4582 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4583 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4584 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004585 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004586
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004587 update_global_block_rsv(fs_info);
4588}
4589
4590static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4591{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004592 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4593 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004594 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4595 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4596 WARN_ON(fs_info->trans_block_rsv.size > 0);
4597 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4598 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4599 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004600 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4601 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004602}
4603
Yan, Zhenga22285a2010-05-16 10:48:46 -04004604void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4605 struct btrfs_root *root)
4606{
Josef Bacik0e721102012-06-26 16:13:18 -04004607 if (!trans->block_rsv)
4608 return;
4609
Yan, Zhenga22285a2010-05-16 10:48:46 -04004610 if (!trans->bytes_reserved)
4611 return;
4612
Chris Masone77266e2012-02-24 10:39:05 -05004613 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004614 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004615 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004616 trans->bytes_reserved = 0;
4617}
4618
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004619/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004620int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4621 struct inode *inode)
4622{
4623 struct btrfs_root *root = BTRFS_I(inode)->root;
4624 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4625 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4626
4627 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004628 * We need to hold space in order to delete our orphan item once we've
4629 * added it, so this takes the reservation so we can release it later
4630 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004631 */
Chris Masonff5714c2011-05-28 07:00:39 -04004632 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004633 trace_btrfs_space_reservation(root->fs_info, "orphan",
4634 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004635 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4636}
4637
4638void btrfs_orphan_release_metadata(struct inode *inode)
4639{
4640 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004641 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004642 trace_btrfs_space_reservation(root->fs_info, "orphan",
4643 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004644 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4645}
4646
Miao Xied5c12072013-02-28 10:04:33 +00004647/*
4648 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4649 * root: the root of the parent directory
4650 * rsv: block reservation
4651 * items: the number of items that we need do reservation
4652 * qgroup_reserved: used to return the reserved size in qgroup
4653 *
4654 * This function is used to reserve the space for snapshot/subvolume
4655 * creation and deletion. Those operations are different with the
4656 * common file/directory operations, they change two fs/file trees
4657 * and root tree, the number of items that the qgroup reserves is
4658 * different with the free space reservation. So we can not use
4659 * the space reseravtion mechanism in start_transaction().
4660 */
4661int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4662 struct btrfs_block_rsv *rsv,
4663 int items,
4664 u64 *qgroup_reserved)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004665{
Miao Xied5c12072013-02-28 10:04:33 +00004666 u64 num_bytes;
4667 int ret;
4668
4669 if (root->fs_info->quota_enabled) {
4670 /* One for parent inode, two for dir entries */
4671 num_bytes = 3 * root->leafsize;
4672 ret = btrfs_qgroup_reserve(root, num_bytes);
4673 if (ret)
4674 return ret;
4675 } else {
4676 num_bytes = 0;
4677 }
4678
4679 *qgroup_reserved = num_bytes;
4680
4681 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4682 rsv->space_info = __find_space_info(root->fs_info,
4683 BTRFS_BLOCK_GROUP_METADATA);
4684 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4685 BTRFS_RESERVE_FLUSH_ALL);
4686 if (ret) {
4687 if (*qgroup_reserved)
4688 btrfs_qgroup_free(root, *qgroup_reserved);
4689 }
4690
4691 return ret;
4692}
4693
4694void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4695 struct btrfs_block_rsv *rsv,
4696 u64 qgroup_reserved)
4697{
4698 btrfs_block_rsv_release(root, rsv, (u64)-1);
4699 if (qgroup_reserved)
4700 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004701}
4702
Josef Bacik7709cde2011-08-04 10:25:02 -04004703/**
4704 * drop_outstanding_extent - drop an outstanding extent
4705 * @inode: the inode we're dropping the extent for
4706 *
4707 * This is called when we are freeing up an outstanding extent, either called
4708 * after an error or after an extent is written. This will return the number of
4709 * reserved extents that need to be freed. This must be called with
4710 * BTRFS_I(inode)->lock held.
4711 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004712static unsigned drop_outstanding_extent(struct inode *inode)
4713{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004714 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004715 unsigned dropped_extents = 0;
4716
Josef Bacik9e0baf62011-07-15 15:16:44 +00004717 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4718 BTRFS_I(inode)->outstanding_extents--;
4719
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004720 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004721 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4722 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004723 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004724
Josef Bacik9e0baf62011-07-15 15:16:44 +00004725 /*
4726 * If we have more or the same amount of outsanding extents than we have
4727 * reserved then we need to leave the reserved extents count alone.
4728 */
4729 if (BTRFS_I(inode)->outstanding_extents >=
4730 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004731 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004732
4733 dropped_extents = BTRFS_I(inode)->reserved_extents -
4734 BTRFS_I(inode)->outstanding_extents;
4735 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004736 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004737}
4738
Josef Bacik7709cde2011-08-04 10:25:02 -04004739/**
4740 * calc_csum_metadata_size - return the amount of metada space that must be
4741 * reserved/free'd for the given bytes.
4742 * @inode: the inode we're manipulating
4743 * @num_bytes: the number of bytes in question
4744 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4745 *
4746 * This adjusts the number of csum_bytes in the inode and then returns the
4747 * correct amount of metadata that must either be reserved or freed. We
4748 * calculate how many checksums we can fit into one leaf and then divide the
4749 * number of bytes that will need to be checksumed by this value to figure out
4750 * how many checksums will be required. If we are adding bytes then the number
4751 * may go up and we will return the number of additional bytes that must be
4752 * reserved. If it is going down we will return the number of bytes that must
4753 * be freed.
4754 *
4755 * This must be called with BTRFS_I(inode)->lock held.
4756 */
4757static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4758 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004759{
Josef Bacik7709cde2011-08-04 10:25:02 -04004760 struct btrfs_root *root = BTRFS_I(inode)->root;
4761 u64 csum_size;
4762 int num_csums_per_leaf;
4763 int num_csums;
4764 int old_csums;
4765
4766 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4767 BTRFS_I(inode)->csum_bytes == 0)
4768 return 0;
4769
4770 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4771 if (reserve)
4772 BTRFS_I(inode)->csum_bytes += num_bytes;
4773 else
4774 BTRFS_I(inode)->csum_bytes -= num_bytes;
4775 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4776 num_csums_per_leaf = (int)div64_u64(csum_size,
4777 sizeof(struct btrfs_csum_item) +
4778 sizeof(struct btrfs_disk_key));
4779 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4780 num_csums = num_csums + num_csums_per_leaf - 1;
4781 num_csums = num_csums / num_csums_per_leaf;
4782
4783 old_csums = old_csums + num_csums_per_leaf - 1;
4784 old_csums = old_csums / num_csums_per_leaf;
4785
4786 /* No change, no need to reserve more */
4787 if (old_csums == num_csums)
4788 return 0;
4789
4790 if (reserve)
4791 return btrfs_calc_trans_metadata_size(root,
4792 num_csums - old_csums);
4793
4794 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004795}
4796
4797int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4798{
4799 struct btrfs_root *root = BTRFS_I(inode)->root;
4800 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004801 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004802 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004803 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004804 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004805 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004806 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004807 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004808 u64 to_free = 0;
4809 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004810
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004811 /* If we are a free space inode we need to not flush since we will be in
4812 * the middle of a transaction commit. We also don't need the delalloc
4813 * mutex since we won't race with anybody. We need this mostly to make
4814 * lockdep shut its filthy mouth.
4815 */
4816 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004817 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004818 delalloc_lock = false;
4819 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004820
Miao Xie08e007d2012-10-16 11:33:38 +00004821 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4822 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004823 schedule_timeout(1);
4824
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004825 if (delalloc_lock)
4826 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4827
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004828 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004829
Josef Bacik9e0baf62011-07-15 15:16:44 +00004830 spin_lock(&BTRFS_I(inode)->lock);
4831 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004832
Josef Bacik9e0baf62011-07-15 15:16:44 +00004833 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004834 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004835 nr_extents = BTRFS_I(inode)->outstanding_extents -
4836 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004837
4838 /*
4839 * Add an item to reserve for updating the inode when we complete the
4840 * delalloc io.
4841 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004842 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4843 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004844 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004845 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004846 }
4847
4848 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004849 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004850 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004851 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004852
Wang Shilong88e081bf2013-03-01 11:36:01 +00004853 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02004854 ret = btrfs_qgroup_reserve(root, num_bytes +
4855 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004856 if (ret)
4857 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00004858 }
Arne Jansenc5567232011-09-14 15:44:05 +02004859
Wang Shilong88e081bf2013-03-01 11:36:01 +00004860 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4861 if (unlikely(ret)) {
4862 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00004863 btrfs_qgroup_free(root, num_bytes +
4864 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004865 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004866 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004867
Josef Bacik660d3f62011-12-09 11:18:51 -05004868 spin_lock(&BTRFS_I(inode)->lock);
4869 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004870 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4871 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004872 nr_extents--;
4873 }
4874 BTRFS_I(inode)->reserved_extents += nr_extents;
4875 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004876
4877 if (delalloc_lock)
4878 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004879
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004880 if (to_reserve)
4881 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4882 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004883 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4884
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004885 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004886
4887out_fail:
4888 spin_lock(&BTRFS_I(inode)->lock);
4889 dropped = drop_outstanding_extent(inode);
4890 /*
4891 * If the inodes csum_bytes is the same as the original
4892 * csum_bytes then we know we haven't raced with any free()ers
4893 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00004894 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04004895 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00004896 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04004897 } else {
4898 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
4899 u64 bytes;
4900
4901 /*
4902 * This is tricky, but first we need to figure out how much we
4903 * free'd from any free-ers that occured during this
4904 * reservation, so we reset ->csum_bytes to the csum_bytes
4905 * before we dropped our lock, and then call the free for the
4906 * number of bytes that were freed while we were trying our
4907 * reservation.
4908 */
4909 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
4910 BTRFS_I(inode)->csum_bytes = csum_bytes;
4911 to_free = calc_csum_metadata_size(inode, bytes, 0);
4912
4913
4914 /*
4915 * Now we need to see how much we would have freed had we not
4916 * been making this reservation and our ->csum_bytes were not
4917 * artificially inflated.
4918 */
4919 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
4920 bytes = csum_bytes - orig_csum_bytes;
4921 bytes = calc_csum_metadata_size(inode, bytes, 0);
4922
4923 /*
4924 * Now reset ->csum_bytes to what it should be. If bytes is
4925 * more than to_free then we would have free'd more space had we
4926 * not had an artificially high ->csum_bytes, so we need to free
4927 * the remainder. If bytes is the same or less then we don't
4928 * need to do anything, the other free-ers did the correct
4929 * thing.
4930 */
4931 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
4932 if (bytes > to_free)
4933 to_free = bytes - to_free;
4934 else
4935 to_free = 0;
4936 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00004937 spin_unlock(&BTRFS_I(inode)->lock);
4938 if (dropped)
4939 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4940
4941 if (to_free) {
4942 btrfs_block_rsv_release(root, block_rsv, to_free);
4943 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4944 btrfs_ino(inode), to_free, 0);
4945 }
4946 if (delalloc_lock)
4947 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4948 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004949}
4950
Josef Bacik7709cde2011-08-04 10:25:02 -04004951/**
4952 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4953 * @inode: the inode to release the reservation for
4954 * @num_bytes: the number of bytes we're releasing
4955 *
4956 * This will release the metadata reservation for an inode. This can be called
4957 * once we complete IO for a given set of bytes to release their metadata
4958 * reservations.
4959 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004960void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4961{
4962 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004963 u64 to_free = 0;
4964 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004965
4966 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004967 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004968 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004969
Miao Xie09348562013-02-07 10:12:07 +00004970 if (num_bytes)
4971 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04004972 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004973 if (dropped > 0)
4974 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004975
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004976 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4977 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004978 if (root->fs_info->quota_enabled) {
4979 btrfs_qgroup_free(root, num_bytes +
4980 dropped * root->leafsize);
4981 }
4982
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004983 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4984 to_free);
4985}
4986
Josef Bacik7709cde2011-08-04 10:25:02 -04004987/**
4988 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4989 * @inode: inode we're writing to
4990 * @num_bytes: the number of bytes we want to allocate
4991 *
4992 * This will do the following things
4993 *
4994 * o reserve space in the data space info for num_bytes
4995 * o reserve space in the metadata space info based on number of outstanding
4996 * extents and how much csums will be needed
4997 * o add to the inodes ->delalloc_bytes
4998 * o add it to the fs_info's delalloc inodes list.
4999 *
5000 * This will return 0 for success and -ENOSPC if there is no space left.
5001 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005002int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5003{
5004 int ret;
5005
5006 ret = btrfs_check_data_free_space(inode, num_bytes);
5007 if (ret)
5008 return ret;
5009
5010 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5011 if (ret) {
5012 btrfs_free_reserved_data_space(inode, num_bytes);
5013 return ret;
5014 }
5015
5016 return 0;
5017}
5018
Josef Bacik7709cde2011-08-04 10:25:02 -04005019/**
5020 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5021 * @inode: inode we're releasing space for
5022 * @num_bytes: the number of bytes we want to free up
5023 *
5024 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5025 * called in the case that we don't need the metadata AND data reservations
5026 * anymore. So if there is an error or we insert an inline extent.
5027 *
5028 * This function will release the metadata space that was not used and will
5029 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5030 * list if there are no delalloc bytes left.
5031 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005032void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5033{
5034 btrfs_delalloc_release_metadata(inode, num_bytes);
5035 btrfs_free_reserved_data_space(inode, num_bytes);
5036}
5037
Liu Boc53d6132012-12-27 09:01:19 +00005038static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005039 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005040{
Josef Bacik0af3d002010-06-21 14:48:16 -04005041 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005042 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005043 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005044 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005045 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005046 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005047
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005048 /* block accounting for super block */
5049 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02005050 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005051 if (alloc)
5052 old_val += num_bytes;
5053 else
5054 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005055 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005056 spin_unlock(&info->delalloc_lock);
5057
Chris Masond3977122009-01-05 21:25:51 -05005058 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005059 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005060 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005061 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005062 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5063 BTRFS_BLOCK_GROUP_RAID1 |
5064 BTRFS_BLOCK_GROUP_RAID10))
5065 factor = 2;
5066 else
5067 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005068 /*
5069 * If this block group has free space cache written out, we
5070 * need to make sure to load it if we are removing space. This
5071 * is because we need the unpinning stage to actually add the
5072 * space back to the block group, otherwise we will leak space.
5073 */
5074 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005075 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005076
Chris Masondb945352007-10-15 16:15:53 -04005077 byte_in_group = bytenr - cache->key.objectid;
5078 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005079
Josef Bacik25179202008-10-29 14:49:05 -04005080 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005081 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005082
Josef Bacik73bc1872011-10-03 14:07:49 -04005083 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005084 cache->disk_cache_state < BTRFS_DC_CLEAR)
5085 cache->disk_cache_state = BTRFS_DC_CLEAR;
5086
Josef Bacik0f9dd462008-09-23 13:14:11 -04005087 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005088 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005089 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005090 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005091 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005092 btrfs_set_block_group_used(&cache->item, old_val);
5093 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005094 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005095 cache->space_info->bytes_used += num_bytes;
5096 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005097 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005098 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005099 } else {
Chris Masondb945352007-10-15 16:15:53 -04005100 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005101 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005102 cache->pinned += num_bytes;
5103 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005104 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005105 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005106 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005107 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005108
Yan, Zhengf0486c62010-05-16 10:46:25 -04005109 set_extent_dirty(info->pinned_extents,
5110 bytenr, bytenr + num_bytes - 1,
5111 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005112 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005113 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005114 total -= num_bytes;
5115 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005116 }
5117 return 0;
5118}
Chris Mason6324fbf2008-03-24 15:01:59 -04005119
Chris Masona061fc82008-05-07 11:43:44 -04005120static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5121{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005122 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005123 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005124
Liu Boa1897fd2012-12-27 09:01:23 +00005125 spin_lock(&root->fs_info->block_group_cache_lock);
5126 bytenr = root->fs_info->first_logical_byte;
5127 spin_unlock(&root->fs_info->block_group_cache_lock);
5128
5129 if (bytenr < (u64)-1)
5130 return bytenr;
5131
Josef Bacik0f9dd462008-09-23 13:14:11 -04005132 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5133 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005134 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005135
Yan Zhengd2fb3432008-12-11 16:30:39 -05005136 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005137 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005138
5139 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005140}
5141
Yan, Zhengf0486c62010-05-16 10:46:25 -04005142static int pin_down_extent(struct btrfs_root *root,
5143 struct btrfs_block_group_cache *cache,
5144 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005145{
Yan Zheng11833d62009-09-11 16:11:19 -04005146 spin_lock(&cache->space_info->lock);
5147 spin_lock(&cache->lock);
5148 cache->pinned += num_bytes;
5149 cache->space_info->bytes_pinned += num_bytes;
5150 if (reserved) {
5151 cache->reserved -= num_bytes;
5152 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005153 }
Yan Zheng11833d62009-09-11 16:11:19 -04005154 spin_unlock(&cache->lock);
5155 spin_unlock(&cache->space_info->lock);
5156
Yan, Zhengf0486c62010-05-16 10:46:25 -04005157 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5158 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005159 return 0;
5160}
Chris Mason9078a3e2007-04-26 16:46:15 -04005161
Yan, Zhengf0486c62010-05-16 10:46:25 -04005162/*
5163 * this function must be called within transaction
5164 */
5165int btrfs_pin_extent(struct btrfs_root *root,
5166 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005167{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005168 struct btrfs_block_group_cache *cache;
5169
5170 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005171 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005172
5173 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5174
5175 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005176 return 0;
5177}
Zheng Yane8569812008-09-26 10:05:48 -04005178
Yan, Zhengf0486c62010-05-16 10:46:25 -04005179/*
Chris Masone688b7252011-10-31 20:52:39 -04005180 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005181 */
Liu Bodcfac412012-12-27 09:01:20 +00005182int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005183 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005184{
Chris Masone688b7252011-10-31 20:52:39 -04005185 struct btrfs_block_group_cache *cache;
5186
5187 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005188 BUG_ON(!cache); /* Logic error */
Chris Masone688b7252011-10-31 20:52:39 -04005189
5190 /*
5191 * pull in the free space cache (if any) so that our pin
5192 * removes the free space from the cache. We have load_only set
5193 * to one because the slow code to read in the free extents does check
5194 * the pinned extents.
5195 */
Liu Bof6373bf2012-12-27 09:01:18 +00005196 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005197
5198 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5199
5200 /* remove us from the free space cache (if we're there at all) */
5201 btrfs_remove_free_space(cache, bytenr, num_bytes);
5202 btrfs_put_block_group(cache);
5203 return 0;
5204}
5205
Josef Bacikfb25e912011-07-26 17:00:46 -04005206/**
5207 * btrfs_update_reserved_bytes - update the block_group and space info counters
5208 * @cache: The cache we are manipulating
5209 * @num_bytes: The number of bytes in question
5210 * @reserve: One of the reservation enums
5211 *
5212 * This is called by the allocator when it reserves space, or by somebody who is
5213 * freeing space that was never actually used on disk. For example if you
5214 * reserve some space for a new leaf in transaction A and before transaction A
5215 * commits you free that leaf, you call this with reserve set to 0 in order to
5216 * clear the reservation.
5217 *
5218 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5219 * ENOSPC accounting. For data we handle the reservation through clearing the
5220 * delalloc bits in the io_tree. We have to do this since we could end up
5221 * allocating less disk space for the amount of data we have reserved in the
5222 * case of compression.
5223 *
5224 * If this is a reservation and the block group has become read only we cannot
5225 * make the reservation and return -EAGAIN, otherwise this function always
5226 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005227 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005228static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5229 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005230{
Josef Bacikfb25e912011-07-26 17:00:46 -04005231 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005232 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005233
Josef Bacikfb25e912011-07-26 17:00:46 -04005234 spin_lock(&space_info->lock);
5235 spin_lock(&cache->lock);
5236 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005237 if (cache->ro) {
5238 ret = -EAGAIN;
5239 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005240 cache->reserved += num_bytes;
5241 space_info->bytes_reserved += num_bytes;
5242 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005243 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005244 "space_info", space_info->flags,
5245 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005246 space_info->bytes_may_use -= num_bytes;
5247 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005248 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005249 } else {
5250 if (cache->ro)
5251 space_info->bytes_readonly += num_bytes;
5252 cache->reserved -= num_bytes;
5253 space_info->bytes_reserved -= num_bytes;
5254 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005255 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005256 spin_unlock(&cache->lock);
5257 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005258 return ret;
5259}
5260
Jeff Mahoney143bede2012-03-01 14:56:26 +01005261void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005262 struct btrfs_root *root)
5263{
5264 struct btrfs_fs_info *fs_info = root->fs_info;
5265 struct btrfs_caching_control *next;
5266 struct btrfs_caching_control *caching_ctl;
5267 struct btrfs_block_group_cache *cache;
5268
5269 down_write(&fs_info->extent_commit_sem);
5270
5271 list_for_each_entry_safe(caching_ctl, next,
5272 &fs_info->caching_block_groups, list) {
5273 cache = caching_ctl->block_group;
5274 if (block_group_cache_done(cache)) {
5275 cache->last_byte_to_unpin = (u64)-1;
5276 list_del_init(&caching_ctl->list);
5277 put_caching_control(caching_ctl);
5278 } else {
5279 cache->last_byte_to_unpin = caching_ctl->progress;
5280 }
5281 }
5282
5283 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5284 fs_info->pinned_extents = &fs_info->freed_extents[1];
5285 else
5286 fs_info->pinned_extents = &fs_info->freed_extents[0];
5287
5288 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005289
5290 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005291}
5292
5293static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5294{
5295 struct btrfs_fs_info *fs_info = root->fs_info;
5296 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005297 struct btrfs_space_info *space_info;
5298 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005299 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005300 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005301
5302 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005303 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005304 if (!cache ||
5305 start >= cache->key.objectid + cache->key.offset) {
5306 if (cache)
5307 btrfs_put_block_group(cache);
5308 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005309 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005310 }
5311
5312 len = cache->key.objectid + cache->key.offset - start;
5313 len = min(len, end + 1 - start);
5314
5315 if (start < cache->last_byte_to_unpin) {
5316 len = min(len, cache->last_byte_to_unpin - start);
5317 btrfs_add_free_space(cache, start, len);
5318 }
Josef Bacik25179202008-10-29 14:49:05 -04005319
Yan, Zhengf0486c62010-05-16 10:46:25 -04005320 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005321 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005322
Josef Bacik7b398f82012-10-22 15:52:28 -04005323 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005324 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005325 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005326 space_info->bytes_pinned -= len;
5327 if (cache->ro) {
5328 space_info->bytes_readonly += len;
5329 readonly = true;
5330 }
Josef Bacik25179202008-10-29 14:49:05 -04005331 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005332 if (!readonly && global_rsv->space_info == space_info) {
5333 spin_lock(&global_rsv->lock);
5334 if (!global_rsv->full) {
5335 len = min(len, global_rsv->size -
5336 global_rsv->reserved);
5337 global_rsv->reserved += len;
5338 space_info->bytes_may_use += len;
5339 if (global_rsv->reserved >= global_rsv->size)
5340 global_rsv->full = 1;
5341 }
5342 spin_unlock(&global_rsv->lock);
5343 }
5344 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005345 }
5346
5347 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005348 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005349 return 0;
5350}
5351
5352int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005353 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005354{
Yan Zheng11833d62009-09-11 16:11:19 -04005355 struct btrfs_fs_info *fs_info = root->fs_info;
5356 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005357 u64 start;
5358 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005359 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005360
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005361 if (trans->aborted)
5362 return 0;
5363
Yan Zheng11833d62009-09-11 16:11:19 -04005364 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5365 unpin = &fs_info->freed_extents[1];
5366 else
5367 unpin = &fs_info->freed_extents[0];
5368
Chris Masond3977122009-01-05 21:25:51 -05005369 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005370 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005371 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005372 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005373 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005374
Li Dongyang5378e602011-03-24 10:24:27 +00005375 if (btrfs_test_opt(root, DISCARD))
5376 ret = btrfs_discard_extent(root, start,
5377 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005378
Chris Mason1a5bc162007-10-15 16:15:26 -04005379 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005380 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005381 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005382 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005383
Chris Masone20d96d2007-03-22 12:13:20 -04005384 return 0;
5385}
5386
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005387static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5388 struct btrfs_root *root,
5389 u64 bytenr, u64 num_bytes, u64 parent,
5390 u64 root_objectid, u64 owner_objectid,
5391 u64 owner_offset, int refs_to_drop,
5392 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005393{
Chris Masone2fa7222007-03-12 16:22:34 -04005394 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005395 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005396 struct btrfs_fs_info *info = root->fs_info;
5397 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005398 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005399 struct btrfs_extent_item *ei;
5400 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005401 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005402 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005403 int extent_slot = 0;
5404 int found_extent = 0;
5405 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005406 u32 item_size;
5407 u64 refs;
Josef Bacik3173a182013-03-07 14:22:04 -05005408 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5409 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005410
Chris Mason5caf2a02007-04-02 11:20:42 -04005411 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005412 if (!path)
5413 return -ENOMEM;
5414
Chris Mason3c12ac72008-04-21 12:01:38 -04005415 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005416 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005417
5418 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5419 BUG_ON(!is_data && refs_to_drop != 1);
5420
Josef Bacik3173a182013-03-07 14:22:04 -05005421 if (is_data)
5422 skinny_metadata = 0;
5423
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005424 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5425 bytenr, num_bytes, parent,
5426 root_objectid, owner_objectid,
5427 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005428 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005429 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005430 while (extent_slot >= 0) {
5431 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005432 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005433 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005434 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005435 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5436 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005437 found_extent = 1;
5438 break;
5439 }
Josef Bacik3173a182013-03-07 14:22:04 -05005440 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5441 key.offset == owner_objectid) {
5442 found_extent = 1;
5443 break;
5444 }
Chris Mason952fcca2008-02-18 16:33:44 -05005445 if (path->slots[0] - extent_slot > 5)
5446 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005447 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005448 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005449#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5450 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5451 if (found_extent && item_size < sizeof(*ei))
5452 found_extent = 0;
5453#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005454 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005455 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005456 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005457 NULL, refs_to_drop,
5458 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005459 if (ret) {
5460 btrfs_abort_transaction(trans, extent_root, ret);
5461 goto out;
5462 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005463 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005464 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005465
5466 key.objectid = bytenr;
5467 key.type = BTRFS_EXTENT_ITEM_KEY;
5468 key.offset = num_bytes;
5469
Josef Bacik3173a182013-03-07 14:22:04 -05005470 if (!is_data && skinny_metadata) {
5471 key.type = BTRFS_METADATA_ITEM_KEY;
5472 key.offset = owner_objectid;
5473 }
5474
Zheng Yan31840ae2008-09-23 13:14:14 -04005475 ret = btrfs_search_slot(trans, extent_root,
5476 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005477 if (ret > 0 && skinny_metadata && path->slots[0]) {
5478 /*
5479 * Couldn't find our skinny metadata item,
5480 * see if we have ye olde extent item.
5481 */
5482 path->slots[0]--;
5483 btrfs_item_key_to_cpu(path->nodes[0], &key,
5484 path->slots[0]);
5485 if (key.objectid == bytenr &&
5486 key.type == BTRFS_EXTENT_ITEM_KEY &&
5487 key.offset == num_bytes)
5488 ret = 0;
5489 }
5490
5491 if (ret > 0 && skinny_metadata) {
5492 skinny_metadata = false;
5493 key.type = BTRFS_EXTENT_ITEM_KEY;
5494 key.offset = num_bytes;
5495 btrfs_release_path(path);
5496 ret = btrfs_search_slot(trans, extent_root,
5497 &key, path, -1, 1);
5498 }
5499
Josef Bacikf3465ca2008-11-12 14:19:50 -05005500 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005501 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5502 ret, (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005503 if (ret > 0)
5504 btrfs_print_leaf(extent_root,
5505 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005506 }
David Sterba005d6422012-09-18 07:52:32 -06005507 if (ret < 0) {
5508 btrfs_abort_transaction(trans, extent_root, ret);
5509 goto out;
5510 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005511 extent_slot = path->slots[0];
5512 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005513 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005514 btrfs_print_leaf(extent_root, path->nodes[0]);
5515 WARN_ON(1);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005516 btrfs_err(info,
5517 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5518 (unsigned long long)bytenr,
5519 (unsigned long long)parent,
5520 (unsigned long long)root_objectid,
5521 (unsigned long long)owner_objectid,
5522 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005523 } else {
David Sterba005d6422012-09-18 07:52:32 -06005524 btrfs_abort_transaction(trans, extent_root, ret);
5525 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005526 }
Chris Mason5f39d392007-10-15 16:14:19 -04005527
5528 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005529 item_size = btrfs_item_size_nr(leaf, extent_slot);
5530#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5531 if (item_size < sizeof(*ei)) {
5532 BUG_ON(found_extent || extent_slot != path->slots[0]);
5533 ret = convert_extent_item_v0(trans, extent_root, path,
5534 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005535 if (ret < 0) {
5536 btrfs_abort_transaction(trans, extent_root, ret);
5537 goto out;
5538 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005539
David Sterbab3b4aa72011-04-21 01:20:15 +02005540 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 path->leave_spinning = 1;
5542
5543 key.objectid = bytenr;
5544 key.type = BTRFS_EXTENT_ITEM_KEY;
5545 key.offset = num_bytes;
5546
5547 ret = btrfs_search_slot(trans, extent_root, &key, path,
5548 -1, 1);
5549 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005550 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5551 ret, (unsigned long long)bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005552 btrfs_print_leaf(extent_root, path->nodes[0]);
5553 }
David Sterba005d6422012-09-18 07:52:32 -06005554 if (ret < 0) {
5555 btrfs_abort_transaction(trans, extent_root, ret);
5556 goto out;
5557 }
5558
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 extent_slot = path->slots[0];
5560 leaf = path->nodes[0];
5561 item_size = btrfs_item_size_nr(leaf, extent_slot);
5562 }
5563#endif
5564 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005565 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005566 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005567 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5568 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569 struct btrfs_tree_block_info *bi;
5570 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5571 bi = (struct btrfs_tree_block_info *)(ei + 1);
5572 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005573 }
5574
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 refs = btrfs_extent_refs(leaf, ei);
5576 BUG_ON(refs < refs_to_drop);
5577 refs -= refs_to_drop;
5578
5579 if (refs > 0) {
5580 if (extent_op)
5581 __run_delayed_extent_op(extent_op, leaf, ei);
5582 /*
5583 * In the case of inline back ref, reference count will
5584 * be updated by remove_extent_backref
5585 */
5586 if (iref) {
5587 BUG_ON(!found_extent);
5588 } else {
5589 btrfs_set_extent_refs(leaf, ei, refs);
5590 btrfs_mark_buffer_dirty(leaf);
5591 }
5592 if (found_extent) {
5593 ret = remove_extent_backref(trans, extent_root, path,
5594 iref, refs_to_drop,
5595 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005596 if (ret) {
5597 btrfs_abort_transaction(trans, extent_root, ret);
5598 goto out;
5599 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 }
5601 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602 if (found_extent) {
5603 BUG_ON(is_data && refs_to_drop !=
5604 extent_data_ref_count(root, path, iref));
5605 if (iref) {
5606 BUG_ON(path->slots[0] != extent_slot);
5607 } else {
5608 BUG_ON(path->slots[0] != extent_slot + 1);
5609 path->slots[0] = extent_slot;
5610 num_to_del = 2;
5611 }
Chris Mason78fae272007-03-25 11:35:08 -04005612 }
Chris Masonb9473432009-03-13 11:00:37 -04005613
Chris Mason952fcca2008-02-18 16:33:44 -05005614 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5615 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005616 if (ret) {
5617 btrfs_abort_transaction(trans, extent_root, ret);
5618 goto out;
5619 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005620 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005621
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005622 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005623 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005624 if (ret) {
5625 btrfs_abort_transaction(trans, extent_root, ret);
5626 goto out;
5627 }
Chris Mason459931e2008-12-10 09:10:46 -05005628 }
5629
Liu Boc53d6132012-12-27 09:01:19 +00005630 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005631 if (ret) {
5632 btrfs_abort_transaction(trans, extent_root, ret);
5633 goto out;
5634 }
Chris Masona28ec192007-03-06 20:08:01 -05005635 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005636out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005637 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005638 return ret;
5639}
5640
5641/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005642 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005643 * delayed ref for that extent as well. This searches the delayed ref tree for
5644 * a given extent, and if there are no other delayed refs to be processed, it
5645 * removes it from the tree.
5646 */
5647static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5648 struct btrfs_root *root, u64 bytenr)
5649{
5650 struct btrfs_delayed_ref_head *head;
5651 struct btrfs_delayed_ref_root *delayed_refs;
5652 struct btrfs_delayed_ref_node *ref;
5653 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005654 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005655
5656 delayed_refs = &trans->transaction->delayed_refs;
5657 spin_lock(&delayed_refs->lock);
5658 head = btrfs_find_delayed_ref_head(trans, bytenr);
5659 if (!head)
5660 goto out;
5661
5662 node = rb_prev(&head->node.rb_node);
5663 if (!node)
5664 goto out;
5665
5666 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5667
5668 /* there are still entries for this ref, we can't drop it */
5669 if (ref->bytenr == bytenr)
5670 goto out;
5671
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005672 if (head->extent_op) {
5673 if (!head->must_insert_reserved)
5674 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005675 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005676 head->extent_op = NULL;
5677 }
5678
Chris Mason1887be62009-03-13 10:11:24 -04005679 /*
5680 * waiting for the lock here would deadlock. If someone else has it
5681 * locked they are already in the process of dropping it anyway
5682 */
5683 if (!mutex_trylock(&head->mutex))
5684 goto out;
5685
5686 /*
5687 * at this point we have a head with no other entries. Go
5688 * ahead and process it.
5689 */
5690 head->node.in_tree = 0;
5691 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005692
Chris Mason1887be62009-03-13 10:11:24 -04005693 delayed_refs->num_entries--;
5694
5695 /*
5696 * we don't take a ref on the node because we're removing it from the
5697 * tree, so we just steal the ref the tree was holding.
5698 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005699 delayed_refs->num_heads--;
5700 if (list_empty(&head->cluster))
5701 delayed_refs->num_heads_ready--;
5702
5703 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005704 spin_unlock(&delayed_refs->lock);
5705
Yan, Zhengf0486c62010-05-16 10:46:25 -04005706 BUG_ON(head->extent_op);
5707 if (head->must_insert_reserved)
5708 ret = 1;
5709
5710 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005711 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005712 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005713out:
5714 spin_unlock(&delayed_refs->lock);
5715 return 0;
5716}
5717
Yan, Zhengf0486c62010-05-16 10:46:25 -04005718void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5719 struct btrfs_root *root,
5720 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005721 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005722{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005723 struct btrfs_block_group_cache *cache = NULL;
5724 int ret;
5725
5726 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005727 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5728 buf->start, buf->len,
5729 parent, root->root_key.objectid,
5730 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005731 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005732 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005733 }
5734
5735 if (!last_ref)
5736 return;
5737
Yan, Zhengf0486c62010-05-16 10:46:25 -04005738 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005739
5740 if (btrfs_header_generation(buf) == trans->transid) {
5741 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5742 ret = check_ref_cleanup(trans, root, buf->start);
5743 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005744 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005745 }
5746
5747 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5748 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005749 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005750 }
5751
5752 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5753
5754 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005755 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005756 }
5757out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005758 /*
5759 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5760 * anymore.
5761 */
5762 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005763 btrfs_put_block_group(cache);
5764}
5765
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005766/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005767int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5768 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5769 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005770{
5771 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005772 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005773
Chris Mason56bec292009-03-13 10:10:06 -04005774 /*
5775 * tree log blocks never actually go into the extent allocation
5776 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005777 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005778 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5779 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005780 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005781 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005782 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005783 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005784 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5785 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005786 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005787 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005788 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005789 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5790 num_bytes,
5791 parent, root_objectid, owner,
5792 offset, BTRFS_DROP_DELAYED_REF,
5793 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005794 }
Chris Mason925baed2008-06-25 16:01:30 -04005795 return ret;
5796}
5797
David Woodhouse53b381b2013-01-29 18:40:14 -05005798static u64 stripe_align(struct btrfs_root *root,
5799 struct btrfs_block_group_cache *cache,
5800 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05005801{
Qu Wenruofda28322013-02-26 08:10:22 +00005802 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05005803 return ret;
5804}
5805
Chris Masonfec577f2007-02-26 10:40:21 -05005806/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005807 * when we wait for progress in the block group caching, its because
5808 * our allocation attempt failed at least once. So, we must sleep
5809 * and let some progress happen before we try again.
5810 *
5811 * This function will sleep at least once waiting for new free space to
5812 * show up, and then it will check the block group free space numbers
5813 * for our min num_bytes. Another option is to have it go ahead
5814 * and look in the rbtree for a free extent of a given size, but this
5815 * is a good start.
5816 */
5817static noinline int
5818wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5819 u64 num_bytes)
5820{
Yan Zheng11833d62009-09-11 16:11:19 -04005821 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005822
Yan Zheng11833d62009-09-11 16:11:19 -04005823 caching_ctl = get_caching_control(cache);
5824 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005825 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005826
Yan Zheng11833d62009-09-11 16:11:19 -04005827 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005828 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005829
5830 put_caching_control(caching_ctl);
5831 return 0;
5832}
5833
5834static noinline int
5835wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5836{
5837 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04005838
5839 caching_ctl = get_caching_control(cache);
5840 if (!caching_ctl)
5841 return 0;
5842
5843 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5844
5845 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005846 return 0;
5847}
5848
Liu Bo31e50222012-11-21 14:18:10 +00005849int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005850{
Ilya Dryomov7738a532012-03-27 17:09:17 +03005851 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00005852 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005853 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00005854 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005855 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00005856 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005857 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00005858 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005859 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05005860 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05005861 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05005862 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005863
Chris Masone942f882013-02-20 14:06:05 -05005864 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04005865}
5866
Ilya Dryomov7738a532012-03-27 17:09:17 +03005867static int get_block_group_index(struct btrfs_block_group_cache *cache)
5868{
Liu Bo31e50222012-11-21 14:18:10 +00005869 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005870}
5871
Josef Bacik817d52f2009-07-13 21:29:25 -04005872enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005873 LOOP_CACHING_NOWAIT = 0,
5874 LOOP_CACHING_WAIT = 1,
5875 LOOP_ALLOC_CHUNK = 2,
5876 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005877};
5878
5879/*
Chris Masonfec577f2007-02-26 10:40:21 -05005880 * walks the btree of allocated extents and find a hole of a given size.
5881 * The key ins is changed to record the hole:
5882 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005883 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005884 * ins->offset == number of blocks
5885 * Any available blocks before search_start are skipped.
5886 */
Chris Masond3977122009-01-05 21:25:51 -05005887static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005888 struct btrfs_root *orig_root,
5889 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005890 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005891 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005892{
Josef Bacik80eb2342008-10-29 14:49:05 -04005893 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005894 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005895 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005896 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005897 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005898 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005899 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005900 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005901 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005902 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005903 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5904 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005905 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005906 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005907 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005908 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005909 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005910
Chris Masondb945352007-10-15 16:15:53 -04005911 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005912 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005913 ins->objectid = 0;
5914 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005915
Josef Bacik3f7de032011-11-10 08:29:20 -05005916 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5917
Josef Bacik2552d172009-04-03 10:14:19 -04005918 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005919 if (!space_info) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005920 btrfs_err(root->fs_info, "No space info for %llu", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005921 return -ENOSPC;
5922 }
Josef Bacik2552d172009-04-03 10:14:19 -04005923
Josef Bacik67377732010-09-16 16:19:09 -04005924 /*
5925 * If the space info is for both data and metadata it means we have a
5926 * small filesystem and we can't use the clustering stuff.
5927 */
5928 if (btrfs_mixed_space_info(space_info))
5929 use_cluster = false;
5930
Josef Bacik67377732010-09-16 16:19:09 -04005931 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005932 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005933 if (!btrfs_test_opt(root, SSD))
5934 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005935 }
5936
Josef Bacik67377732010-09-16 16:19:09 -04005937 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5938 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005939 last_ptr = &root->fs_info->data_alloc_cluster;
5940 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005941
Chris Mason239b14b2008-03-24 15:02:07 -04005942 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005943 spin_lock(&last_ptr->lock);
5944 if (last_ptr->block_group)
5945 hint_byte = last_ptr->window_start;
5946 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005947 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005948
Chris Masona061fc82008-05-07 11:43:44 -04005949 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005950 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005951
Josef Bacik817d52f2009-07-13 21:29:25 -04005952 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005953 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005954
Josef Bacik2552d172009-04-03 10:14:19 -04005955 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005956 block_group = btrfs_lookup_block_group(root->fs_info,
5957 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005958 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005959 /*
5960 * we don't want to use the block group if it doesn't match our
5961 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005962 *
5963 * However if we are re-searching with an ideal block group
5964 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005965 */
5966 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005967 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005968 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005969 if (list_empty(&block_group->list) ||
5970 block_group->ro) {
5971 /*
5972 * someone is removing this block group,
5973 * we can't jump into the have_block_group
5974 * target because our list pointers are not
5975 * valid
5976 */
5977 btrfs_put_block_group(block_group);
5978 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005979 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005980 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005981 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005982 }
Josef Bacik2552d172009-04-03 10:14:19 -04005983 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005984 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005985 }
Chris Mason42e70e72008-11-07 18:17:11 -05005986 }
Josef Bacik2552d172009-04-03 10:14:19 -04005987search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005988 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005989 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005990 list_for_each_entry(block_group, &space_info->block_groups[index],
5991 list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005992 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005993 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005994
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005995 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005996 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005997 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005998
Chris Mason83a50de2010-12-13 15:06:46 -05005999 /*
6000 * this can happen if we end up cycling through all the
6001 * raid types, but we want to make sure we only allocate
6002 * for the proper type.
6003 */
6004 if (!block_group_bits(block_group, data)) {
6005 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6006 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006007 BTRFS_BLOCK_GROUP_RAID5 |
6008 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006009 BTRFS_BLOCK_GROUP_RAID10;
6010
6011 /*
6012 * if they asked for extra copies and this block group
6013 * doesn't provide them, bail. This does allow us to
6014 * fill raid0 from raid1.
6015 */
6016 if ((data & extra) && !(block_group->flags & extra))
6017 goto loop;
6018 }
6019
Josef Bacik2552d172009-04-03 10:14:19 -04006020have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006021 cached = block_group_cache_done(block_group);
6022 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05006023 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00006024 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006025 BUG_ON(ret < 0);
6026 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006027 }
6028
Josef Bacikea6a4782008-11-20 12:16:16 -05006029 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006030 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006031
Josef Bacik0a243252009-09-11 16:11:20 -04006032 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006033 * Ok we want to try and use the cluster allocator, so
6034 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006035 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006036 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05006037 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006038 /*
6039 * the refill lock keeps out other
6040 * people trying to start a new cluster
6041 */
6042 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006043 used_block_group = last_ptr->block_group;
6044 if (used_block_group != block_group &&
6045 (!used_block_group ||
6046 used_block_group->ro ||
6047 !block_group_bits(used_block_group, data))) {
6048 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006049 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006050 }
Chris Mason44fb5512009-06-04 15:34:51 -04006051
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006052 if (used_block_group != block_group)
6053 btrfs_get_block_group(used_block_group);
6054
6055 offset = btrfs_alloc_from_cluster(used_block_group,
6056 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006057 if (offset) {
6058 /* we have a block, we're done */
6059 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006060 trace_btrfs_reserve_extent_cluster(root,
6061 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006062 goto checks;
6063 }
6064
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006065 WARN_ON(last_ptr->block_group != used_block_group);
6066 if (used_block_group != block_group) {
6067 btrfs_put_block_group(used_block_group);
6068 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006069 }
Chris Mason44fb5512009-06-04 15:34:51 -04006070refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006071 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006072 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6073 * set up a new clusters, so lets just skip it
6074 * and let the allocator find whatever block
6075 * it can find. If we reach this point, we
6076 * will have tried the cluster allocator
6077 * plenty of times and not have found
6078 * anything, so we are likely way too
6079 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006080 * anything.
6081 *
6082 * However, if the cluster is taken from the
6083 * current block group, release the cluster
6084 * first, so that we stand a better chance of
6085 * succeeding in the unclustered
6086 * allocation. */
6087 if (loop >= LOOP_NO_EMPTY_SIZE &&
6088 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006089 spin_unlock(&last_ptr->refill_lock);
6090 goto unclustered_alloc;
6091 }
6092
Chris Masonfa9c0d792009-04-03 09:47:43 -04006093 /*
6094 * this cluster didn't work out, free it and
6095 * start over
6096 */
6097 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6098
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006099 if (loop >= LOOP_NO_EMPTY_SIZE) {
6100 spin_unlock(&last_ptr->refill_lock);
6101 goto unclustered_alloc;
6102 }
6103
Chris Mason8de972b2013-01-04 15:39:43 -05006104 aligned_cluster = max_t(unsigned long,
6105 empty_cluster + empty_size,
6106 block_group->full_stripe_len);
6107
Chris Masonfa9c0d792009-04-03 09:47:43 -04006108 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04006109 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04006110 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05006111 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05006112 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006113 if (ret == 0) {
6114 /*
6115 * now pull our allocation out of this
6116 * cluster
6117 */
6118 offset = btrfs_alloc_from_cluster(block_group,
6119 last_ptr, num_bytes,
6120 search_start);
6121 if (offset) {
6122 /* we found one, proceed */
6123 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006124 trace_btrfs_reserve_extent_cluster(root,
6125 block_group, search_start,
6126 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006127 goto checks;
6128 }
Josef Bacik0a243252009-09-11 16:11:20 -04006129 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6130 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006131 spin_unlock(&last_ptr->refill_lock);
6132
Josef Bacik0a243252009-09-11 16:11:20 -04006133 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006134 wait_block_group_cache_progress(block_group,
6135 num_bytes + empty_cluster + empty_size);
6136 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006137 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006138
Chris Masonfa9c0d792009-04-03 09:47:43 -04006139 /*
6140 * at this point we either didn't find a cluster
6141 * or we weren't able to allocate a block from our
6142 * cluster. Free the cluster we've been trying
6143 * to use, and go to the next block group
6144 */
Josef Bacik0a243252009-09-11 16:11:20 -04006145 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006146 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006147 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006148 }
6149
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006150unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006151 spin_lock(&block_group->free_space_ctl->tree_lock);
6152 if (cached &&
6153 block_group->free_space_ctl->free_space <
6154 num_bytes + empty_cluster + empty_size) {
6155 spin_unlock(&block_group->free_space_ctl->tree_lock);
6156 goto loop;
6157 }
6158 spin_unlock(&block_group->free_space_ctl->tree_lock);
6159
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006160 offset = btrfs_find_space_for_alloc(block_group, search_start,
6161 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006162 /*
6163 * If we didn't find a chunk, and we haven't failed on this
6164 * block group before, and this block group is in the middle of
6165 * caching and we are ok with waiting, then go ahead and wait
6166 * for progress to be made, and set failed_alloc to true.
6167 *
6168 * If failed_alloc is true then we've already waited on this
6169 * block group once and should move on to the next block group.
6170 */
6171 if (!offset && !failed_alloc && !cached &&
6172 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006173 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006174 num_bytes + empty_size);
6175 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006176 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006177 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006178 if (!cached)
6179 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006180 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006181 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006182checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006183 search_start = stripe_align(root, used_block_group,
6184 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006185
Josef Bacik2552d172009-04-03 10:14:19 -04006186 /* move on to the next group */
6187 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006188 used_block_group->key.objectid + used_block_group->key.offset) {
6189 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006190 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006191 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006192
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006193 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006194 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006195 search_start - offset);
6196 BUG_ON(offset > search_start);
6197
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006198 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006199 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006200 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006201 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006202 goto loop;
6203 }
Yan Zheng11833d62009-09-11 16:11:19 -04006204
Josef Bacik2552d172009-04-03 10:14:19 -04006205 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006206 ins->objectid = search_start;
6207 ins->offset = num_bytes;
6208
Josef Bacik3f7de032011-11-10 08:29:20 -05006209 trace_btrfs_reserve_extent(orig_root, block_group,
6210 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006211 if (used_block_group != block_group)
6212 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006213 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006214 break;
6215loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006216 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006217 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006218 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006219 if (used_block_group != block_group)
6220 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006221 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006222 }
6223 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006224
Miao Xie60d2adb2011-09-09 17:34:35 +08006225 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6226 goto search;
6227
Yan, Zhengb742bb82010-05-16 10:46:24 -04006228 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6229 goto search;
6230
Josef Bacik285ff5a2012-01-13 15:27:45 -05006231 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006232 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6233 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006234 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6235 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6236 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6237 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006238 */
Josef Bacik723bda22011-05-27 16:11:38 -04006239 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006240 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006241 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006242 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04006243 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04006244 CHUNK_ALLOC_FORCE);
6245 /*
6246 * Do not bail out on ENOSPC since we
6247 * can do more things.
6248 */
6249 if (ret < 0 && ret != -ENOSPC) {
6250 btrfs_abort_transaction(trans,
6251 root, ret);
6252 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006253 }
Josef Bacik723bda22011-05-27 16:11:38 -04006254 }
6255
6256 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006257 empty_size = 0;
6258 empty_cluster = 0;
6259 }
Chris Mason42e70e72008-11-07 18:17:11 -05006260
Josef Bacik723bda22011-05-27 16:11:38 -04006261 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006262 } else if (!ins->objectid) {
6263 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006264 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006265 ret = 0;
6266 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006267out:
Chris Mason0b86a832008-03-24 15:01:56 -04006268
Chris Mason0f70abe2007-02-28 16:46:22 -05006269 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006270}
Chris Masonec44a352008-04-28 15:29:52 -04006271
Josef Bacik9ed74f22009-09-11 16:12:44 -04006272static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6273 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006274{
6275 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006276 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006277
Josef Bacik9ed74f22009-09-11 16:12:44 -04006278 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006279 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6280 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006281 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006282 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006283 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006284 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006285 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6286 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006287 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006288 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006289 (unsigned long long)info->bytes_pinned,
6290 (unsigned long long)info->bytes_reserved,
6291 (unsigned long long)info->bytes_may_use,
6292 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006293 spin_unlock(&info->lock);
6294
6295 if (!dump_block_groups)
6296 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006297
Josef Bacik80eb2342008-10-29 14:49:05 -04006298 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006299again:
6300 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006301 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006302 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006303 (unsigned long long)cache->key.objectid,
6304 (unsigned long long)cache->key.offset,
6305 (unsigned long long)btrfs_block_group_used(&cache->item),
6306 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006307 (unsigned long long)cache->reserved,
6308 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006309 btrfs_dump_free_space(cache, bytes);
6310 spin_unlock(&cache->lock);
6311 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006312 if (++index < BTRFS_NR_RAID_TYPES)
6313 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006314 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006315}
Zheng Yane8569812008-09-26 10:05:48 -04006316
Yan Zheng11833d62009-09-11 16:11:19 -04006317int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6318 struct btrfs_root *root,
6319 u64 num_bytes, u64 min_alloc_size,
6320 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006321 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006322{
Miao Xie9e622d62012-01-26 15:01:12 -05006323 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006324 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006325
Josef Bacik6a632092009-02-20 11:00:09 -05006326 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006327again:
Chris Masondb945352007-10-15 16:15:53 -04006328 WARN_ON(num_bytes < root->sectorsize);
6329 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006330 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006331
Miao Xie9e622d62012-01-26 15:01:12 -05006332 if (ret == -ENOSPC) {
6333 if (!final_tried) {
6334 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006335 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006336 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006337 if (num_bytes == min_alloc_size)
6338 final_tried = true;
6339 goto again;
6340 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6341 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006342
Miao Xie9e622d62012-01-26 15:01:12 -05006343 sinfo = __find_space_info(root->fs_info, data);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006344 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
6345 (unsigned long long)data,
6346 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006347 if (sinfo)
6348 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006349 }
Chris Mason925baed2008-06-25 16:01:30 -04006350 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006351
liubo1abe9b82011-03-24 11:18:59 +00006352 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6353
Josef Bacik0f9dd462008-09-23 13:14:11 -04006354 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006355}
6356
Chris Masone688b7252011-10-31 20:52:39 -04006357static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6358 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006359{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006360 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006361 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006362
Josef Bacik0f9dd462008-09-23 13:14:11 -04006363 cache = btrfs_lookup_block_group(root->fs_info, start);
6364 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006365 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6366 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006367 return -ENOSPC;
6368 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006369
Li Dongyang5378e602011-03-24 10:24:27 +00006370 if (btrfs_test_opt(root, DISCARD))
6371 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006372
Chris Masone688b7252011-10-31 20:52:39 -04006373 if (pin)
6374 pin_down_extent(root, cache, start, len, 1);
6375 else {
6376 btrfs_add_free_space(cache, start, len);
6377 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6378 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006379 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006380
liubo1abe9b82011-03-24 11:18:59 +00006381 trace_btrfs_reserved_extent_free(root, start, len);
6382
Chris Masone6dcd2d2008-07-17 12:53:50 -04006383 return ret;
6384}
6385
Chris Masone688b7252011-10-31 20:52:39 -04006386int btrfs_free_reserved_extent(struct btrfs_root *root,
6387 u64 start, u64 len)
6388{
6389 return __btrfs_free_reserved_extent(root, start, len, 0);
6390}
6391
6392int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6393 u64 start, u64 len)
6394{
6395 return __btrfs_free_reserved_extent(root, start, len, 1);
6396}
6397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006398static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6399 struct btrfs_root *root,
6400 u64 parent, u64 root_objectid,
6401 u64 flags, u64 owner, u64 offset,
6402 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006403{
6404 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006405 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006406 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006407 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006408 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006409 struct extent_buffer *leaf;
6410 int type;
6411 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006412
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006413 if (parent > 0)
6414 type = BTRFS_SHARED_DATA_REF_KEY;
6415 else
6416 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006417
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006418 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006419
6420 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006421 if (!path)
6422 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006423
Chris Masonb9473432009-03-13 11:00:37 -04006424 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006425 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6426 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006427 if (ret) {
6428 btrfs_free_path(path);
6429 return ret;
6430 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006431
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006432 leaf = path->nodes[0];
6433 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006434 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006435 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6436 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6437 btrfs_set_extent_flags(leaf, extent_item,
6438 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006439
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006440 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6441 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6442 if (parent > 0) {
6443 struct btrfs_shared_data_ref *ref;
6444 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6445 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6446 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6447 } else {
6448 struct btrfs_extent_data_ref *ref;
6449 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6450 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6451 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6452 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6453 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6454 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006455
6456 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006457 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006458
Liu Boc53d6132012-12-27 09:01:19 +00006459 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006460 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006461 btrfs_err(fs_info, "update block group failed for %llu %llu",
6462 (unsigned long long)ins->objectid,
6463 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006464 BUG();
6465 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006466 return ret;
6467}
6468
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006469static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6470 struct btrfs_root *root,
6471 u64 parent, u64 root_objectid,
6472 u64 flags, struct btrfs_disk_key *key,
6473 int level, struct btrfs_key *ins)
6474{
6475 int ret;
6476 struct btrfs_fs_info *fs_info = root->fs_info;
6477 struct btrfs_extent_item *extent_item;
6478 struct btrfs_tree_block_info *block_info;
6479 struct btrfs_extent_inline_ref *iref;
6480 struct btrfs_path *path;
6481 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006482 u32 size = sizeof(*extent_item) + sizeof(*iref);
6483 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6484 SKINNY_METADATA);
6485
6486 if (!skinny_metadata)
6487 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006488
6489 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006490 if (!path)
6491 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006492
6493 path->leave_spinning = 1;
6494 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6495 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006496 if (ret) {
6497 btrfs_free_path(path);
6498 return ret;
6499 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006500
6501 leaf = path->nodes[0];
6502 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6503 struct btrfs_extent_item);
6504 btrfs_set_extent_refs(leaf, extent_item, 1);
6505 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6506 btrfs_set_extent_flags(leaf, extent_item,
6507 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006508
Josef Bacik3173a182013-03-07 14:22:04 -05006509 if (skinny_metadata) {
6510 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6511 } else {
6512 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6513 btrfs_set_tree_block_key(leaf, block_info, key);
6514 btrfs_set_tree_block_level(leaf, block_info, level);
6515 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6516 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006517
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006518 if (parent > 0) {
6519 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6520 btrfs_set_extent_inline_ref_type(leaf, iref,
6521 BTRFS_SHARED_BLOCK_REF_KEY);
6522 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6523 } else {
6524 btrfs_set_extent_inline_ref_type(leaf, iref,
6525 BTRFS_TREE_BLOCK_REF_KEY);
6526 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6527 }
6528
6529 btrfs_mark_buffer_dirty(leaf);
6530 btrfs_free_path(path);
6531
Josef Bacik3173a182013-03-07 14:22:04 -05006532 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006533 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006534 btrfs_err(fs_info, "update block group failed for %llu %llu",
6535 (unsigned long long)ins->objectid,
6536 (unsigned long long)ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006537 BUG();
6538 }
6539 return ret;
6540}
6541
6542int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6543 struct btrfs_root *root,
6544 u64 root_objectid, u64 owner,
6545 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006546{
6547 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006548
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006549 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006550
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006551 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6552 ins->offset, 0,
6553 root_objectid, owner, offset,
6554 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006555 return ret;
6556}
Chris Masone02119d2008-09-05 16:13:11 -04006557
6558/*
6559 * this is used by the tree logging recovery code. It records that
6560 * an extent has been allocated and makes sure to clear the free
6561 * space cache bits as well
6562 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006563int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6564 struct btrfs_root *root,
6565 u64 root_objectid, u64 owner, u64 offset,
6566 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006567{
6568 int ret;
6569 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006570 struct btrfs_caching_control *caching_ctl;
6571 u64 start = ins->objectid;
6572 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006573
Chris Masone02119d2008-09-05 16:13:11 -04006574 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006575 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006576 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006577
Yan Zheng11833d62009-09-11 16:11:19 -04006578 if (!caching_ctl) {
6579 BUG_ON(!block_group_cache_done(block_group));
6580 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006581 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006582 } else {
6583 mutex_lock(&caching_ctl->mutex);
6584
6585 if (start >= caching_ctl->progress) {
6586 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006587 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006588 } else if (start + num_bytes <= caching_ctl->progress) {
6589 ret = btrfs_remove_free_space(block_group,
6590 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006591 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006592 } else {
6593 num_bytes = caching_ctl->progress - start;
6594 ret = btrfs_remove_free_space(block_group,
6595 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006596 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006597
6598 start = caching_ctl->progress;
6599 num_bytes = ins->objectid + ins->offset -
6600 caching_ctl->progress;
6601 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006602 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006603 }
6604
6605 mutex_unlock(&caching_ctl->mutex);
6606 put_caching_control(caching_ctl);
6607 }
6608
Josef Bacikfb25e912011-07-26 17:00:46 -04006609 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6610 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006611 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d792009-04-03 09:47:43 -04006612 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006613 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6614 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006615 return ret;
6616}
6617
Chris Mason65b51a02008-08-01 15:11:20 -04006618struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6619 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006620 u64 bytenr, u32 blocksize,
6621 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006622{
6623 struct extent_buffer *buf;
6624
6625 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6626 if (!buf)
6627 return ERR_PTR(-ENOMEM);
6628 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006629 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006630 btrfs_tree_lock(buf);
6631 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006632 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006633
6634 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006635 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006636
Chris Masond0c803c2008-09-11 16:17:57 -04006637 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006638 /*
6639 * we allow two log transactions at a time, use different
6640 * EXENT bit to differentiate dirty pages.
6641 */
6642 if (root->log_transid % 2 == 0)
6643 set_extent_dirty(&root->dirty_log_pages, buf->start,
6644 buf->start + buf->len - 1, GFP_NOFS);
6645 else
6646 set_extent_new(&root->dirty_log_pages, buf->start,
6647 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006648 } else {
6649 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6650 buf->start + buf->len - 1, GFP_NOFS);
6651 }
Chris Mason65b51a02008-08-01 15:11:20 -04006652 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006653 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006654 return buf;
6655}
6656
Yan, Zhengf0486c62010-05-16 10:46:25 -04006657static struct btrfs_block_rsv *
6658use_block_rsv(struct btrfs_trans_handle *trans,
6659 struct btrfs_root *root, u32 blocksize)
6660{
6661 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006662 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006663 int ret;
6664
6665 block_rsv = get_block_rsv(trans, root);
6666
6667 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006668 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6669 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006670 /*
6671 * If we couldn't reserve metadata bytes try and use some from
6672 * the global reserve.
6673 */
6674 if (ret && block_rsv != global_rsv) {
6675 ret = block_rsv_use_bytes(global_rsv, blocksize);
6676 if (!ret)
6677 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006678 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006679 } else if (ret) {
6680 return ERR_PTR(ret);
6681 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006682 return block_rsv;
6683 }
6684
6685 ret = block_rsv_use_bytes(block_rsv, blocksize);
6686 if (!ret)
6687 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006688 if (ret && !block_rsv->failfast) {
David Sterbab069e0c2013-02-08 21:28:17 +00006689 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6690 static DEFINE_RATELIMIT_STATE(_rs,
6691 DEFAULT_RATELIMIT_INTERVAL * 10,
6692 /*DEFAULT_RATELIMIT_BURST*/ 1);
6693 if (__ratelimit(&_rs))
6694 WARN(1, KERN_DEBUG
6695 "btrfs: block rsv returned %d\n", ret);
6696 }
Miao Xie08e007d2012-10-16 11:33:38 +00006697 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6698 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006699 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006700 return block_rsv;
6701 } else if (ret && block_rsv != global_rsv) {
6702 ret = block_rsv_use_bytes(global_rsv, blocksize);
6703 if (!ret)
6704 return global_rsv;
6705 }
6706 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006707
Yan, Zhengf0486c62010-05-16 10:46:25 -04006708 return ERR_PTR(-ENOSPC);
6709}
6710
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006711static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6712 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006713{
6714 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006715 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006716}
6717
Chris Masonfec577f2007-02-26 10:40:21 -05006718/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006719 * finds a free extent and does all the dirty work required for allocation
6720 * returns the key for the extent through ins, and a tree buffer for
6721 * the first block of the extent through buf.
6722 *
Chris Masonfec577f2007-02-26 10:40:21 -05006723 * returns the tree buffer or NULL.
6724 */
Chris Mason5f39d392007-10-15 16:14:19 -04006725struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006726 struct btrfs_root *root, u32 blocksize,
6727 u64 parent, u64 root_objectid,
6728 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006729 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006730{
Chris Masone2fa7222007-03-12 16:22:34 -04006731 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006732 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006733 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006734 u64 flags = 0;
6735 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05006736 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6737 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006738
6739 block_rsv = use_block_rsv(trans, root, blocksize);
6740 if (IS_ERR(block_rsv))
6741 return ERR_CAST(block_rsv);
6742
6743 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006744 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006745 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006746 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006747 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006748 }
Chris Mason55c69072008-01-09 15:55:33 -05006749
Chris Mason4008c042009-02-12 14:09:45 -05006750 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6751 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006752 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006753
6754 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6755 if (parent == 0)
6756 parent = ins.objectid;
6757 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6758 } else
6759 BUG_ON(parent > 0);
6760
6761 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6762 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006763 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006764 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006765 if (key)
6766 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6767 else
6768 memset(&extent_op->key, 0, sizeof(extent_op->key));
6769 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05006770 if (skinny_metadata)
6771 extent_op->update_key = 0;
6772 else
6773 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006774 extent_op->update_flags = 1;
6775 extent_op->is_data = 0;
6776
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006777 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6778 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006779 ins.offset, parent, root_objectid,
6780 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006781 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006782 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006783 }
Chris Masonfec577f2007-02-26 10:40:21 -05006784 return buf;
6785}
Chris Masona28ec192007-03-06 20:08:01 -05006786
Yan Zheng2c47e6052009-06-27 21:07:35 -04006787struct walk_control {
6788 u64 refs[BTRFS_MAX_LEVEL];
6789 u64 flags[BTRFS_MAX_LEVEL];
6790 struct btrfs_key update_progress;
6791 int stage;
6792 int level;
6793 int shared_level;
6794 int update_ref;
6795 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006796 int reada_slot;
6797 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006798 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006799};
6800
6801#define DROP_REFERENCE 1
6802#define UPDATE_BACKREF 2
6803
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006804static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6805 struct btrfs_root *root,
6806 struct walk_control *wc,
6807 struct btrfs_path *path)
6808{
6809 u64 bytenr;
6810 u64 generation;
6811 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006812 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006813 u32 nritems;
6814 u32 blocksize;
6815 struct btrfs_key key;
6816 struct extent_buffer *eb;
6817 int ret;
6818 int slot;
6819 int nread = 0;
6820
6821 if (path->slots[wc->level] < wc->reada_slot) {
6822 wc->reada_count = wc->reada_count * 2 / 3;
6823 wc->reada_count = max(wc->reada_count, 2);
6824 } else {
6825 wc->reada_count = wc->reada_count * 3 / 2;
6826 wc->reada_count = min_t(int, wc->reada_count,
6827 BTRFS_NODEPTRS_PER_BLOCK(root));
6828 }
6829
6830 eb = path->nodes[wc->level];
6831 nritems = btrfs_header_nritems(eb);
6832 blocksize = btrfs_level_size(root, wc->level - 1);
6833
6834 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6835 if (nread >= wc->reada_count)
6836 break;
6837
6838 cond_resched();
6839 bytenr = btrfs_node_blockptr(eb, slot);
6840 generation = btrfs_node_ptr_generation(eb, slot);
6841
6842 if (slot == path->slots[wc->level])
6843 goto reada;
6844
6845 if (wc->stage == UPDATE_BACKREF &&
6846 generation <= root->root_key.offset)
6847 continue;
6848
Yan, Zheng94fcca92009-10-09 09:25:16 -04006849 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05006850 ret = btrfs_lookup_extent_info(trans, root, bytenr,
6851 wc->level - 1, 1, &refs,
6852 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006853 /* We don't care about errors in readahead. */
6854 if (ret < 0)
6855 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006856 BUG_ON(refs == 0);
6857
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006858 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006859 if (refs == 1)
6860 goto reada;
6861
Yan, Zheng94fcca92009-10-09 09:25:16 -04006862 if (wc->level == 1 &&
6863 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6864 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006865 if (!wc->update_ref ||
6866 generation <= root->root_key.offset)
6867 continue;
6868 btrfs_node_key_to_cpu(eb, &key, slot);
6869 ret = btrfs_comp_cpu_keys(&key,
6870 &wc->update_progress);
6871 if (ret < 0)
6872 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006873 } else {
6874 if (wc->level == 1 &&
6875 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6876 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006877 }
6878reada:
6879 ret = readahead_tree_block(root, bytenr, blocksize,
6880 generation);
6881 if (ret)
6882 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006883 nread++;
6884 }
6885 wc->reada_slot = slot;
6886}
6887
Chris Mason9aca1d52007-03-13 11:09:37 -04006888/*
Liu Bo2c016dc2012-12-26 15:32:17 +08006889 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04006890 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006891 * when wc->stage == UPDATE_BACKREF, this function updates
6892 * back refs for pointers in the block.
6893 *
6894 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006895 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006896static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6897 struct btrfs_root *root,
6898 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006899 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006900{
6901 int level = wc->level;
6902 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006903 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6904 int ret;
6905
6906 if (wc->stage == UPDATE_BACKREF &&
6907 btrfs_header_owner(eb) != root->root_key.objectid)
6908 return 1;
6909
6910 /*
6911 * when reference count of tree block is 1, it won't increase
6912 * again. once full backref flag is set, we never clear it.
6913 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006914 if (lookup_info &&
6915 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6916 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006917 BUG_ON(!path->locks[level]);
6918 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05006919 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006920 &wc->refs[level],
6921 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006922 BUG_ON(ret == -ENOMEM);
6923 if (ret)
6924 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006925 BUG_ON(wc->refs[level] == 0);
6926 }
6927
Yan Zheng2c47e6052009-06-27 21:07:35 -04006928 if (wc->stage == DROP_REFERENCE) {
6929 if (wc->refs[level] > 1)
6930 return 1;
6931
6932 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006933 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006934 path->locks[level] = 0;
6935 }
6936 return 0;
6937 }
6938
6939 /* wc->stage == UPDATE_BACKREF */
6940 if (!(wc->flags[level] & flag)) {
6941 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006942 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006943 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006944 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006945 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006946 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6947 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006948 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006949 wc->flags[level] |= flag;
6950 }
6951
6952 /*
6953 * the block is shared by multiple trees, so it's not good to
6954 * keep the tree lock
6955 */
6956 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006957 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006958 path->locks[level] = 0;
6959 }
6960 return 0;
6961}
6962
6963/*
Liu Bo2c016dc2012-12-26 15:32:17 +08006964 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006965 *
6966 * when wc->stage == DROP_REFERENCE, this function checks
6967 * reference count of the block pointed to. if the block
6968 * is shared and we need update back refs for the subtree
6969 * rooted at the block, this function changes wc->stage to
6970 * UPDATE_BACKREF. if the block is shared and there is no
6971 * need to update back, this function drops the reference
6972 * to the block.
6973 *
6974 * NOTE: return value 1 means we should stop walking down.
6975 */
6976static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6977 struct btrfs_root *root,
6978 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006979 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006980{
6981 u64 bytenr;
6982 u64 generation;
6983 u64 parent;
6984 u32 blocksize;
6985 struct btrfs_key key;
6986 struct extent_buffer *next;
6987 int level = wc->level;
6988 int reada = 0;
6989 int ret = 0;
6990
6991 generation = btrfs_node_ptr_generation(path->nodes[level],
6992 path->slots[level]);
6993 /*
6994 * if the lower level block was created before the snapshot
6995 * was created, we know there is no need to update back refs
6996 * for the subtree
6997 */
6998 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006999 generation <= root->root_key.offset) {
7000 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007001 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007002 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007003
7004 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7005 blocksize = btrfs_level_size(root, level - 1);
7006
7007 next = btrfs_find_tree_block(root, bytenr, blocksize);
7008 if (!next) {
7009 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00007010 if (!next)
7011 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007012 reada = 1;
7013 }
7014 btrfs_tree_lock(next);
7015 btrfs_set_lock_blocking(next);
7016
Josef Bacik3173a182013-03-07 14:22:04 -05007017 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007018 &wc->refs[level - 1],
7019 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007020 if (ret < 0) {
7021 btrfs_tree_unlock(next);
7022 return ret;
7023 }
7024
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007025 if (unlikely(wc->refs[level - 1] == 0)) {
7026 btrfs_err(root->fs_info, "Missing references.");
7027 BUG();
7028 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007029 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007030
Yan, Zheng94fcca92009-10-09 09:25:16 -04007031 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007032 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007033 if (level == 1 &&
7034 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7035 goto skip;
7036
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007037 if (!wc->update_ref ||
7038 generation <= root->root_key.offset)
7039 goto skip;
7040
7041 btrfs_node_key_to_cpu(path->nodes[level], &key,
7042 path->slots[level]);
7043 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7044 if (ret < 0)
7045 goto skip;
7046
7047 wc->stage = UPDATE_BACKREF;
7048 wc->shared_level = level - 1;
7049 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007050 } else {
7051 if (level == 1 &&
7052 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7053 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007054 }
7055
Chris Masonb9fab912012-05-06 07:23:47 -04007056 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007057 btrfs_tree_unlock(next);
7058 free_extent_buffer(next);
7059 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007060 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007061 }
7062
7063 if (!next) {
7064 if (reada && level == 1)
7065 reada_walk_down(trans, root, wc, path);
7066 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007067 if (!next)
7068 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007069 btrfs_tree_lock(next);
7070 btrfs_set_lock_blocking(next);
7071 }
7072
7073 level--;
7074 BUG_ON(level != btrfs_header_level(next));
7075 path->nodes[level] = next;
7076 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007077 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007078 wc->level = level;
7079 if (wc->level == 1)
7080 wc->reada_slot = 0;
7081 return 0;
7082skip:
7083 wc->refs[level - 1] = 0;
7084 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007085 if (wc->stage == DROP_REFERENCE) {
7086 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7087 parent = path->nodes[level]->start;
7088 } else {
7089 BUG_ON(root->root_key.objectid !=
7090 btrfs_header_owner(path->nodes[level]));
7091 parent = 0;
7092 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007093
Yan, Zheng94fcca92009-10-09 09:25:16 -04007094 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007095 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007096 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007097 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007098 btrfs_tree_unlock(next);
7099 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007100 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007101 return 1;
7102}
7103
7104/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007105 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007106 *
7107 * when wc->stage == DROP_REFERENCE, this function drops
7108 * reference count on the block.
7109 *
7110 * when wc->stage == UPDATE_BACKREF, this function changes
7111 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7112 * to UPDATE_BACKREF previously while processing the block.
7113 *
7114 * NOTE: return value 1 means we should stop walking up.
7115 */
7116static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7117 struct btrfs_root *root,
7118 struct btrfs_path *path,
7119 struct walk_control *wc)
7120{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007121 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007122 int level = wc->level;
7123 struct extent_buffer *eb = path->nodes[level];
7124 u64 parent = 0;
7125
7126 if (wc->stage == UPDATE_BACKREF) {
7127 BUG_ON(wc->shared_level < level);
7128 if (level < wc->shared_level)
7129 goto out;
7130
Yan Zheng2c47e6052009-06-27 21:07:35 -04007131 ret = find_next_key(path, level + 1, &wc->update_progress);
7132 if (ret > 0)
7133 wc->update_ref = 0;
7134
7135 wc->stage = DROP_REFERENCE;
7136 wc->shared_level = -1;
7137 path->slots[level] = 0;
7138
7139 /*
7140 * check reference count again if the block isn't locked.
7141 * we should start walking down the tree again if reference
7142 * count is one.
7143 */
7144 if (!path->locks[level]) {
7145 BUG_ON(level == 0);
7146 btrfs_tree_lock(eb);
7147 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007148 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007149
7150 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007151 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007152 &wc->refs[level],
7153 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007154 if (ret < 0) {
7155 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007156 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007157 return ret;
7158 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007159 BUG_ON(wc->refs[level] == 0);
7160 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007161 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007162 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007163 return 1;
7164 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007165 }
7166 }
7167
7168 /* wc->stage == DROP_REFERENCE */
7169 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7170
7171 if (wc->refs[level] == 1) {
7172 if (level == 0) {
7173 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007174 ret = btrfs_dec_ref(trans, root, eb, 1,
7175 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007176 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007177 ret = btrfs_dec_ref(trans, root, eb, 0,
7178 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007179 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007180 }
7181 /* make block locked assertion in clean_tree_block happy */
7182 if (!path->locks[level] &&
7183 btrfs_header_generation(eb) == trans->transid) {
7184 btrfs_tree_lock(eb);
7185 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007186 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007187 }
7188 clean_tree_block(trans, root, eb);
7189 }
7190
7191 if (eb == root->node) {
7192 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7193 parent = eb->start;
7194 else
7195 BUG_ON(root->root_key.objectid !=
7196 btrfs_header_owner(eb));
7197 } else {
7198 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7199 parent = path->nodes[level + 1]->start;
7200 else
7201 BUG_ON(root->root_key.objectid !=
7202 btrfs_header_owner(path->nodes[level + 1]));
7203 }
7204
Jan Schmidt5581a512012-05-16 17:04:52 +02007205 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007206out:
7207 wc->refs[level] = 0;
7208 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007209 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007210}
7211
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007212static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7213 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007214 struct btrfs_path *path,
7215 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007216{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007217 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007218 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007219 int ret;
7220
Yan Zheng2c47e6052009-06-27 21:07:35 -04007221 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007222 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007223 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007224 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007225
Yan Zheng2c47e6052009-06-27 21:07:35 -04007226 if (level == 0)
7227 break;
7228
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007229 if (path->slots[level] >=
7230 btrfs_header_nritems(path->nodes[level]))
7231 break;
7232
Yan, Zheng94fcca92009-10-09 09:25:16 -04007233 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007234 if (ret > 0) {
7235 path->slots[level]++;
7236 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00007237 } else if (ret < 0)
7238 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007239 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007240 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007241 return 0;
7242}
7243
Chris Masond3977122009-01-05 21:25:51 -05007244static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007245 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007246 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007247 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007248{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007249 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007250 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007251
Yan Zheng2c47e6052009-06-27 21:07:35 -04007252 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7253 while (level < max_level && path->nodes[level]) {
7254 wc->level = level;
7255 if (path->slots[level] + 1 <
7256 btrfs_header_nritems(path->nodes[level])) {
7257 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007258 return 0;
7259 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007260 ret = walk_up_proc(trans, root, path, wc);
7261 if (ret > 0)
7262 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007263
Yan Zheng2c47e6052009-06-27 21:07:35 -04007264 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007265 btrfs_tree_unlock_rw(path->nodes[level],
7266 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007267 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007268 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007269 free_extent_buffer(path->nodes[level]);
7270 path->nodes[level] = NULL;
7271 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007272 }
7273 }
7274 return 1;
7275}
7276
Chris Mason9aca1d52007-03-13 11:09:37 -04007277/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007278 * drop a subvolume tree.
7279 *
7280 * this function traverses the tree freeing any blocks that only
7281 * referenced by the tree.
7282 *
7283 * when a shared tree block is found. this function decreases its
7284 * reference count by one. if update_ref is true, this function
7285 * also make sure backrefs for the shared block and all lower level
7286 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007287 *
7288 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007289 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007290int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007291 struct btrfs_block_rsv *block_rsv, int update_ref,
7292 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007293{
Chris Mason5caf2a02007-04-02 11:20:42 -04007294 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007295 struct btrfs_trans_handle *trans;
7296 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007297 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007298 struct walk_control *wc;
7299 struct btrfs_key key;
7300 int err = 0;
7301 int ret;
7302 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007303
Chris Mason5caf2a02007-04-02 11:20:42 -04007304 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007305 if (!path) {
7306 err = -ENOMEM;
7307 goto out;
7308 }
Chris Mason20524f02007-03-10 06:35:47 -05007309
Yan Zheng2c47e6052009-06-27 21:07:35 -04007310 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007311 if (!wc) {
7312 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007313 err = -ENOMEM;
7314 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007315 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007316
Yan, Zhenga22285a2010-05-16 10:48:46 -04007317 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007318 if (IS_ERR(trans)) {
7319 err = PTR_ERR(trans);
7320 goto out_free;
7321 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007322
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007323 if (block_rsv)
7324 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007325
Chris Mason9f3a7422007-08-07 15:52:19 -04007326 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007327 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007328 path->nodes[level] = btrfs_lock_root_node(root);
7329 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007330 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007331 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007332 memset(&wc->update_progress, 0,
7333 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007334 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007335 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007336 memcpy(&wc->update_progress, &key,
7337 sizeof(wc->update_progress));
7338
Chris Mason6702ed42007-08-07 16:15:09 -04007339 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007340 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007341 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7343 path->lowest_level = 0;
7344 if (ret < 0) {
7345 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007346 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007347 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007348 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007349
Chris Mason7d9eb122008-07-08 14:19:17 -04007350 /*
7351 * unlock our path, this is safe because only this
7352 * function is allowed to delete this snapshot
7353 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007354 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007355
Yan Zheng2c47e6052009-06-27 21:07:35 -04007356 level = btrfs_header_level(root->node);
7357 while (1) {
7358 btrfs_tree_lock(path->nodes[level]);
7359 btrfs_set_lock_blocking(path->nodes[level]);
7360
7361 ret = btrfs_lookup_extent_info(trans, root,
7362 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007363 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007364 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007365 if (ret < 0) {
7366 err = ret;
7367 goto out_end_trans;
7368 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007369 BUG_ON(wc->refs[level] == 0);
7370
7371 if (level == root_item->drop_level)
7372 break;
7373
7374 btrfs_tree_unlock(path->nodes[level]);
7375 WARN_ON(wc->refs[level] != 1);
7376 level--;
7377 }
7378 }
7379
7380 wc->level = level;
7381 wc->shared_level = -1;
7382 wc->stage = DROP_REFERENCE;
7383 wc->update_ref = update_ref;
7384 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007385 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007386 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007387
7388 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007389 if (!for_reloc && btrfs_fs_closing(root->fs_info)) {
7390 pr_debug("btrfs: drop snapshot early exit\n");
7391 err = -EAGAIN;
7392 goto out_end_trans;
7393 }
7394
Yan Zheng2c47e6052009-06-27 21:07:35 -04007395 ret = walk_down_tree(trans, root, path, wc);
7396 if (ret < 0) {
7397 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007398 break;
7399 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007400
7401 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7402 if (ret < 0) {
7403 err = ret;
7404 break;
7405 }
7406
7407 if (ret > 0) {
7408 BUG_ON(wc->stage != DROP_REFERENCE);
7409 break;
7410 }
7411
7412 if (wc->stage == DROP_REFERENCE) {
7413 level = wc->level;
7414 btrfs_node_key(path->nodes[level],
7415 &root_item->drop_progress,
7416 path->slots[level]);
7417 root_item->drop_level = level;
7418 }
7419
7420 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007421 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007422 ret = btrfs_update_root(trans, tree_root,
7423 &root->root_key,
7424 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007425 if (ret) {
7426 btrfs_abort_transaction(trans, tree_root, ret);
7427 err = ret;
7428 goto out_end_trans;
7429 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007430
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007431 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007432 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007433 if (IS_ERR(trans)) {
7434 err = PTR_ERR(trans);
7435 goto out_free;
7436 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007437 if (block_rsv)
7438 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007439 }
Chris Mason20524f02007-03-10 06:35:47 -05007440 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007441 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007442 if (err)
7443 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007444
7445 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007446 if (ret) {
7447 btrfs_abort_transaction(trans, tree_root, ret);
7448 goto out_end_trans;
7449 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007450
Yan, Zheng76dda932009-09-21 16:00:26 -04007451 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7452 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7453 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007454 if (ret < 0) {
7455 btrfs_abort_transaction(trans, tree_root, ret);
7456 err = ret;
7457 goto out_end_trans;
7458 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007459 /* if we fail to delete the orphan item this time
7460 * around, it'll get picked up the next time.
7461 *
7462 * The most common failure here is just -ENOENT.
7463 */
7464 btrfs_del_orphan_item(trans, tree_root,
7465 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007466 }
7467 }
7468
7469 if (root->in_radix) {
7470 btrfs_free_fs_root(tree_root->fs_info, root);
7471 } else {
7472 free_extent_buffer(root->node);
7473 free_extent_buffer(root->commit_root);
7474 kfree(root);
7475 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007476out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007477 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007478out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007479 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007480 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007481out:
7482 if (err)
7483 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007484 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007485}
Chris Mason9078a3e2007-04-26 16:46:15 -04007486
Yan Zheng2c47e6052009-06-27 21:07:35 -04007487/*
7488 * drop subtree rooted at tree block 'node'.
7489 *
7490 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007491 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007492 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007493int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7494 struct btrfs_root *root,
7495 struct extent_buffer *node,
7496 struct extent_buffer *parent)
7497{
7498 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007499 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007500 int level;
7501 int parent_level;
7502 int ret = 0;
7503 int wret;
7504
Yan Zheng2c47e6052009-06-27 21:07:35 -04007505 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7506
Yan Zhengf82d02d2008-10-29 14:49:05 -04007507 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007508 if (!path)
7509 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007510
Yan Zheng2c47e6052009-06-27 21:07:35 -04007511 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007512 if (!wc) {
7513 btrfs_free_path(path);
7514 return -ENOMEM;
7515 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007516
Chris Masonb9447ef2009-03-09 11:45:38 -04007517 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007518 parent_level = btrfs_header_level(parent);
7519 extent_buffer_get(parent);
7520 path->nodes[parent_level] = parent;
7521 path->slots[parent_level] = btrfs_header_nritems(parent);
7522
Chris Masonb9447ef2009-03-09 11:45:38 -04007523 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007524 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007525 path->nodes[level] = node;
7526 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007527 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007528
7529 wc->refs[parent_level] = 1;
7530 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7531 wc->level = level;
7532 wc->shared_level = -1;
7533 wc->stage = DROP_REFERENCE;
7534 wc->update_ref = 0;
7535 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007536 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007537 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007538
7539 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007540 wret = walk_down_tree(trans, root, path, wc);
7541 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007542 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007543 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007544 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007545
Yan Zheng2c47e6052009-06-27 21:07:35 -04007546 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007547 if (wret < 0)
7548 ret = wret;
7549 if (wret != 0)
7550 break;
7551 }
7552
Yan Zheng2c47e6052009-06-27 21:07:35 -04007553 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007554 btrfs_free_path(path);
7555 return ret;
7556}
7557
Chris Masonec44a352008-04-28 15:29:52 -04007558static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7559{
7560 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007561 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007562
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007563 /*
7564 * if restripe for this chunk_type is on pick target profile and
7565 * return, otherwise do the usual balance
7566 */
7567 stripped = get_restripe_target(root->fs_info, flags);
7568 if (stripped)
7569 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007570
Chris Masoncd02dca2010-12-13 14:56:23 -05007571 /*
7572 * we add in the count of missing devices because we want
7573 * to make sure that any RAID levels on a degraded FS
7574 * continue to be honored.
7575 */
7576 num_devices = root->fs_info->fs_devices->rw_devices +
7577 root->fs_info->fs_devices->missing_devices;
7578
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007579 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007580 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007581 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7582
Chris Masonec44a352008-04-28 15:29:52 -04007583 if (num_devices == 1) {
7584 stripped |= BTRFS_BLOCK_GROUP_DUP;
7585 stripped = flags & ~stripped;
7586
7587 /* turn raid0 into single device chunks */
7588 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7589 return stripped;
7590
7591 /* turn mirroring into duplication */
7592 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7593 BTRFS_BLOCK_GROUP_RAID10))
7594 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007595 } else {
7596 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007597 if (flags & stripped)
7598 return flags;
7599
7600 stripped |= BTRFS_BLOCK_GROUP_DUP;
7601 stripped = flags & ~stripped;
7602
7603 /* switch duplicated blocks with raid1 */
7604 if (flags & BTRFS_BLOCK_GROUP_DUP)
7605 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7606
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007607 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007608 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007609
Chris Masonec44a352008-04-28 15:29:52 -04007610 return flags;
7611}
7612
Miao Xie199c36e2011-07-15 10:34:36 +00007613static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007614{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007615 struct btrfs_space_info *sinfo = cache->space_info;
7616 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007617 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007618 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007619
Chris Masonc286ac42008-07-22 23:06:41 -04007620
Miao Xie199c36e2011-07-15 10:34:36 +00007621 /*
7622 * We need some metadata space and system metadata space for
7623 * allocating chunks in some corner cases until we force to set
7624 * it to be readonly.
7625 */
7626 if ((sinfo->flags &
7627 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7628 !force)
7629 min_allocable_bytes = 1 * 1024 * 1024;
7630 else
7631 min_allocable_bytes = 0;
7632
Yan, Zhengf0486c62010-05-16 10:46:25 -04007633 spin_lock(&sinfo->lock);
7634 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007635
7636 if (cache->ro) {
7637 ret = 0;
7638 goto out;
7639 }
7640
Yan, Zhengf0486c62010-05-16 10:46:25 -04007641 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7642 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007643
Yan, Zhengf0486c62010-05-16 10:46:25 -04007644 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007645 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7646 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007647 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007648 cache->ro = 1;
7649 ret = 0;
7650 }
WuBo61cfea92011-07-26 03:30:11 +00007651out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007652 spin_unlock(&cache->lock);
7653 spin_unlock(&sinfo->lock);
7654 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007655}
7656
Yan, Zhengf0486c62010-05-16 10:46:25 -04007657int btrfs_set_block_group_ro(struct btrfs_root *root,
7658 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007659
7660{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007661 struct btrfs_trans_handle *trans;
7662 u64 alloc_flags;
7663 int ret;
7664
7665 BUG_ON(cache->ro);
7666
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007667 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007668 if (IS_ERR(trans))
7669 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007670
7671 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007672 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007673 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007674 CHUNK_ALLOC_FORCE);
7675 if (ret < 0)
7676 goto out;
7677 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007678
Miao Xie199c36e2011-07-15 10:34:36 +00007679 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007680 if (!ret)
7681 goto out;
7682 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007683 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007684 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007685 if (ret < 0)
7686 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007687 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007688out:
7689 btrfs_end_transaction(trans, root);
7690 return ret;
7691}
7692
Chris Masonc87f08c2011-02-16 13:57:04 -05007693int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7694 struct btrfs_root *root, u64 type)
7695{
7696 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007697 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007698 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007699}
7700
Miao Xie6d07bce2011-01-05 10:07:31 +00007701/*
7702 * helper to account the unused space of all the readonly block group in the
7703 * list. takes mirrors into account.
7704 */
7705static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7706{
7707 struct btrfs_block_group_cache *block_group;
7708 u64 free_bytes = 0;
7709 int factor;
7710
7711 list_for_each_entry(block_group, groups_list, list) {
7712 spin_lock(&block_group->lock);
7713
7714 if (!block_group->ro) {
7715 spin_unlock(&block_group->lock);
7716 continue;
7717 }
7718
7719 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7720 BTRFS_BLOCK_GROUP_RAID10 |
7721 BTRFS_BLOCK_GROUP_DUP))
7722 factor = 2;
7723 else
7724 factor = 1;
7725
7726 free_bytes += (block_group->key.offset -
7727 btrfs_block_group_used(&block_group->item)) *
7728 factor;
7729
7730 spin_unlock(&block_group->lock);
7731 }
7732
7733 return free_bytes;
7734}
7735
7736/*
7737 * helper to account the unused space of all the readonly block group in the
7738 * space_info. takes mirrors into account.
7739 */
7740u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7741{
7742 int i;
7743 u64 free_bytes = 0;
7744
7745 spin_lock(&sinfo->lock);
7746
7747 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7748 if (!list_empty(&sinfo->block_groups[i]))
7749 free_bytes += __btrfs_get_ro_block_group_free_space(
7750 &sinfo->block_groups[i]);
7751
7752 spin_unlock(&sinfo->lock);
7753
7754 return free_bytes;
7755}
7756
Jeff Mahoney143bede2012-03-01 14:56:26 +01007757void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007758 struct btrfs_block_group_cache *cache)
7759{
7760 struct btrfs_space_info *sinfo = cache->space_info;
7761 u64 num_bytes;
7762
7763 BUG_ON(!cache->ro);
7764
7765 spin_lock(&sinfo->lock);
7766 spin_lock(&cache->lock);
7767 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7768 cache->bytes_super - btrfs_block_group_used(&cache->item);
7769 sinfo->bytes_readonly -= num_bytes;
7770 cache->ro = 0;
7771 spin_unlock(&cache->lock);
7772 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007773}
7774
Josef Bacikba1bf482009-09-11 16:11:19 -04007775/*
7776 * checks to see if its even possible to relocate this block group.
7777 *
7778 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7779 * ok to go ahead and try.
7780 */
7781int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007782{
Zheng Yan1a40e232008-09-26 10:09:34 -04007783 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007784 struct btrfs_space_info *space_info;
7785 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7786 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007787 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007788 u64 dev_min = 1;
7789 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007790 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007791 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007792 int full = 0;
7793 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007794
Josef Bacikba1bf482009-09-11 16:11:19 -04007795 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007796
Josef Bacikba1bf482009-09-11 16:11:19 -04007797 /* odd, couldn't find the block group, leave it alone */
7798 if (!block_group)
7799 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007800
liubocdcb7252011-08-03 10:15:25 +00007801 min_free = btrfs_block_group_used(&block_group->item);
7802
Josef Bacikba1bf482009-09-11 16:11:19 -04007803 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007804 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007805 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007806
Josef Bacikba1bf482009-09-11 16:11:19 -04007807 space_info = block_group->space_info;
7808 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007809
Josef Bacikba1bf482009-09-11 16:11:19 -04007810 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007811
Josef Bacikba1bf482009-09-11 16:11:19 -04007812 /*
7813 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007814 * relocate it unless we're able to allocate a new chunk below.
7815 *
7816 * Otherwise, we need to make sure we have room in the space to handle
7817 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007818 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007819 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007820 (space_info->bytes_used + space_info->bytes_reserved +
7821 space_info->bytes_pinned + space_info->bytes_readonly +
7822 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007823 spin_unlock(&space_info->lock);
7824 goto out;
7825 }
7826 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007827
Josef Bacikba1bf482009-09-11 16:11:19 -04007828 /*
7829 * ok we don't have enough space, but maybe we have free space on our
7830 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007831 * alloc devices and guess if we have enough space. if this block
7832 * group is going to be restriped, run checks against the target
7833 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007834 */
7835 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007836
liubocdcb7252011-08-03 10:15:25 +00007837 /*
7838 * index:
7839 * 0: raid10
7840 * 1: raid1
7841 * 2: dup
7842 * 3: raid0
7843 * 4: single
7844 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007845 target = get_restripe_target(root->fs_info, block_group->flags);
7846 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007847 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007848 } else {
7849 /*
7850 * this is just a balance, so if we were marked as full
7851 * we know there is no space for a new chunk
7852 */
7853 if (full)
7854 goto out;
7855
7856 index = get_block_group_index(block_group);
7857 }
7858
Miao Xiee6ec7162013-01-17 05:38:51 +00007859 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00007860 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007861 /* Divide by 2 */
7862 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007863 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00007864 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00007865 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04007866 /* Multiply by 2 */
7867 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007868 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00007869 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007870 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007871 }
7872
Josef Bacikba1bf482009-09-11 16:11:19 -04007873 mutex_lock(&root->fs_info->chunk_mutex);
7874 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007875 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007876
Josef Bacikba1bf482009-09-11 16:11:19 -04007877 /*
7878 * check to make sure we can actually find a chunk with enough
7879 * space to fit our block group in.
7880 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007881 if (device->total_bytes > device->bytes_used + min_free &&
7882 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007883 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007884 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007885 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007886 dev_nr++;
7887
7888 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007889 break;
liubocdcb7252011-08-03 10:15:25 +00007890
Josef Bacikba1bf482009-09-11 16:11:19 -04007891 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007892 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007893 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007894 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007895out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007896 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007897 return ret;
7898}
7899
Christoph Hellwigb2950862008-12-02 09:54:17 -05007900static int find_first_block_group(struct btrfs_root *root,
7901 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007902{
Chris Mason925baed2008-06-25 16:01:30 -04007903 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007904 struct btrfs_key found_key;
7905 struct extent_buffer *leaf;
7906 int slot;
7907
7908 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7909 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007910 goto out;
7911
Chris Masond3977122009-01-05 21:25:51 -05007912 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007913 slot = path->slots[0];
7914 leaf = path->nodes[0];
7915 if (slot >= btrfs_header_nritems(leaf)) {
7916 ret = btrfs_next_leaf(root, path);
7917 if (ret == 0)
7918 continue;
7919 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007920 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007921 break;
7922 }
7923 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7924
7925 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007926 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7927 ret = 0;
7928 goto out;
7929 }
Chris Mason0b86a832008-03-24 15:01:56 -04007930 path->slots[0]++;
7931 }
Chris Mason925baed2008-06-25 16:01:30 -04007932out:
Chris Mason0b86a832008-03-24 15:01:56 -04007933 return ret;
7934}
7935
Josef Bacik0af3d002010-06-21 14:48:16 -04007936void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7937{
7938 struct btrfs_block_group_cache *block_group;
7939 u64 last = 0;
7940
7941 while (1) {
7942 struct inode *inode;
7943
7944 block_group = btrfs_lookup_first_block_group(info, last);
7945 while (block_group) {
7946 spin_lock(&block_group->lock);
7947 if (block_group->iref)
7948 break;
7949 spin_unlock(&block_group->lock);
7950 block_group = next_block_group(info->tree_root,
7951 block_group);
7952 }
7953 if (!block_group) {
7954 if (last == 0)
7955 break;
7956 last = 0;
7957 continue;
7958 }
7959
7960 inode = block_group->inode;
7961 block_group->iref = 0;
7962 block_group->inode = NULL;
7963 spin_unlock(&block_group->lock);
7964 iput(inode);
7965 last = block_group->key.objectid + block_group->key.offset;
7966 btrfs_put_block_group(block_group);
7967 }
7968}
7969
Zheng Yan1a40e232008-09-26 10:09:34 -04007970int btrfs_free_block_groups(struct btrfs_fs_info *info)
7971{
7972 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007973 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007974 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007975 struct rb_node *n;
7976
Yan Zheng11833d62009-09-11 16:11:19 -04007977 down_write(&info->extent_commit_sem);
7978 while (!list_empty(&info->caching_block_groups)) {
7979 caching_ctl = list_entry(info->caching_block_groups.next,
7980 struct btrfs_caching_control, list);
7981 list_del(&caching_ctl->list);
7982 put_caching_control(caching_ctl);
7983 }
7984 up_write(&info->extent_commit_sem);
7985
Zheng Yan1a40e232008-09-26 10:09:34 -04007986 spin_lock(&info->block_group_cache_lock);
7987 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7988 block_group = rb_entry(n, struct btrfs_block_group_cache,
7989 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007990 rb_erase(&block_group->cache_node,
7991 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007992 spin_unlock(&info->block_group_cache_lock);
7993
Josef Bacik80eb2342008-10-29 14:49:05 -04007994 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007995 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007996 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007997
Josef Bacik817d52f2009-07-13 21:29:25 -04007998 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007999 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008000
Josef Bacik3c148742011-02-02 15:53:47 +00008001 /*
8002 * We haven't cached this block group, which means we could
8003 * possibly have excluded extents on this block group.
8004 */
8005 if (block_group->cached == BTRFS_CACHE_NO)
8006 free_excluded_extents(info->extent_root, block_group);
8007
Josef Bacik817d52f2009-07-13 21:29:25 -04008008 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008009 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008010
8011 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008012 }
8013 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008014
8015 /* now that all the block groups are freed, go through and
8016 * free all the space_info structs. This is only called during
8017 * the final stages of unmount, and so we know nobody is
8018 * using them. We call synchronize_rcu() once before we start,
8019 * just to be on the safe side.
8020 */
8021 synchronize_rcu();
8022
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008023 release_global_block_rsv(info);
8024
Chris Mason4184ea72009-03-10 12:39:20 -04008025 while(!list_empty(&info->space_info)) {
8026 space_info = list_entry(info->space_info.next,
8027 struct btrfs_space_info,
8028 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008029 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8030 if (space_info->bytes_pinned > 0 ||
8031 space_info->bytes_reserved > 0 ||
8032 space_info->bytes_may_use > 0) {
8033 WARN_ON(1);
8034 dump_space_info(space_info, 0, 0);
8035 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008036 }
Chris Mason4184ea72009-03-10 12:39:20 -04008037 list_del(&space_info->list);
8038 kfree(space_info);
8039 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008040 return 0;
8041}
8042
Yan, Zhengb742bb82010-05-16 10:46:24 -04008043static void __link_block_group(struct btrfs_space_info *space_info,
8044 struct btrfs_block_group_cache *cache)
8045{
8046 int index = get_block_group_index(cache);
8047
8048 down_write(&space_info->groups_sem);
8049 list_add_tail(&cache->list, &space_info->block_groups[index]);
8050 up_write(&space_info->groups_sem);
8051}
8052
Chris Mason9078a3e2007-04-26 16:46:15 -04008053int btrfs_read_block_groups(struct btrfs_root *root)
8054{
8055 struct btrfs_path *path;
8056 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008057 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008058 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008059 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008060 struct btrfs_key key;
8061 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008062 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008063 int need_clear = 0;
8064 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008065
Chris Masonbe744172007-05-06 10:15:01 -04008066 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008067 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008068 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008069 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008070 path = btrfs_alloc_path();
8071 if (!path)
8072 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008073 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008074
David Sterba6c417612011-04-13 15:41:04 +02008075 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008076 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008077 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008078 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008079 if (btrfs_test_opt(root, CLEAR_CACHE))
8080 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008081
Chris Masond3977122009-01-05 21:25:51 -05008082 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008083 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008084 if (ret > 0)
8085 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008086 if (ret != 0)
8087 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008088 leaf = path->nodes[0];
8089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008090 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008091 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008092 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008093 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008094 }
Li Zefan34d52cb2011-03-29 13:46:06 +08008095 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8096 GFP_NOFS);
8097 if (!cache->free_space_ctl) {
8098 kfree(cache);
8099 ret = -ENOMEM;
8100 goto error;
8101 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008102
Yan Zhengd2fb3432008-12-11 16:30:39 -05008103 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008104 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008105 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008106 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008107 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008108
Liu Bocf7c1ef2012-07-06 03:31:34 -06008109 if (need_clear) {
8110 /*
8111 * When we mount with old space cache, we need to
8112 * set BTRFS_DC_CLEAR and set dirty flag.
8113 *
8114 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8115 * truncate the old free space cache inode and
8116 * setup a new one.
8117 * b) Setting 'dirty flag' makes sure that we flush
8118 * the new space cache info onto disk.
8119 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008120 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008121 if (btrfs_test_opt(root, SPACE_CACHE))
8122 cache->dirty = 1;
8123 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008124
Chris Mason5f39d392007-10-15 16:14:19 -04008125 read_extent_buffer(leaf, &cache->item,
8126 btrfs_item_ptr_offset(leaf, path->slots[0]),
8127 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008128 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008129
Chris Mason9078a3e2007-04-26 16:46:15 -04008130 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008131 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008132 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008133 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05008134 cache->full_stripe_len = btrfs_full_stripe_len(root,
8135 &root->fs_info->mapping_tree,
8136 found_key.objectid);
Li Zefan34d52cb2011-03-29 13:46:06 +08008137 btrfs_init_free_space_ctl(cache);
8138
Josef Bacik817d52f2009-07-13 21:29:25 -04008139 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008140 * We need to exclude the super stripes now so that the space
8141 * info has super bytes accounted for, otherwise we'll think
8142 * we have more space than we actually do.
8143 */
Josef Bacik835d9742013-03-19 12:13:25 -04008144 ret = exclude_super_stripes(root, cache);
8145 if (ret) {
8146 /*
8147 * We may have excluded something, so call this just in
8148 * case.
8149 */
8150 free_excluded_extents(root, cache);
8151 kfree(cache->free_space_ctl);
8152 kfree(cache);
8153 goto error;
8154 }
Josef Bacik3c148742011-02-02 15:53:47 +00008155
8156 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008157 * check for two cases, either we are full, and therefore
8158 * don't need to bother with the caching work since we won't
8159 * find any space, or we are empty, and we can just add all
8160 * the space in and be done with it. This saves us _alot_ of
8161 * time, particularly in the full case.
8162 */
8163 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008164 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008165 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008166 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008167 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008168 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008169 cache->cached = BTRFS_CACHE_FINISHED;
8170 add_new_free_space(cache, root->fs_info,
8171 found_key.objectid,
8172 found_key.objectid +
8173 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008174 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008175 }
Chris Mason96b51792007-10-15 16:15:19 -04008176
Josef Bacik8c579fe2013-04-02 12:40:42 -04008177 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8178 if (ret) {
8179 btrfs_remove_free_space_cache(cache);
8180 btrfs_put_block_group(cache);
8181 goto error;
8182 }
8183
Chris Mason6324fbf2008-03-24 15:01:59 -04008184 ret = update_space_info(info, cache->flags, found_key.offset,
8185 btrfs_block_group_used(&cache->item),
8186 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008187 if (ret) {
8188 btrfs_remove_free_space_cache(cache);
8189 spin_lock(&info->block_group_cache_lock);
8190 rb_erase(&cache->cache_node,
8191 &info->block_group_cache_tree);
8192 spin_unlock(&info->block_group_cache_lock);
8193 btrfs_put_block_group(cache);
8194 goto error;
8195 }
8196
Chris Mason6324fbf2008-03-24 15:01:59 -04008197 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008198 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008199 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008200 spin_unlock(&cache->space_info->lock);
8201
Yan, Zhengb742bb82010-05-16 10:46:24 -04008202 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008203
Chris Mason75ccf472008-09-30 19:24:06 -04008204 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008205 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008206 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008207 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008208
8209 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8210 if (!(get_alloc_profile(root, space_info->flags) &
8211 (BTRFS_BLOCK_GROUP_RAID10 |
8212 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008213 BTRFS_BLOCK_GROUP_RAID5 |
8214 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008215 BTRFS_BLOCK_GROUP_DUP)))
8216 continue;
8217 /*
8218 * avoid allocating from un-mirrored block group if there are
8219 * mirrored block groups.
8220 */
8221 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008222 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008223 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008224 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008225 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008226
8227 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008228 ret = 0;
8229error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008230 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008231 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008232}
Chris Mason6324fbf2008-03-24 15:01:59 -04008233
Josef Bacikea658ba2012-09-11 16:57:25 -04008234void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8235 struct btrfs_root *root)
8236{
8237 struct btrfs_block_group_cache *block_group, *tmp;
8238 struct btrfs_root *extent_root = root->fs_info->extent_root;
8239 struct btrfs_block_group_item item;
8240 struct btrfs_key key;
8241 int ret = 0;
8242
8243 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8244 new_bg_list) {
8245 list_del_init(&block_group->new_bg_list);
8246
8247 if (ret)
8248 continue;
8249
8250 spin_lock(&block_group->lock);
8251 memcpy(&item, &block_group->item, sizeof(item));
8252 memcpy(&key, &block_group->key, sizeof(key));
8253 spin_unlock(&block_group->lock);
8254
8255 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8256 sizeof(item));
8257 if (ret)
8258 btrfs_abort_transaction(trans, extent_root, ret);
8259 }
8260}
8261
Chris Mason6324fbf2008-03-24 15:01:59 -04008262int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8263 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008264 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008265 u64 size)
8266{
8267 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008268 struct btrfs_root *extent_root;
8269 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008270
8271 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008272
Chris Mason12fcfd22009-03-24 10:24:20 -04008273 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008274
Chris Mason8f18cf12008-04-25 16:53:30 -04008275 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008276 if (!cache)
8277 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008278 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8279 GFP_NOFS);
8280 if (!cache->free_space_ctl) {
8281 kfree(cache);
8282 return -ENOMEM;
8283 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008284
Chris Masone17cade2008-04-15 15:41:47 -04008285 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008286 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008287 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008288 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008289 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008290 cache->full_stripe_len = btrfs_full_stripe_len(root,
8291 &root->fs_info->mapping_tree,
8292 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008293
Yan Zhengd2fb3432008-12-11 16:30:39 -05008294 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008295 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008296 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008297 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008298 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008299
Li Zefan34d52cb2011-03-29 13:46:06 +08008300 btrfs_init_free_space_ctl(cache);
8301
Chris Mason6324fbf2008-03-24 15:01:59 -04008302 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008303 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8304 cache->flags = type;
8305 btrfs_set_block_group_flags(&cache->item, type);
8306
Yan Zheng11833d62009-09-11 16:11:19 -04008307 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008308 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008309 ret = exclude_super_stripes(root, cache);
8310 if (ret) {
8311 /*
8312 * We may have excluded something, so call this just in
8313 * case.
8314 */
8315 free_excluded_extents(root, cache);
8316 kfree(cache->free_space_ctl);
8317 kfree(cache);
8318 return ret;
8319 }
Josef Bacik96303082009-07-13 21:29:25 -04008320
Josef Bacik817d52f2009-07-13 21:29:25 -04008321 add_new_free_space(cache, root->fs_info, chunk_offset,
8322 chunk_offset + size);
8323
Yan Zheng11833d62009-09-11 16:11:19 -04008324 free_excluded_extents(root, cache);
8325
Josef Bacik8c579fe2013-04-02 12:40:42 -04008326 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8327 if (ret) {
8328 btrfs_remove_free_space_cache(cache);
8329 btrfs_put_block_group(cache);
8330 return ret;
8331 }
8332
Chris Mason6324fbf2008-03-24 15:01:59 -04008333 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8334 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008335 if (ret) {
8336 btrfs_remove_free_space_cache(cache);
8337 spin_lock(&root->fs_info->block_group_cache_lock);
8338 rb_erase(&cache->cache_node,
8339 &root->fs_info->block_group_cache_tree);
8340 spin_unlock(&root->fs_info->block_group_cache_lock);
8341 btrfs_put_block_group(cache);
8342 return ret;
8343 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008344 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008345
8346 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008347 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008348 spin_unlock(&cache->space_info->lock);
8349
Yan, Zhengb742bb82010-05-16 10:46:24 -04008350 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008351
Josef Bacikea658ba2012-09-11 16:57:25 -04008352 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008353
Chris Masond18a2c42008-04-04 15:40:00 -04008354 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008355
Chris Mason6324fbf2008-03-24 15:01:59 -04008356 return 0;
8357}
Zheng Yan1a40e232008-09-26 10:09:34 -04008358
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008359static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8360{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008361 u64 extra_flags = chunk_to_extended(flags) &
8362 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008363
Miao Xiede98ced2013-01-29 10:13:12 +00008364 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008365 if (flags & BTRFS_BLOCK_GROUP_DATA)
8366 fs_info->avail_data_alloc_bits &= ~extra_flags;
8367 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8368 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8369 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8370 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008371 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008372}
8373
Zheng Yan1a40e232008-09-26 10:09:34 -04008374int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8375 struct btrfs_root *root, u64 group_start)
8376{
8377 struct btrfs_path *path;
8378 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008379 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008380 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008381 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008382 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008383 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008384 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008385 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008386
Zheng Yan1a40e232008-09-26 10:09:34 -04008387 root = root->fs_info->extent_root;
8388
8389 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8390 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008391 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008392
liubo9f7c43c2011-03-07 02:13:33 +00008393 /*
8394 * Free the reserved super bytes from this block group before
8395 * remove it.
8396 */
8397 free_excluded_extents(root, block_group);
8398
Zheng Yan1a40e232008-09-26 10:09:34 -04008399 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008400 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008401 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8402 BTRFS_BLOCK_GROUP_RAID1 |
8403 BTRFS_BLOCK_GROUP_RAID10))
8404 factor = 2;
8405 else
8406 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008407
Chris Mason44fb5512009-06-04 15:34:51 -04008408 /* make sure this block group isn't part of an allocation cluster */
8409 cluster = &root->fs_info->data_alloc_cluster;
8410 spin_lock(&cluster->refill_lock);
8411 btrfs_return_cluster_to_free_space(block_group, cluster);
8412 spin_unlock(&cluster->refill_lock);
8413
8414 /*
8415 * make sure this block group isn't part of a metadata
8416 * allocation cluster
8417 */
8418 cluster = &root->fs_info->meta_alloc_cluster;
8419 spin_lock(&cluster->refill_lock);
8420 btrfs_return_cluster_to_free_space(block_group, cluster);
8421 spin_unlock(&cluster->refill_lock);
8422
Zheng Yan1a40e232008-09-26 10:09:34 -04008423 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008424 if (!path) {
8425 ret = -ENOMEM;
8426 goto out;
8427 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008428
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008429 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008430 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008431 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008432 if (ret) {
8433 btrfs_add_delayed_iput(inode);
8434 goto out;
8435 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008436 clear_nlink(inode);
8437 /* One for the block groups ref */
8438 spin_lock(&block_group->lock);
8439 if (block_group->iref) {
8440 block_group->iref = 0;
8441 block_group->inode = NULL;
8442 spin_unlock(&block_group->lock);
8443 iput(inode);
8444 } else {
8445 spin_unlock(&block_group->lock);
8446 }
8447 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008448 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008449 }
8450
8451 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8452 key.offset = block_group->key.objectid;
8453 key.type = 0;
8454
8455 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8456 if (ret < 0)
8457 goto out;
8458 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008459 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008460 if (ret == 0) {
8461 ret = btrfs_del_item(trans, tree_root, path);
8462 if (ret)
8463 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008464 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008465 }
8466
Yan Zheng3dfdb932009-01-21 10:49:16 -05008467 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008468 rb_erase(&block_group->cache_node,
8469 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008470
8471 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8472 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008473 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008474
Josef Bacik80eb2342008-10-29 14:49:05 -04008475 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008476 /*
8477 * we must use list_del_init so people can check to see if they
8478 * are still on the list after taking the semaphore
8479 */
8480 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008481 if (list_empty(&block_group->space_info->block_groups[index]))
8482 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008483 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008484
Josef Bacik817d52f2009-07-13 21:29:25 -04008485 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008486 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008487
8488 btrfs_remove_free_space_cache(block_group);
8489
Yan Zhengc146afa2008-11-12 14:34:12 -05008490 spin_lock(&block_group->space_info->lock);
8491 block_group->space_info->total_bytes -= block_group->key.offset;
8492 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008493 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008494 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008495
Josef Bacik0af3d002010-06-21 14:48:16 -04008496 memcpy(&key, &block_group->key, sizeof(key));
8497
Chris Mason283bb192009-07-24 16:30:55 -04008498 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008499
Chris Masonfa9c0d792009-04-03 09:47:43 -04008500 btrfs_put_block_group(block_group);
8501 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008502
8503 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8504 if (ret > 0)
8505 ret = -EIO;
8506 if (ret < 0)
8507 goto out;
8508
8509 ret = btrfs_del_item(trans, root, path);
8510out:
8511 btrfs_free_path(path);
8512 return ret;
8513}
liuboacce9522011-01-06 19:30:25 +08008514
liuboc59021f2011-03-07 02:13:14 +00008515int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8516{
8517 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008518 struct btrfs_super_block *disk_super;
8519 u64 features;
8520 u64 flags;
8521 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008522 int ret;
8523
David Sterba6c417612011-04-13 15:41:04 +02008524 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008525 if (!btrfs_super_root(disk_super))
8526 return 1;
liuboc59021f2011-03-07 02:13:14 +00008527
liubo1aba86d2011-04-08 08:44:37 +00008528 features = btrfs_super_incompat_flags(disk_super);
8529 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8530 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008531
liubo1aba86d2011-04-08 08:44:37 +00008532 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8533 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008534 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008535 goto out;
liuboc59021f2011-03-07 02:13:14 +00008536
liubo1aba86d2011-04-08 08:44:37 +00008537 if (mixed) {
8538 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8539 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8540 } else {
8541 flags = BTRFS_BLOCK_GROUP_METADATA;
8542 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8543 if (ret)
8544 goto out;
8545
8546 flags = BTRFS_BLOCK_GROUP_DATA;
8547 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8548 }
8549out:
liuboc59021f2011-03-07 02:13:14 +00008550 return ret;
8551}
8552
liuboacce9522011-01-06 19:30:25 +08008553int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8554{
8555 return unpin_extent_range(root, start, end);
8556}
8557
8558int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008559 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008560{
Li Dongyang5378e602011-03-24 10:24:27 +00008561 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008562}
Li Dongyangf7039b12011-03-24 10:24:28 +00008563
8564int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8565{
8566 struct btrfs_fs_info *fs_info = root->fs_info;
8567 struct btrfs_block_group_cache *cache = NULL;
8568 u64 group_trimmed;
8569 u64 start;
8570 u64 end;
8571 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008572 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008573 int ret = 0;
8574
Liu Bo2cac13e2012-02-09 18:17:41 +08008575 /*
8576 * try to trim all FS space, our block group may start from non-zero.
8577 */
8578 if (range->len == total_bytes)
8579 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8580 else
8581 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008582
8583 while (cache) {
8584 if (cache->key.objectid >= (range->start + range->len)) {
8585 btrfs_put_block_group(cache);
8586 break;
8587 }
8588
8589 start = max(range->start, cache->key.objectid);
8590 end = min(range->start + range->len,
8591 cache->key.objectid + cache->key.offset);
8592
8593 if (end - start >= range->minlen) {
8594 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008595 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008596 if (!ret)
8597 wait_block_group_cache_done(cache);
8598 }
8599 ret = btrfs_trim_block_group(cache,
8600 &group_trimmed,
8601 start,
8602 end,
8603 range->minlen);
8604
8605 trimmed += group_trimmed;
8606 if (ret) {
8607 btrfs_put_block_group(cache);
8608 break;
8609 }
8610 }
8611
8612 cache = next_block_group(fs_info->tree_root, cache);
8613 }
8614
8615 range->len = trimmed;
8616 return ret;
8617}