blob: 1818dd90c27ea00583ebc76919c2de59d19b72f3 [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"
Chris Mason925baed2008-06-25 16:01:30 -040034#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040035#include "free-space-cache.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060036#include "math.h"
Chris Masonfec577f2007-02-26 10:40:21 -050037
Arne Jansen709c0482011-09-12 12:22:57 +020038#undef SCRAMBLE_DELAYED_REFS
39
Miao Xie9e622d62012-01-26 15:01:12 -050040/*
41 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040042 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
43 * if we really need one.
44 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040045 * CHUNK_ALLOC_LIMITED means to only try and allocate one
46 * if we have very few chunks already allocated. This is
47 * used as part of the clustering code to help make sure
48 * we have a good pool of storage to cluster in, without
49 * filling the FS with empty chunks
50 *
Miao Xie9e622d62012-01-26 15:01:12 -050051 * CHUNK_ALLOC_FORCE means it must try to allocate one
52 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040053 */
54enum {
55 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050056 CHUNK_ALLOC_LIMITED = 1,
57 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040058};
59
Josef Bacikfb25e912011-07-26 17:00:46 -040060/*
61 * Control how reservations are dealt with.
62 *
63 * RESERVE_FREE - freeing a reservation.
64 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
65 * ENOSPC accounting
66 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
67 * bytes_may_use as the ENOSPC accounting is done elsewhere
68 */
69enum {
70 RESERVE_FREE = 0,
71 RESERVE_ALLOC = 1,
72 RESERVE_ALLOC_NO_ACCOUNT = 2,
73};
74
Liu Boc53d6132012-12-27 09:01:19 +000075static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040076 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040077static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
78 struct btrfs_root *root,
79 u64 bytenr, u64 num_bytes, u64 parent,
80 u64 root_objectid, u64 owner_objectid,
81 u64 owner_offset, int refs_to_drop,
82 struct btrfs_delayed_extent_op *extra_op);
83static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
84 struct extent_buffer *leaf,
85 struct btrfs_extent_item *ei);
86static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
87 struct btrfs_root *root,
88 u64 parent, u64 root_objectid,
89 u64 flags, u64 owner, u64 offset,
90 struct btrfs_key *ins, int ref_mod);
91static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
92 struct btrfs_root *root,
93 u64 parent, u64 root_objectid,
94 u64 flags, struct btrfs_disk_key *key,
95 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050096static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -040097 struct btrfs_root *extent_root, u64 flags,
98 int force);
Yan Zheng11833d62009-09-11 16:11:19 -040099static int find_next_key(struct btrfs_path *path, int level,
100 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400101static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
102 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400103static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
104 u64 num_bytes, int reserve);
Josef Bacik5d803662013-02-07 16:06:02 -0500105static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
106 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500107
Josef Bacik817d52f2009-07-13 21:29:25 -0400108static noinline int
109block_group_cache_done(struct btrfs_block_group_cache *cache)
110{
111 smp_mb();
112 return cache->cached == BTRFS_CACHE_FINISHED;
113}
114
Josef Bacik0f9dd462008-09-23 13:14:11 -0400115static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
116{
117 return (cache->flags & bits) == bits;
118}
119
David Sterba62a45b62011-04-20 15:52:26 +0200120static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000121{
122 atomic_inc(&cache->count);
123}
124
125void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
126{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400127 if (atomic_dec_and_test(&cache->count)) {
128 WARN_ON(cache->pinned > 0);
129 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800130 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000131 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400132 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000133}
134
Josef Bacik0f9dd462008-09-23 13:14:11 -0400135/*
136 * this adds the block group to the fs_info rb tree for the block group
137 * cache
138 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500139static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400140 struct btrfs_block_group_cache *block_group)
141{
142 struct rb_node **p;
143 struct rb_node *parent = NULL;
144 struct btrfs_block_group_cache *cache;
145
146 spin_lock(&info->block_group_cache_lock);
147 p = &info->block_group_cache_tree.rb_node;
148
149 while (*p) {
150 parent = *p;
151 cache = rb_entry(parent, struct btrfs_block_group_cache,
152 cache_node);
153 if (block_group->key.objectid < cache->key.objectid) {
154 p = &(*p)->rb_left;
155 } else if (block_group->key.objectid > cache->key.objectid) {
156 p = &(*p)->rb_right;
157 } else {
158 spin_unlock(&info->block_group_cache_lock);
159 return -EEXIST;
160 }
161 }
162
163 rb_link_node(&block_group->cache_node, parent, p);
164 rb_insert_color(&block_group->cache_node,
165 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000166
167 if (info->first_logical_byte > block_group->key.objectid)
168 info->first_logical_byte = block_group->key.objectid;
169
Josef Bacik0f9dd462008-09-23 13:14:11 -0400170 spin_unlock(&info->block_group_cache_lock);
171
172 return 0;
173}
174
175/*
176 * This will return the block group at or after bytenr if contains is 0, else
177 * it will return the block group that contains the bytenr
178 */
179static struct btrfs_block_group_cache *
180block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
181 int contains)
182{
183 struct btrfs_block_group_cache *cache, *ret = NULL;
184 struct rb_node *n;
185 u64 end, start;
186
187 spin_lock(&info->block_group_cache_lock);
188 n = info->block_group_cache_tree.rb_node;
189
190 while (n) {
191 cache = rb_entry(n, struct btrfs_block_group_cache,
192 cache_node);
193 end = cache->key.objectid + cache->key.offset - 1;
194 start = cache->key.objectid;
195
196 if (bytenr < start) {
197 if (!contains && (!ret || start < ret->key.objectid))
198 ret = cache;
199 n = n->rb_left;
200 } else if (bytenr > start) {
201 if (contains && bytenr <= end) {
202 ret = cache;
203 break;
204 }
205 n = n->rb_right;
206 } else {
207 ret = cache;
208 break;
209 }
210 }
Liu Boa1897fd2012-12-27 09:01:23 +0000211 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000212 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000213 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
214 info->first_logical_byte = ret->key.objectid;
215 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400216 spin_unlock(&info->block_group_cache_lock);
217
218 return ret;
219}
220
Yan Zheng11833d62009-09-11 16:11:19 -0400221static int add_excluded_extent(struct btrfs_root *root,
222 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400223{
Yan Zheng11833d62009-09-11 16:11:19 -0400224 u64 end = start + num_bytes - 1;
225 set_extent_bits(&root->fs_info->freed_extents[0],
226 start, end, EXTENT_UPTODATE, GFP_NOFS);
227 set_extent_bits(&root->fs_info->freed_extents[1],
228 start, end, EXTENT_UPTODATE, GFP_NOFS);
229 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400230}
231
Yan Zheng11833d62009-09-11 16:11:19 -0400232static void free_excluded_extents(struct btrfs_root *root,
233 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400234{
Yan Zheng11833d62009-09-11 16:11:19 -0400235 u64 start, end;
236
237 start = cache->key.objectid;
238 end = start + cache->key.offset - 1;
239
240 clear_extent_bits(&root->fs_info->freed_extents[0],
241 start, end, EXTENT_UPTODATE, GFP_NOFS);
242 clear_extent_bits(&root->fs_info->freed_extents[1],
243 start, end, EXTENT_UPTODATE, GFP_NOFS);
244}
245
246static int exclude_super_stripes(struct btrfs_root *root,
247 struct btrfs_block_group_cache *cache)
248{
Josef Bacik817d52f2009-07-13 21:29:25 -0400249 u64 bytenr;
250 u64 *logical;
251 int stripe_len;
252 int i, nr, ret;
253
Yan, Zheng06b23312009-11-26 09:31:11 +0000254 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
255 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
256 cache->bytes_super += stripe_len;
257 ret = add_excluded_extent(root, cache->key.objectid,
258 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100259 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng06b23312009-11-26 09:31:11 +0000260 }
261
Josef Bacik817d52f2009-07-13 21:29:25 -0400262 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
263 bytenr = btrfs_sb_offset(i);
264 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
265 cache->key.objectid, bytenr,
266 0, &logical, &nr, &stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -0400268
Josef Bacik817d52f2009-07-13 21:29:25 -0400269 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400270 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400271 ret = add_excluded_extent(root, logical[nr],
272 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100273 BUG_ON(ret); /* -ENOMEM */
Josef Bacik817d52f2009-07-13 21:29:25 -0400274 }
Yan Zheng11833d62009-09-11 16:11:19 -0400275
Josef Bacik817d52f2009-07-13 21:29:25 -0400276 kfree(logical);
277 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400278 return 0;
279}
280
Yan Zheng11833d62009-09-11 16:11:19 -0400281static struct btrfs_caching_control *
282get_caching_control(struct btrfs_block_group_cache *cache)
283{
284 struct btrfs_caching_control *ctl;
285
286 spin_lock(&cache->lock);
287 if (cache->cached != BTRFS_CACHE_STARTED) {
288 spin_unlock(&cache->lock);
289 return NULL;
290 }
291
Josef Bacikdde5abe2010-09-16 16:17:03 -0400292 /* We're loading it the fast way, so we don't have a caching_ctl. */
293 if (!cache->caching_ctl) {
294 spin_unlock(&cache->lock);
295 return NULL;
296 }
297
Yan Zheng11833d62009-09-11 16:11:19 -0400298 ctl = cache->caching_ctl;
299 atomic_inc(&ctl->count);
300 spin_unlock(&cache->lock);
301 return ctl;
302}
303
304static void put_caching_control(struct btrfs_caching_control *ctl)
305{
306 if (atomic_dec_and_test(&ctl->count))
307 kfree(ctl);
308}
309
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310/*
311 * this is only called by cache_block_group, since we could have freed extents
312 * we need to check the pinned_extents for any extents that can't be used yet
313 * since their free space will be released as soon as the transaction commits.
314 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400315static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 struct btrfs_fs_info *info, u64 start, u64 end)
317{
Josef Bacik817d52f2009-07-13 21:29:25 -0400318 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400319 int ret;
320
321 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400322 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400323 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400324 EXTENT_DIRTY | EXTENT_UPTODATE,
325 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400326 if (ret)
327 break;
328
Yan, Zheng06b23312009-11-26 09:31:11 +0000329 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400330 start = extent_end + 1;
331 } else if (extent_start > start && extent_start < end) {
332 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400333 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500334 ret = btrfs_add_free_space(block_group, start,
335 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100336 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337 start = extent_end + 1;
338 } else {
339 break;
340 }
341 }
342
343 if (start < end) {
344 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400345 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500346 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100347 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400348 }
349
Josef Bacik817d52f2009-07-13 21:29:25 -0400350 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400351}
352
Josef Bacikbab39bf2011-06-30 14:42:28 -0400353static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400354{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400355 struct btrfs_block_group_cache *block_group;
356 struct btrfs_fs_info *fs_info;
357 struct btrfs_caching_control *caching_ctl;
358 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400359 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400360 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400361 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400362 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400363 u64 last = 0;
364 u32 nritems;
365 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400366
Josef Bacikbab39bf2011-06-30 14:42:28 -0400367 caching_ctl = container_of(work, struct btrfs_caching_control, work);
368 block_group = caching_ctl->block_group;
369 fs_info = block_group->fs_info;
370 extent_root = fs_info->extent_root;
371
Chris Masone37c9e62007-05-09 20:13:14 -0400372 path = btrfs_alloc_path();
373 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400374 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400375
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400377
Chris Mason5cd57b22008-06-25 16:01:30 -0400378 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400379 * We don't want to deadlock with somebody trying to allocate a new
380 * extent for the extent root while also trying to search the extent
381 * root to add free space. So we skip locking and search the commit
382 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400383 */
384 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400385 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400386 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400387
Yan Zhenge4404d62008-12-12 10:03:26 -0500388 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400389 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400390 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400391again:
Yan Zheng11833d62009-09-11 16:11:19 -0400392 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400393 /* need to make sure the commit_root doesn't disappear */
394 down_read(&fs_info->extent_commit_sem);
395
Yan Zheng11833d62009-09-11 16:11:19 -0400396 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400397 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400398 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500399
Yan Zheng11833d62009-09-11 16:11:19 -0400400 leaf = path->nodes[0];
401 nritems = btrfs_header_nritems(leaf);
402
Chris Masond3977122009-01-05 21:25:51 -0500403 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200404 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400405 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400406 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400407 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400408
Yan Zheng11833d62009-09-11 16:11:19 -0400409 if (path->slots[0] < nritems) {
410 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
411 } else {
412 ret = find_next_key(path, 0, &key);
413 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400414 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400415
Josef Bacik589d8ad2011-05-11 17:30:53 -0400416 if (need_resched() ||
417 btrfs_next_leaf(extent_root, path)) {
418 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400419 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400420 up_read(&fs_info->extent_commit_sem);
421 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400422 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400423 goto again;
424 }
425 leaf = path->nodes[0];
426 nritems = btrfs_header_nritems(leaf);
427 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400428 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400429
Yan Zheng11833d62009-09-11 16:11:19 -0400430 if (key.objectid < block_group->key.objectid) {
431 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400432 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400433 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400434
Chris Masone37c9e62007-05-09 20:13:14 -0400435 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400436 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400437 break;
Yan7d7d6062007-09-14 16:15:28 -0400438
Yan Zheng11833d62009-09-11 16:11:19 -0400439 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400440 total_found += add_new_free_space(block_group,
441 fs_info, last,
442 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400443 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400444
Yan Zheng11833d62009-09-11 16:11:19 -0400445 if (total_found > (1024 * 1024 * 2)) {
446 total_found = 0;
447 wake_up(&caching_ctl->wait);
448 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400449 }
Chris Masone37c9e62007-05-09 20:13:14 -0400450 path->slots[0]++;
451 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400452 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400453
454 total_found += add_new_free_space(block_group, fs_info, last,
455 block_group->key.objectid +
456 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400457 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400458
459 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400460 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400461 block_group->cached = BTRFS_CACHE_FINISHED;
462 spin_unlock(&block_group->lock);
463
Chris Mason54aa1f42007-06-22 14:16:25 -0400464err:
Chris Masone37c9e62007-05-09 20:13:14 -0400465 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400466 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400467
Yan Zheng11833d62009-09-11 16:11:19 -0400468 free_excluded_extents(extent_root, block_group);
469
470 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400471out:
Yan Zheng11833d62009-09-11 16:11:19 -0400472 wake_up(&caching_ctl->wait);
473
474 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000475 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400476}
477
Josef Bacik9d66e232010-08-25 16:54:15 -0400478static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400479 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400480{
Josef Bacik291c7d22011-11-14 13:52:14 -0500481 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400482 struct btrfs_fs_info *fs_info = cache->fs_info;
483 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400484 int ret = 0;
485
Josef Bacik291c7d22011-11-14 13:52:14 -0500486 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100487 if (!caching_ctl)
488 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500489
490 INIT_LIST_HEAD(&caching_ctl->list);
491 mutex_init(&caching_ctl->mutex);
492 init_waitqueue_head(&caching_ctl->wait);
493 caching_ctl->block_group = cache;
494 caching_ctl->progress = cache->key.objectid;
495 atomic_set(&caching_ctl->count, 1);
496 caching_ctl->work.func = caching_thread;
497
498 spin_lock(&cache->lock);
499 /*
500 * This should be a rare occasion, but this could happen I think in the
501 * case where one thread starts to load the space cache info, and then
502 * some other thread starts a transaction commit which tries to do an
503 * allocation while the other thread is still loading the space cache
504 * info. The previous loop should have kept us from choosing this block
505 * group, but if we've moved to the state where we will wait on caching
506 * block groups we need to first check if we're doing a fast load here,
507 * so we can wait for it to finish, otherwise we could end up allocating
508 * from a block group who's cache gets evicted for one reason or
509 * another.
510 */
511 while (cache->cached == BTRFS_CACHE_FAST) {
512 struct btrfs_caching_control *ctl;
513
514 ctl = cache->caching_ctl;
515 atomic_inc(&ctl->count);
516 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
517 spin_unlock(&cache->lock);
518
519 schedule();
520
521 finish_wait(&ctl->wait, &wait);
522 put_caching_control(ctl);
523 spin_lock(&cache->lock);
524 }
525
526 if (cache->cached != BTRFS_CACHE_NO) {
527 spin_unlock(&cache->lock);
528 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400529 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500530 }
531 WARN_ON(cache->caching_ctl);
532 cache->caching_ctl = caching_ctl;
533 cache->cached = BTRFS_CACHE_FAST;
534 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400535
Josef Bacikd53ba472012-04-12 16:03:57 -0400536 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400537 ret = load_free_space_cache(fs_info, cache);
538
539 spin_lock(&cache->lock);
540 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500541 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400542 cache->cached = BTRFS_CACHE_FINISHED;
543 cache->last_byte_to_unpin = (u64)-1;
544 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500545 if (load_cache_only) {
546 cache->caching_ctl = NULL;
547 cache->cached = BTRFS_CACHE_NO;
548 } else {
549 cache->cached = BTRFS_CACHE_STARTED;
550 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400551 }
552 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500553 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000554 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500555 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000556 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400557 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000558 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500559 } else {
560 /*
561 * We are not going to do the fast caching, set cached to the
562 * appropriate value and wakeup any waiters.
563 */
564 spin_lock(&cache->lock);
565 if (load_cache_only) {
566 cache->caching_ctl = NULL;
567 cache->cached = BTRFS_CACHE_NO;
568 } else {
569 cache->cached = BTRFS_CACHE_STARTED;
570 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400571 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500572 wake_up(&caching_ctl->wait);
573 }
574
575 if (load_cache_only) {
576 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400577 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400578 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400579
Yan Zheng11833d62009-09-11 16:11:19 -0400580 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500581 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400582 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
583 up_write(&fs_info->extent_commit_sem);
584
Josef Bacik11dfe352009-11-13 20:12:59 +0000585 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400586
Josef Bacikbab39bf2011-06-30 14:42:28 -0400587 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400588
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400589 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400590}
591
Josef Bacik0f9dd462008-09-23 13:14:11 -0400592/*
593 * return the block group that starts at or after bytenr
594 */
Chris Masond3977122009-01-05 21:25:51 -0500595static struct btrfs_block_group_cache *
596btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400597{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400598 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400599
Josef Bacik0f9dd462008-09-23 13:14:11 -0400600 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400601
Josef Bacik0f9dd462008-09-23 13:14:11 -0400602 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400603}
604
Josef Bacik0f9dd462008-09-23 13:14:11 -0400605/*
Sankar P9f556842009-05-14 13:52:22 -0400606 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400607 */
Chris Masond3977122009-01-05 21:25:51 -0500608struct btrfs_block_group_cache *btrfs_lookup_block_group(
609 struct btrfs_fs_info *info,
610 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400611{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400612 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400613
Josef Bacik0f9dd462008-09-23 13:14:11 -0400614 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400615
Josef Bacik0f9dd462008-09-23 13:14:11 -0400616 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400617}
Chris Mason0b86a832008-03-24 15:01:56 -0400618
Josef Bacik0f9dd462008-09-23 13:14:11 -0400619static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
620 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400621{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400622 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400623 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400624
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200625 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400626
Chris Mason4184ea72009-03-10 12:39:20 -0400627 rcu_read_lock();
628 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400629 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400630 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400631 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400632 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400633 }
Chris Mason4184ea72009-03-10 12:39:20 -0400634 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400635 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400636}
637
Chris Mason4184ea72009-03-10 12:39:20 -0400638/*
639 * after adding space to the filesystem, we need to clear the full flags
640 * on all the space infos.
641 */
642void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
643{
644 struct list_head *head = &info->space_info;
645 struct btrfs_space_info *found;
646
647 rcu_read_lock();
648 list_for_each_entry_rcu(found, head, list)
649 found->full = 0;
650 rcu_read_unlock();
651}
652
Yan Zhengd2fb3432008-12-11 16:30:39 -0500653u64 btrfs_find_block_group(struct btrfs_root *root,
654 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400655{
Chris Mason96b51792007-10-15 16:15:19 -0400656 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400657 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500658 u64 last = max(search_hint, search_start);
659 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400660 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500661 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400662 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400663again:
Zheng Yane8569812008-09-26 10:05:48 -0400664 while (1) {
665 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400666 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400667 break;
Chris Mason96b51792007-10-15 16:15:19 -0400668
Chris Masonc286ac42008-07-22 23:06:41 -0400669 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400670 last = cache->key.objectid + cache->key.offset;
671 used = btrfs_block_group_used(&cache->item);
672
Yan Zhengd2fb3432008-12-11 16:30:39 -0500673 if ((full_search || !cache->ro) &&
674 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400675 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500676 div_factor(cache->key.offset, factor)) {
677 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400678 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400679 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400680 goto found;
681 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400682 }
Chris Masonc286ac42008-07-22 23:06:41 -0400683 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400684 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400685 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400686 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400687 if (!wrapped) {
688 last = search_start;
689 wrapped = 1;
690 goto again;
691 }
692 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400693 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400694 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400695 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400696 goto again;
697 }
Chris Masonbe744172007-05-06 10:15:01 -0400698found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500699 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400700}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400701
Chris Masone02119d2008-09-05 16:13:11 -0400702/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400703int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400704{
705 int ret;
706 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400707 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400708
Zheng Yan31840ae2008-09-23 13:14:14 -0400709 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700710 if (!path)
711 return -ENOMEM;
712
Chris Masone02119d2008-09-05 16:13:11 -0400713 key.objectid = start;
714 key.offset = len;
715 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
716 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
717 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400718 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500719 return ret;
720}
721
Chris Masond8d5f3e2007-12-11 12:42:00 -0500722/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400723 * helper function to lookup reference count and flags of extent.
724 *
725 * the head node for delayed ref is used to store the sum of all the
726 * reference count modifications queued up in the rbtree. the head
727 * node may also store the extent flags to set. This way you can check
728 * to see what the reference count and extent flags would be if all of
729 * the delayed refs are not processed.
730 */
731int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
732 struct btrfs_root *root, u64 bytenr,
733 u64 num_bytes, u64 *refs, u64 *flags)
734{
735 struct btrfs_delayed_ref_head *head;
736 struct btrfs_delayed_ref_root *delayed_refs;
737 struct btrfs_path *path;
738 struct btrfs_extent_item *ei;
739 struct extent_buffer *leaf;
740 struct btrfs_key key;
741 u32 item_size;
742 u64 num_refs;
743 u64 extent_flags;
744 int ret;
745
746 path = btrfs_alloc_path();
747 if (!path)
748 return -ENOMEM;
749
750 key.objectid = bytenr;
751 key.type = BTRFS_EXTENT_ITEM_KEY;
752 key.offset = num_bytes;
753 if (!trans) {
754 path->skip_locking = 1;
755 path->search_commit_root = 1;
756 }
757again:
758 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
759 &key, path, 0, 0);
760 if (ret < 0)
761 goto out_free;
762
763 if (ret == 0) {
764 leaf = path->nodes[0];
765 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
766 if (item_size >= sizeof(*ei)) {
767 ei = btrfs_item_ptr(leaf, path->slots[0],
768 struct btrfs_extent_item);
769 num_refs = btrfs_extent_refs(leaf, ei);
770 extent_flags = btrfs_extent_flags(leaf, ei);
771 } else {
772#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
773 struct btrfs_extent_item_v0 *ei0;
774 BUG_ON(item_size != sizeof(*ei0));
775 ei0 = btrfs_item_ptr(leaf, path->slots[0],
776 struct btrfs_extent_item_v0);
777 num_refs = btrfs_extent_refs_v0(leaf, ei0);
778 /* FIXME: this isn't correct for data */
779 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
780#else
781 BUG();
782#endif
783 }
784 BUG_ON(num_refs == 0);
785 } else {
786 num_refs = 0;
787 extent_flags = 0;
788 ret = 0;
789 }
790
791 if (!trans)
792 goto out;
793
794 delayed_refs = &trans->transaction->delayed_refs;
795 spin_lock(&delayed_refs->lock);
796 head = btrfs_find_delayed_ref_head(trans, bytenr);
797 if (head) {
798 if (!mutex_trylock(&head->mutex)) {
799 atomic_inc(&head->node.refs);
800 spin_unlock(&delayed_refs->lock);
801
David Sterbab3b4aa72011-04-21 01:20:15 +0200802 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400803
David Sterba8cc33e52011-05-02 15:29:25 +0200804 /*
805 * Mutex was contended, block until it's released and try
806 * again
807 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400808 mutex_lock(&head->mutex);
809 mutex_unlock(&head->mutex);
810 btrfs_put_delayed_ref(&head->node);
811 goto again;
812 }
813 if (head->extent_op && head->extent_op->update_flags)
814 extent_flags |= head->extent_op->flags_to_set;
815 else
816 BUG_ON(num_refs == 0);
817
818 num_refs += head->node.ref_mod;
819 mutex_unlock(&head->mutex);
820 }
821 spin_unlock(&delayed_refs->lock);
822out:
823 WARN_ON(num_refs == 0);
824 if (refs)
825 *refs = num_refs;
826 if (flags)
827 *flags = extent_flags;
828out_free:
829 btrfs_free_path(path);
830 return ret;
831}
832
833/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500834 * Back reference rules. Back refs have three main goals:
835 *
836 * 1) differentiate between all holders of references to an extent so that
837 * when a reference is dropped we can make sure it was a valid reference
838 * before freeing the extent.
839 *
840 * 2) Provide enough information to quickly find the holders of an extent
841 * if we notice a given block is corrupted or bad.
842 *
843 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
844 * maintenance. This is actually the same as #2, but with a slightly
845 * different use case.
846 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 * There are two kinds of back refs. The implicit back refs is optimized
848 * for pointers in non-shared tree blocks. For a given pointer in a block,
849 * back refs of this kind provide information about the block's owner tree
850 * and the pointer's key. These information allow us to find the block by
851 * b-tree searching. The full back refs is for pointers in tree blocks not
852 * referenced by their owner trees. The location of tree block is recorded
853 * in the back refs. Actually the full back refs is generic, and can be
854 * used in all cases the implicit back refs is used. The major shortcoming
855 * of the full back refs is its overhead. Every time a tree block gets
856 * COWed, we have to update back refs entry for all pointers in it.
857 *
858 * For a newly allocated tree block, we use implicit back refs for
859 * pointers in it. This means most tree related operations only involve
860 * implicit back refs. For a tree block created in old transaction, the
861 * only way to drop a reference to it is COW it. So we can detect the
862 * event that tree block loses its owner tree's reference and do the
863 * back refs conversion.
864 *
865 * When a tree block is COW'd through a tree, there are four cases:
866 *
867 * The reference count of the block is one and the tree is the block's
868 * owner tree. Nothing to do in this case.
869 *
870 * The reference count of the block is one and the tree is not the
871 * block's owner tree. In this case, full back refs is used for pointers
872 * in the block. Remove these full back refs, add implicit back refs for
873 * every pointers in the new block.
874 *
875 * The reference count of the block is greater than one and the tree is
876 * the block's owner tree. In this case, implicit back refs is used for
877 * pointers in the block. Add full back refs for every pointers in the
878 * block, increase lower level extents' reference counts. The original
879 * implicit back refs are entailed to the new block.
880 *
881 * The reference count of the block is greater than one and the tree is
882 * not the block's owner tree. Add implicit back refs for every pointer in
883 * the new block, increase lower level extents' reference count.
884 *
885 * Back Reference Key composing:
886 *
887 * The key objectid corresponds to the first byte in the extent,
888 * The key type is used to differentiate between types of back refs.
889 * There are different meanings of the key offset for different types
890 * of back refs.
891 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500892 * File extents can be referenced by:
893 *
894 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400895 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500896 * - different offsets inside a file (bookend extents in file.c)
897 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400898 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500899 *
900 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500901 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 * - original offset in the file
903 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400904 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400905 * The key offset for the implicit back refs is hash of the first
906 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500907 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500909 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500911 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400912 * The key offset for the implicit back refs is the first byte of
913 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500914 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 * When a file extent is allocated, The implicit back refs is used.
916 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500917 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 * (root_key.objectid, inode objectid, offset in file, 1)
919 *
920 * When a file extent is removed file truncation, we find the
921 * corresponding implicit back refs and check the following fields:
922 *
923 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500924 *
925 * Btree extents can be referenced by:
926 *
927 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500928 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929 * Both the implicit back refs and the full back refs for tree blocks
930 * only consist of key. The key offset for the implicit back refs is
931 * objectid of block's owner tree. The key offset for the full back refs
932 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500933 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400934 * When implicit back refs is used, information about the lowest key and
935 * level of the tree block are required. These information are stored in
936 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500937 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400938
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400939#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
940static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
941 struct btrfs_root *root,
942 struct btrfs_path *path,
943 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500944{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 struct btrfs_extent_item *item;
946 struct btrfs_extent_item_v0 *ei0;
947 struct btrfs_extent_ref_v0 *ref0;
948 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400949 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 struct btrfs_key key;
951 struct btrfs_key found_key;
952 u32 new_size = sizeof(*item);
953 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500954 int ret;
955
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 leaf = path->nodes[0];
957 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500958
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
960 ei0 = btrfs_item_ptr(leaf, path->slots[0],
961 struct btrfs_extent_item_v0);
962 refs = btrfs_extent_refs_v0(leaf, ei0);
963
964 if (owner == (u64)-1) {
965 while (1) {
966 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
967 ret = btrfs_next_leaf(root, path);
968 if (ret < 0)
969 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971 leaf = path->nodes[0];
972 }
973 btrfs_item_key_to_cpu(leaf, &found_key,
974 path->slots[0]);
975 BUG_ON(key.objectid != found_key.objectid);
976 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
977 path->slots[0]++;
978 continue;
979 }
980 ref0 = btrfs_item_ptr(leaf, path->slots[0],
981 struct btrfs_extent_ref_v0);
982 owner = btrfs_ref_objectid_v0(leaf, ref0);
983 break;
984 }
985 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200986 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987
988 if (owner < BTRFS_FIRST_FREE_OBJECTID)
989 new_size += sizeof(*bi);
990
991 new_size -= sizeof(*ei0);
992 ret = btrfs_search_slot(trans, root, &key, path,
993 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400994 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100996 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997
Jeff Mahoney143bede2012-03-01 14:56:26 +0100998 btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999
1000 leaf = path->nodes[0];
1001 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1002 btrfs_set_extent_refs(leaf, item, refs);
1003 /* FIXME: get real generation */
1004 btrfs_set_extent_generation(leaf, item, 0);
1005 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1006 btrfs_set_extent_flags(leaf, item,
1007 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1008 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1009 bi = (struct btrfs_tree_block_info *)(item + 1);
1010 /* FIXME: get first key of the block */
1011 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1012 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1013 } else {
1014 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1015 }
1016 btrfs_mark_buffer_dirty(leaf);
1017 return 0;
1018}
1019#endif
1020
1021static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1022{
1023 u32 high_crc = ~(u32)0;
1024 u32 low_crc = ~(u32)0;
1025 __le64 lenum;
1026
1027 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001028 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001029 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001030 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001032 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001033
1034 return ((u64)high_crc << 31) ^ (u64)low_crc;
1035}
1036
1037static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1038 struct btrfs_extent_data_ref *ref)
1039{
1040 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1041 btrfs_extent_data_ref_objectid(leaf, ref),
1042 btrfs_extent_data_ref_offset(leaf, ref));
1043}
1044
1045static int match_extent_data_ref(struct extent_buffer *leaf,
1046 struct btrfs_extent_data_ref *ref,
1047 u64 root_objectid, u64 owner, u64 offset)
1048{
1049 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1050 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1051 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1052 return 0;
1053 return 1;
1054}
1055
1056static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1057 struct btrfs_root *root,
1058 struct btrfs_path *path,
1059 u64 bytenr, u64 parent,
1060 u64 root_objectid,
1061 u64 owner, u64 offset)
1062{
1063 struct btrfs_key key;
1064 struct btrfs_extent_data_ref *ref;
1065 struct extent_buffer *leaf;
1066 u32 nritems;
1067 int ret;
1068 int recow;
1069 int err = -ENOENT;
1070
1071 key.objectid = bytenr;
1072 if (parent) {
1073 key.type = BTRFS_SHARED_DATA_REF_KEY;
1074 key.offset = parent;
1075 } else {
1076 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1077 key.offset = hash_extent_data_ref(root_objectid,
1078 owner, offset);
1079 }
1080again:
1081 recow = 0;
1082 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1083 if (ret < 0) {
1084 err = ret;
1085 goto fail;
1086 }
1087
1088 if (parent) {
1089 if (!ret)
1090 return 0;
1091#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1092 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001093 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1095 if (ret < 0) {
1096 err = ret;
1097 goto fail;
1098 }
1099 if (!ret)
1100 return 0;
1101#endif
1102 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001103 }
1104
1105 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001106 nritems = btrfs_header_nritems(leaf);
1107 while (1) {
1108 if (path->slots[0] >= nritems) {
1109 ret = btrfs_next_leaf(root, path);
1110 if (ret < 0)
1111 err = ret;
1112 if (ret)
1113 goto fail;
1114
1115 leaf = path->nodes[0];
1116 nritems = btrfs_header_nritems(leaf);
1117 recow = 1;
1118 }
1119
1120 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1121 if (key.objectid != bytenr ||
1122 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1123 goto fail;
1124
1125 ref = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_extent_data_ref);
1127
1128 if (match_extent_data_ref(leaf, ref, root_objectid,
1129 owner, offset)) {
1130 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001131 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001132 goto again;
1133 }
1134 err = 0;
1135 break;
1136 }
1137 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001138 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001139fail:
1140 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001141}
1142
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001143static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1144 struct btrfs_root *root,
1145 struct btrfs_path *path,
1146 u64 bytenr, u64 parent,
1147 u64 root_objectid, u64 owner,
1148 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001149{
1150 struct btrfs_key key;
1151 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001152 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001153 u32 num_refs;
1154 int ret;
1155
1156 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 if (parent) {
1158 key.type = BTRFS_SHARED_DATA_REF_KEY;
1159 key.offset = parent;
1160 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001161 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001162 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1163 key.offset = hash_extent_data_ref(root_objectid,
1164 owner, offset);
1165 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001166 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001167
1168 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1169 if (ret && ret != -EEXIST)
1170 goto fail;
1171
1172 leaf = path->nodes[0];
1173 if (parent) {
1174 struct btrfs_shared_data_ref *ref;
1175 ref = btrfs_item_ptr(leaf, path->slots[0],
1176 struct btrfs_shared_data_ref);
1177 if (ret == 0) {
1178 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1179 } else {
1180 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1181 num_refs += refs_to_add;
1182 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1183 }
1184 } else {
1185 struct btrfs_extent_data_ref *ref;
1186 while (ret == -EEXIST) {
1187 ref = btrfs_item_ptr(leaf, path->slots[0],
1188 struct btrfs_extent_data_ref);
1189 if (match_extent_data_ref(leaf, ref, root_objectid,
1190 owner, offset))
1191 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001192 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 key.offset++;
1194 ret = btrfs_insert_empty_item(trans, root, path, &key,
1195 size);
1196 if (ret && ret != -EEXIST)
1197 goto fail;
1198
1199 leaf = path->nodes[0];
1200 }
1201 ref = btrfs_item_ptr(leaf, path->slots[0],
1202 struct btrfs_extent_data_ref);
1203 if (ret == 0) {
1204 btrfs_set_extent_data_ref_root(leaf, ref,
1205 root_objectid);
1206 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1207 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1208 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1209 } else {
1210 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1211 num_refs += refs_to_add;
1212 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1213 }
1214 }
1215 btrfs_mark_buffer_dirty(leaf);
1216 ret = 0;
1217fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001218 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001219 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001220}
1221
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1223 struct btrfs_root *root,
1224 struct btrfs_path *path,
1225 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001226{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001227 struct btrfs_key key;
1228 struct btrfs_extent_data_ref *ref1 = NULL;
1229 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001230 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001231 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001232 int ret = 0;
1233
1234 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001235 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1236
1237 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1238 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1239 struct btrfs_extent_data_ref);
1240 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1241 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1242 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1243 struct btrfs_shared_data_ref);
1244 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1245#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1246 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1247 struct btrfs_extent_ref_v0 *ref0;
1248 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1249 struct btrfs_extent_ref_v0);
1250 num_refs = btrfs_ref_count_v0(leaf, ref0);
1251#endif
1252 } else {
1253 BUG();
1254 }
1255
Chris Mason56bec292009-03-13 10:10:06 -04001256 BUG_ON(num_refs < refs_to_drop);
1257 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001258
Zheng Yan31840ae2008-09-23 13:14:14 -04001259 if (num_refs == 0) {
1260 ret = btrfs_del_item(trans, root, path);
1261 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001262 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1263 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1264 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1265 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1266#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1267 else {
1268 struct btrfs_extent_ref_v0 *ref0;
1269 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1270 struct btrfs_extent_ref_v0);
1271 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1272 }
1273#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001274 btrfs_mark_buffer_dirty(leaf);
1275 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001276 return ret;
1277}
1278
1279static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1280 struct btrfs_path *path,
1281 struct btrfs_extent_inline_ref *iref)
1282{
1283 struct btrfs_key key;
1284 struct extent_buffer *leaf;
1285 struct btrfs_extent_data_ref *ref1;
1286 struct btrfs_shared_data_ref *ref2;
1287 u32 num_refs = 0;
1288
1289 leaf = path->nodes[0];
1290 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1291 if (iref) {
1292 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1293 BTRFS_EXTENT_DATA_REF_KEY) {
1294 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1295 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1296 } else {
1297 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1298 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1299 }
1300 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1301 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1302 struct btrfs_extent_data_ref);
1303 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1304 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1305 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1306 struct btrfs_shared_data_ref);
1307 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1308#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1309 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1310 struct btrfs_extent_ref_v0 *ref0;
1311 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1312 struct btrfs_extent_ref_v0);
1313 num_refs = btrfs_ref_count_v0(leaf, ref0);
1314#endif
1315 } else {
1316 WARN_ON(1);
1317 }
1318 return num_refs;
1319}
1320
1321static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1322 struct btrfs_root *root,
1323 struct btrfs_path *path,
1324 u64 bytenr, u64 parent,
1325 u64 root_objectid)
1326{
1327 struct btrfs_key key;
1328 int ret;
1329
1330 key.objectid = bytenr;
1331 if (parent) {
1332 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1333 key.offset = parent;
1334 } else {
1335 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1336 key.offset = root_objectid;
1337 }
1338
1339 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1340 if (ret > 0)
1341 ret = -ENOENT;
1342#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1343 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001344 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001345 key.type = BTRFS_EXTENT_REF_V0_KEY;
1346 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1347 if (ret > 0)
1348 ret = -ENOENT;
1349 }
1350#endif
1351 return ret;
1352}
1353
1354static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1355 struct btrfs_root *root,
1356 struct btrfs_path *path,
1357 u64 bytenr, u64 parent,
1358 u64 root_objectid)
1359{
1360 struct btrfs_key key;
1361 int ret;
1362
1363 key.objectid = bytenr;
1364 if (parent) {
1365 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1366 key.offset = parent;
1367 } else {
1368 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1369 key.offset = root_objectid;
1370 }
1371
1372 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001373 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001374 return ret;
1375}
1376
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001377static inline int extent_ref_type(u64 parent, u64 owner)
1378{
1379 int type;
1380 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1381 if (parent > 0)
1382 type = BTRFS_SHARED_BLOCK_REF_KEY;
1383 else
1384 type = BTRFS_TREE_BLOCK_REF_KEY;
1385 } else {
1386 if (parent > 0)
1387 type = BTRFS_SHARED_DATA_REF_KEY;
1388 else
1389 type = BTRFS_EXTENT_DATA_REF_KEY;
1390 }
1391 return type;
1392}
1393
Yan Zheng2c47e6052009-06-27 21:07:35 -04001394static int find_next_key(struct btrfs_path *path, int level,
1395 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396
1397{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001398 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001399 if (!path->nodes[level])
1400 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401 if (path->slots[level] + 1 >=
1402 btrfs_header_nritems(path->nodes[level]))
1403 continue;
1404 if (level == 0)
1405 btrfs_item_key_to_cpu(path->nodes[level], key,
1406 path->slots[level] + 1);
1407 else
1408 btrfs_node_key_to_cpu(path->nodes[level], key,
1409 path->slots[level] + 1);
1410 return 0;
1411 }
1412 return 1;
1413}
1414
1415/*
1416 * look for inline back ref. if back ref is found, *ref_ret is set
1417 * to the address of inline back ref, and 0 is returned.
1418 *
1419 * if back ref isn't found, *ref_ret is set to the address where it
1420 * should be inserted, and -ENOENT is returned.
1421 *
1422 * if insert is true and there are too many inline back refs, the path
1423 * points to the extent item, and -EAGAIN is returned.
1424 *
1425 * NOTE: inline back refs are ordered in the same way that back ref
1426 * items in the tree are ordered.
1427 */
1428static noinline_for_stack
1429int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1430 struct btrfs_root *root,
1431 struct btrfs_path *path,
1432 struct btrfs_extent_inline_ref **ref_ret,
1433 u64 bytenr, u64 num_bytes,
1434 u64 parent, u64 root_objectid,
1435 u64 owner, u64 offset, int insert)
1436{
1437 struct btrfs_key key;
1438 struct extent_buffer *leaf;
1439 struct btrfs_extent_item *ei;
1440 struct btrfs_extent_inline_ref *iref;
1441 u64 flags;
1442 u64 item_size;
1443 unsigned long ptr;
1444 unsigned long end;
1445 int extra_size;
1446 int type;
1447 int want;
1448 int ret;
1449 int err = 0;
1450
1451 key.objectid = bytenr;
1452 key.type = BTRFS_EXTENT_ITEM_KEY;
1453 key.offset = num_bytes;
1454
1455 want = extent_ref_type(parent, owner);
1456 if (insert) {
1457 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001458 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001459 } else
1460 extra_size = -1;
1461 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1462 if (ret < 0) {
1463 err = ret;
1464 goto out;
1465 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 if (ret && !insert) {
1467 err = -ENOENT;
1468 goto out;
1469 }
1470 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001471
1472 leaf = path->nodes[0];
1473 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1474#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1475 if (item_size < sizeof(*ei)) {
1476 if (!insert) {
1477 err = -ENOENT;
1478 goto out;
1479 }
1480 ret = convert_extent_item_v0(trans, root, path, owner,
1481 extra_size);
1482 if (ret < 0) {
1483 err = ret;
1484 goto out;
1485 }
1486 leaf = path->nodes[0];
1487 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1488 }
1489#endif
1490 BUG_ON(item_size < sizeof(*ei));
1491
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001492 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1493 flags = btrfs_extent_flags(leaf, ei);
1494
1495 ptr = (unsigned long)(ei + 1);
1496 end = (unsigned long)ei + item_size;
1497
1498 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1499 ptr += sizeof(struct btrfs_tree_block_info);
1500 BUG_ON(ptr > end);
1501 } else {
1502 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1503 }
1504
1505 err = -ENOENT;
1506 while (1) {
1507 if (ptr >= end) {
1508 WARN_ON(ptr > end);
1509 break;
1510 }
1511 iref = (struct btrfs_extent_inline_ref *)ptr;
1512 type = btrfs_extent_inline_ref_type(leaf, iref);
1513 if (want < type)
1514 break;
1515 if (want > type) {
1516 ptr += btrfs_extent_inline_ref_size(type);
1517 continue;
1518 }
1519
1520 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1521 struct btrfs_extent_data_ref *dref;
1522 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1523 if (match_extent_data_ref(leaf, dref, root_objectid,
1524 owner, offset)) {
1525 err = 0;
1526 break;
1527 }
1528 if (hash_extent_data_ref_item(leaf, dref) <
1529 hash_extent_data_ref(root_objectid, owner, offset))
1530 break;
1531 } else {
1532 u64 ref_offset;
1533 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1534 if (parent > 0) {
1535 if (parent == ref_offset) {
1536 err = 0;
1537 break;
1538 }
1539 if (ref_offset < parent)
1540 break;
1541 } else {
1542 if (root_objectid == ref_offset) {
1543 err = 0;
1544 break;
1545 }
1546 if (ref_offset < root_objectid)
1547 break;
1548 }
1549 }
1550 ptr += btrfs_extent_inline_ref_size(type);
1551 }
1552 if (err == -ENOENT && insert) {
1553 if (item_size + extra_size >=
1554 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1555 err = -EAGAIN;
1556 goto out;
1557 }
1558 /*
1559 * To add new inline back ref, we have to make sure
1560 * there is no corresponding back ref item.
1561 * For simplicity, we just do not add new inline back
1562 * ref if there is any kind of item for this block
1563 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001564 if (find_next_key(path, 0, &key) == 0 &&
1565 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001566 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001567 err = -EAGAIN;
1568 goto out;
1569 }
1570 }
1571 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1572out:
Yan Zheng85d41982009-06-11 08:51:10 -04001573 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001574 path->keep_locks = 0;
1575 btrfs_unlock_up_safe(path, 1);
1576 }
1577 return err;
1578}
1579
1580/*
1581 * helper to add new inline back ref
1582 */
1583static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001584void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1585 struct btrfs_root *root,
1586 struct btrfs_path *path,
1587 struct btrfs_extent_inline_ref *iref,
1588 u64 parent, u64 root_objectid,
1589 u64 owner, u64 offset, int refs_to_add,
1590 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591{
1592 struct extent_buffer *leaf;
1593 struct btrfs_extent_item *ei;
1594 unsigned long ptr;
1595 unsigned long end;
1596 unsigned long item_offset;
1597 u64 refs;
1598 int size;
1599 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001600
1601 leaf = path->nodes[0];
1602 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1603 item_offset = (unsigned long)iref - (unsigned long)ei;
1604
1605 type = extent_ref_type(parent, owner);
1606 size = btrfs_extent_inline_ref_size(type);
1607
Jeff Mahoney143bede2012-03-01 14:56:26 +01001608 btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001609
1610 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1611 refs = btrfs_extent_refs(leaf, ei);
1612 refs += refs_to_add;
1613 btrfs_set_extent_refs(leaf, ei, refs);
1614 if (extent_op)
1615 __run_delayed_extent_op(extent_op, leaf, ei);
1616
1617 ptr = (unsigned long)ei + item_offset;
1618 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1619 if (ptr < end - size)
1620 memmove_extent_buffer(leaf, ptr + size, ptr,
1621 end - size - ptr);
1622
1623 iref = (struct btrfs_extent_inline_ref *)ptr;
1624 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1625 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1626 struct btrfs_extent_data_ref *dref;
1627 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1628 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1629 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1630 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1631 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1632 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1633 struct btrfs_shared_data_ref *sref;
1634 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1635 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1636 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1637 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1638 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1639 } else {
1640 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1641 }
1642 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001643}
1644
1645static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1646 struct btrfs_root *root,
1647 struct btrfs_path *path,
1648 struct btrfs_extent_inline_ref **ref_ret,
1649 u64 bytenr, u64 num_bytes, u64 parent,
1650 u64 root_objectid, u64 owner, u64 offset)
1651{
1652 int ret;
1653
1654 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1655 bytenr, num_bytes, parent,
1656 root_objectid, owner, offset, 0);
1657 if (ret != -ENOENT)
1658 return ret;
1659
David Sterbab3b4aa72011-04-21 01:20:15 +02001660 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001661 *ref_ret = NULL;
1662
1663 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1664 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1665 root_objectid);
1666 } else {
1667 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1668 root_objectid, owner, offset);
1669 }
1670 return ret;
1671}
1672
1673/*
1674 * helper to update/remove inline back ref
1675 */
1676static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001677void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1678 struct btrfs_root *root,
1679 struct btrfs_path *path,
1680 struct btrfs_extent_inline_ref *iref,
1681 int refs_to_mod,
1682 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683{
1684 struct extent_buffer *leaf;
1685 struct btrfs_extent_item *ei;
1686 struct btrfs_extent_data_ref *dref = NULL;
1687 struct btrfs_shared_data_ref *sref = NULL;
1688 unsigned long ptr;
1689 unsigned long end;
1690 u32 item_size;
1691 int size;
1692 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001693 u64 refs;
1694
1695 leaf = path->nodes[0];
1696 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1697 refs = btrfs_extent_refs(leaf, ei);
1698 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1699 refs += refs_to_mod;
1700 btrfs_set_extent_refs(leaf, ei, refs);
1701 if (extent_op)
1702 __run_delayed_extent_op(extent_op, leaf, ei);
1703
1704 type = btrfs_extent_inline_ref_type(leaf, iref);
1705
1706 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1707 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1708 refs = btrfs_extent_data_ref_count(leaf, dref);
1709 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1710 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1711 refs = btrfs_shared_data_ref_count(leaf, sref);
1712 } else {
1713 refs = 1;
1714 BUG_ON(refs_to_mod != -1);
1715 }
1716
1717 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1718 refs += refs_to_mod;
1719
1720 if (refs > 0) {
1721 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1722 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1723 else
1724 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1725 } else {
1726 size = btrfs_extent_inline_ref_size(type);
1727 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1728 ptr = (unsigned long)iref;
1729 end = (unsigned long)ei + item_size;
1730 if (ptr + size < end)
1731 memmove_extent_buffer(leaf, ptr, ptr + size,
1732 end - ptr - size);
1733 item_size -= size;
Jeff Mahoney143bede2012-03-01 14:56:26 +01001734 btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735 }
1736 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001737}
1738
1739static noinline_for_stack
1740int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1741 struct btrfs_root *root,
1742 struct btrfs_path *path,
1743 u64 bytenr, u64 num_bytes, u64 parent,
1744 u64 root_objectid, u64 owner,
1745 u64 offset, int refs_to_add,
1746 struct btrfs_delayed_extent_op *extent_op)
1747{
1748 struct btrfs_extent_inline_ref *iref;
1749 int ret;
1750
1751 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1752 bytenr, num_bytes, parent,
1753 root_objectid, owner, offset, 1);
1754 if (ret == 0) {
1755 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001756 update_inline_extent_backref(trans, root, path, iref,
1757 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001758 } else if (ret == -ENOENT) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001759 setup_inline_extent_backref(trans, root, path, iref, parent,
1760 root_objectid, owner, offset,
1761 refs_to_add, extent_op);
1762 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001763 }
1764 return ret;
1765}
1766
1767static int insert_extent_backref(struct btrfs_trans_handle *trans,
1768 struct btrfs_root *root,
1769 struct btrfs_path *path,
1770 u64 bytenr, u64 parent, u64 root_objectid,
1771 u64 owner, u64 offset, int refs_to_add)
1772{
1773 int ret;
1774 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1775 BUG_ON(refs_to_add != 1);
1776 ret = insert_tree_block_ref(trans, root, path, bytenr,
1777 parent, root_objectid);
1778 } else {
1779 ret = insert_extent_data_ref(trans, root, path, bytenr,
1780 parent, root_objectid,
1781 owner, offset, refs_to_add);
1782 }
1783 return ret;
1784}
1785
1786static int remove_extent_backref(struct btrfs_trans_handle *trans,
1787 struct btrfs_root *root,
1788 struct btrfs_path *path,
1789 struct btrfs_extent_inline_ref *iref,
1790 int refs_to_drop, int is_data)
1791{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001792 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001793
1794 BUG_ON(!is_data && refs_to_drop != 1);
1795 if (iref) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001796 update_inline_extent_backref(trans, root, path, iref,
1797 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001798 } else if (is_data) {
1799 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1800 } else {
1801 ret = btrfs_del_item(trans, root, path);
1802 }
1803 return ret;
1804}
1805
Li Dongyang5378e602011-03-24 10:24:27 +00001806static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001807 u64 start, u64 len)
1808{
Li Dongyang5378e602011-03-24 10:24:27 +00001809 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001810}
Chris Mason15916de2008-11-19 21:17:22 -05001811
Liu Hui1f3c79a2009-01-05 15:57:51 -05001812static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001813 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001814{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001815 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001816 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001817 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001818
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001819
Liu Hui1f3c79a2009-01-05 15:57:51 -05001820 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001821 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001822 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001823 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001824 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001825 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001826 int i;
1827
Liu Hui1f3c79a2009-01-05 15:57:51 -05001828
Jan Schmidta1d3c472011-08-04 17:15:33 +02001829 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001830 if (!stripe->dev->can_discard)
1831 continue;
1832
Li Dongyang5378e602011-03-24 10:24:27 +00001833 ret = btrfs_issue_discard(stripe->dev->bdev,
1834 stripe->physical,
1835 stripe->length);
1836 if (!ret)
1837 discarded_bytes += stripe->length;
1838 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001839 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001840
1841 /*
1842 * Just in case we get back EOPNOTSUPP for some reason,
1843 * just ignore the return value so we don't screw up
1844 * people calling discard_extent.
1845 */
1846 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001847 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001848 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001849 }
Li Dongyang5378e602011-03-24 10:24:27 +00001850
1851 if (actual_bytes)
1852 *actual_bytes = discarded_bytes;
1853
Liu Hui1f3c79a2009-01-05 15:57:51 -05001854
1855 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001856}
1857
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001858/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001859int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1860 struct btrfs_root *root,
1861 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001862 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001863{
1864 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001865 struct btrfs_fs_info *fs_info = root->fs_info;
1866
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001867 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1868 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001869
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001871 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1872 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001874 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001875 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001876 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1877 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001879 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001880 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001881 return ret;
1882}
1883
Chris Mason925baed2008-06-25 16:01:30 -04001884static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001885 struct btrfs_root *root,
1886 u64 bytenr, u64 num_bytes,
1887 u64 parent, u64 root_objectid,
1888 u64 owner, u64 offset, int refs_to_add,
1889 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001890{
Chris Mason5caf2a02007-04-02 11:20:42 -04001891 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001893 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 u64 refs;
1895 int ret;
1896 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001897
Chris Mason5caf2a02007-04-02 11:20:42 -04001898 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001899 if (!path)
1900 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001901
Chris Mason3c12ac72008-04-21 12:01:38 -04001902 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001903 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001904 /* this will setup the path even if it fails to insert the back ref */
1905 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1906 path, bytenr, num_bytes, parent,
1907 root_objectid, owner, offset,
1908 refs_to_add, extent_op);
1909 if (ret == 0)
1910 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001911
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001912 if (ret != -EAGAIN) {
1913 err = ret;
1914 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001915 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001916
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001917 leaf = path->nodes[0];
1918 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1919 refs = btrfs_extent_refs(leaf, item);
1920 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1921 if (extent_op)
1922 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001923
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001924 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001925 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001926
Chris Mason3c12ac72008-04-21 12:01:38 -04001927 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001928 path->leave_spinning = 1;
1929
Chris Mason56bec292009-03-13 10:10:06 -04001930 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001931 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001932 path, bytenr, parent, root_objectid,
1933 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001934 if (ret)
1935 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001936out:
Chris Mason74493f72007-12-11 09:25:06 -05001937 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001938 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001939}
1940
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001941static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1942 struct btrfs_root *root,
1943 struct btrfs_delayed_ref_node *node,
1944 struct btrfs_delayed_extent_op *extent_op,
1945 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001946{
Chris Mason56bec292009-03-13 10:10:06 -04001947 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001948 struct btrfs_delayed_data_ref *ref;
1949 struct btrfs_key ins;
1950 u64 parent = 0;
1951 u64 ref_root = 0;
1952 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001953
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001954 ins.objectid = node->bytenr;
1955 ins.offset = node->num_bytes;
1956 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001957
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001958 ref = btrfs_delayed_node_to_data_ref(node);
1959 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1960 parent = ref->parent;
1961 else
1962 ref_root = ref->root;
1963
1964 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1965 if (extent_op) {
1966 BUG_ON(extent_op->update_key);
1967 flags |= extent_op->flags_to_set;
1968 }
1969 ret = alloc_reserved_file_extent(trans, root,
1970 parent, ref_root, flags,
1971 ref->objectid, ref->offset,
1972 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001973 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1974 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1975 node->num_bytes, parent,
1976 ref_root, ref->objectid,
1977 ref->offset, node->ref_mod,
1978 extent_op);
1979 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1980 ret = __btrfs_free_extent(trans, root, node->bytenr,
1981 node->num_bytes, parent,
1982 ref_root, ref->objectid,
1983 ref->offset, node->ref_mod,
1984 extent_op);
1985 } else {
1986 BUG();
1987 }
Chris Mason56bec292009-03-13 10:10:06 -04001988 return ret;
1989}
1990
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001991static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1992 struct extent_buffer *leaf,
1993 struct btrfs_extent_item *ei)
1994{
1995 u64 flags = btrfs_extent_flags(leaf, ei);
1996 if (extent_op->update_flags) {
1997 flags |= extent_op->flags_to_set;
1998 btrfs_set_extent_flags(leaf, ei, flags);
1999 }
2000
2001 if (extent_op->update_key) {
2002 struct btrfs_tree_block_info *bi;
2003 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2004 bi = (struct btrfs_tree_block_info *)(ei + 1);
2005 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2006 }
2007}
2008
2009static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2010 struct btrfs_root *root,
2011 struct btrfs_delayed_ref_node *node,
2012 struct btrfs_delayed_extent_op *extent_op)
2013{
2014 struct btrfs_key key;
2015 struct btrfs_path *path;
2016 struct btrfs_extent_item *ei;
2017 struct extent_buffer *leaf;
2018 u32 item_size;
2019 int ret;
2020 int err = 0;
2021
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002022 if (trans->aborted)
2023 return 0;
2024
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002025 path = btrfs_alloc_path();
2026 if (!path)
2027 return -ENOMEM;
2028
2029 key.objectid = node->bytenr;
2030 key.type = BTRFS_EXTENT_ITEM_KEY;
2031 key.offset = node->num_bytes;
2032
2033 path->reada = 1;
2034 path->leave_spinning = 1;
2035 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2036 path, 0, 1);
2037 if (ret < 0) {
2038 err = ret;
2039 goto out;
2040 }
2041 if (ret > 0) {
2042 err = -EIO;
2043 goto out;
2044 }
2045
2046 leaf = path->nodes[0];
2047 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2048#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2049 if (item_size < sizeof(*ei)) {
2050 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2051 path, (u64)-1, 0);
2052 if (ret < 0) {
2053 err = ret;
2054 goto out;
2055 }
2056 leaf = path->nodes[0];
2057 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2058 }
2059#endif
2060 BUG_ON(item_size < sizeof(*ei));
2061 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2062 __run_delayed_extent_op(extent_op, leaf, ei);
2063
2064 btrfs_mark_buffer_dirty(leaf);
2065out:
2066 btrfs_free_path(path);
2067 return err;
2068}
2069
2070static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2071 struct btrfs_root *root,
2072 struct btrfs_delayed_ref_node *node,
2073 struct btrfs_delayed_extent_op *extent_op,
2074 int insert_reserved)
2075{
2076 int ret = 0;
2077 struct btrfs_delayed_tree_ref *ref;
2078 struct btrfs_key ins;
2079 u64 parent = 0;
2080 u64 ref_root = 0;
2081
2082 ins.objectid = node->bytenr;
2083 ins.offset = node->num_bytes;
2084 ins.type = BTRFS_EXTENT_ITEM_KEY;
2085
2086 ref = btrfs_delayed_node_to_tree_ref(node);
2087 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2088 parent = ref->parent;
2089 else
2090 ref_root = ref->root;
2091
2092 BUG_ON(node->ref_mod != 1);
2093 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2094 BUG_ON(!extent_op || !extent_op->update_flags ||
2095 !extent_op->update_key);
2096 ret = alloc_reserved_tree_block(trans, root,
2097 parent, ref_root,
2098 extent_op->flags_to_set,
2099 &extent_op->key,
2100 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002101 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2102 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2103 node->num_bytes, parent, ref_root,
2104 ref->level, 0, 1, extent_op);
2105 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2106 ret = __btrfs_free_extent(trans, root, node->bytenr,
2107 node->num_bytes, parent, ref_root,
2108 ref->level, 0, 1, extent_op);
2109 } else {
2110 BUG();
2111 }
2112 return ret;
2113}
2114
Chris Mason56bec292009-03-13 10:10:06 -04002115/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2117 struct btrfs_root *root,
2118 struct btrfs_delayed_ref_node *node,
2119 struct btrfs_delayed_extent_op *extent_op,
2120 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002121{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002122 int ret = 0;
2123
2124 if (trans->aborted)
2125 return 0;
2126
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002127 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002128 struct btrfs_delayed_ref_head *head;
2129 /*
2130 * we've hit the end of the chain and we were supposed
2131 * to insert this extent into the tree. But, it got
2132 * deleted before we ever needed to insert it, so all
2133 * we have to do is clean up the accounting
2134 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002135 BUG_ON(extent_op);
2136 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002137 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002138 btrfs_pin_extent(root, node->bytenr,
2139 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002140 if (head->is_data) {
2141 ret = btrfs_del_csums(trans, root,
2142 node->bytenr,
2143 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002144 }
Chris Mason56bec292009-03-13 10:10:06 -04002145 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002146 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002147 }
Josef Bacikeb099672009-02-12 09:27:38 -05002148
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002149 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2150 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2151 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2152 insert_reserved);
2153 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2154 node->type == BTRFS_SHARED_DATA_REF_KEY)
2155 ret = run_delayed_data_ref(trans, root, node, extent_op,
2156 insert_reserved);
2157 else
2158 BUG();
2159 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002160}
2161
Chris Mason56bec292009-03-13 10:10:06 -04002162static noinline struct btrfs_delayed_ref_node *
2163select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002164{
Chris Mason56bec292009-03-13 10:10:06 -04002165 struct rb_node *node;
2166 struct btrfs_delayed_ref_node *ref;
2167 int action = BTRFS_ADD_DELAYED_REF;
2168again:
2169 /*
2170 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2171 * this prevents ref count from going down to zero when
2172 * there still are pending delayed ref.
2173 */
2174 node = rb_prev(&head->node.rb_node);
2175 while (1) {
2176 if (!node)
2177 break;
2178 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2179 rb_node);
2180 if (ref->bytenr != head->node.bytenr)
2181 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002183 return ref;
2184 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 }
Chris Mason56bec292009-03-13 10:10:06 -04002186 if (action == BTRFS_ADD_DELAYED_REF) {
2187 action = BTRFS_DROP_DELAYED_REF;
2188 goto again;
2189 }
2190 return NULL;
2191}
2192
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002193/*
2194 * Returns 0 on success or if called with an already aborted transaction.
2195 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2196 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002197static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2198 struct btrfs_root *root,
2199 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002200{
Chris Mason56bec292009-03-13 10:10:06 -04002201 struct btrfs_delayed_ref_root *delayed_refs;
2202 struct btrfs_delayed_ref_node *ref;
2203 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002205 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002206 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002207 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002208 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002209
2210 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002211 while (1) {
2212 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002213 /* pick a new head ref from the cluster list */
2214 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002215 break;
Chris Mason56bec292009-03-13 10:10:06 -04002216
Chris Masonc3e69d52009-03-13 10:17:05 -04002217 locked_ref = list_entry(cluster->next,
2218 struct btrfs_delayed_ref_head, cluster);
2219
2220 /* grab the lock that says we are going to process
2221 * all the refs for this head */
2222 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2223
2224 /*
2225 * we may have dropped the spin lock to get the head
2226 * mutex lock, and that might have given someone else
2227 * time to free the head. If that's true, it has been
2228 * removed from our list and we can move on.
2229 */
2230 if (ret == -EAGAIN) {
2231 locked_ref = NULL;
2232 count++;
2233 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002234 }
2235 }
2236
2237 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002238 * We need to try and merge add/drops of the same ref since we
2239 * can run into issues with relocate dropping the implicit ref
2240 * and then it being added back again before the drop can
2241 * finish. If we merged anything we need to re-loop so we can
2242 * get a good ref.
2243 */
2244 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2245 locked_ref);
2246
2247 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002248 * locked_ref is the head node, so we have to go one
2249 * node back for any delayed ref updates
2250 */
2251 ref = select_delayed_ref(locked_ref);
2252
2253 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002254 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002255 /*
2256 * there are still refs with lower seq numbers in the
2257 * process of being added. Don't run this ref yet.
2258 */
2259 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002260 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002261 locked_ref = NULL;
2262 delayed_refs->num_heads_ready++;
2263 spin_unlock(&delayed_refs->lock);
2264 cond_resched();
2265 spin_lock(&delayed_refs->lock);
2266 continue;
2267 }
2268
2269 /*
Chris Mason56bec292009-03-13 10:10:06 -04002270 * record the must insert reserved flag before we
2271 * drop the spin lock.
2272 */
2273 must_insert_reserved = locked_ref->must_insert_reserved;
2274 locked_ref->must_insert_reserved = 0;
2275
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002276 extent_op = locked_ref->extent_op;
2277 locked_ref->extent_op = NULL;
2278
Chris Mason56bec292009-03-13 10:10:06 -04002279 if (!ref) {
2280 /* All delayed refs have been processed, Go ahead
2281 * and send the head node to run_one_delayed_ref,
2282 * so that any accounting fixes can happen
2283 */
2284 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002285
2286 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002287 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288 extent_op = NULL;
2289 }
2290
2291 if (extent_op) {
2292 spin_unlock(&delayed_refs->lock);
2293
2294 ret = run_delayed_extent_op(trans, root,
2295 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002296 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002297
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002298 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002299 printk(KERN_DEBUG
2300 "btrfs: run_delayed_extent_op "
2301 "returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002302 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002303 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002304 return ret;
2305 }
2306
Chris Mason203bf282012-01-06 15:23:57 -05002307 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002308 }
Chris Mason56bec292009-03-13 10:10:06 -04002309 }
2310
2311 ref->in_tree = 0;
2312 rb_erase(&ref->rb_node, &delayed_refs->root);
2313 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002314 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002315 /*
2316 * when we play the delayed ref, also correct the
2317 * ref_mod on head
2318 */
2319 switch (ref->action) {
2320 case BTRFS_ADD_DELAYED_REF:
2321 case BTRFS_ADD_DELAYED_EXTENT:
2322 locked_ref->node.ref_mod -= ref->ref_mod;
2323 break;
2324 case BTRFS_DROP_DELAYED_REF:
2325 locked_ref->node.ref_mod += ref->ref_mod;
2326 break;
2327 default:
2328 WARN_ON(1);
2329 }
2330 }
Chris Mason56bec292009-03-13 10:10:06 -04002331 spin_unlock(&delayed_refs->lock);
2332
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002333 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002334 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002335
Miao Xie78a61842012-11-21 02:21:28 +00002336 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002337 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002338 btrfs_delayed_ref_unlock(locked_ref);
2339 btrfs_put_delayed_ref(ref);
2340 printk(KERN_DEBUG
2341 "btrfs: run_one_delayed_ref returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002342 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002343 return ret;
2344 }
2345
Miao Xie093486c2012-12-19 08:10:10 +00002346 /*
2347 * If this node is a head, that means all the refs in this head
2348 * have been dealt with, and we will pick the next head to deal
2349 * with, so we must unlock the head and drop it from the cluster
2350 * list before we release it.
2351 */
2352 if (btrfs_delayed_ref_is_head(ref)) {
2353 list_del_init(&locked_ref->cluster);
2354 btrfs_delayed_ref_unlock(locked_ref);
2355 locked_ref = NULL;
2356 }
2357 btrfs_put_delayed_ref(ref);
2358 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002359next:
Chris Mason1887be62009-03-13 10:11:24 -04002360 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002361 spin_lock(&delayed_refs->lock);
2362 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002363 return count;
2364}
2365
Arne Jansen709c0482011-09-12 12:22:57 +02002366#ifdef SCRAMBLE_DELAYED_REFS
2367/*
2368 * Normally delayed refs get processed in ascending bytenr order. This
2369 * correlates in most cases to the order added. To expose dependencies on this
2370 * order, we start to process the tree in the middle instead of the beginning
2371 */
2372static u64 find_middle(struct rb_root *root)
2373{
2374 struct rb_node *n = root->rb_node;
2375 struct btrfs_delayed_ref_node *entry;
2376 int alt = 1;
2377 u64 middle;
2378 u64 first = 0, last = 0;
2379
2380 n = rb_first(root);
2381 if (n) {
2382 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2383 first = entry->bytenr;
2384 }
2385 n = rb_last(root);
2386 if (n) {
2387 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2388 last = entry->bytenr;
2389 }
2390 n = root->rb_node;
2391
2392 while (n) {
2393 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2394 WARN_ON(!entry->in_tree);
2395
2396 middle = entry->bytenr;
2397
2398 if (alt)
2399 n = n->rb_left;
2400 else
2401 n = n->rb_right;
2402
2403 alt = 1 - alt;
2404 }
2405 return middle;
2406}
2407#endif
2408
Arne Jansenbed92ea2012-06-28 18:03:02 +02002409int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2410 struct btrfs_fs_info *fs_info)
2411{
2412 struct qgroup_update *qgroup_update;
2413 int ret = 0;
2414
2415 if (list_empty(&trans->qgroup_ref_list) !=
2416 !trans->delayed_ref_elem.seq) {
2417 /* list without seq or seq without list */
2418 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2419 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2420 trans->delayed_ref_elem.seq);
2421 BUG();
2422 }
2423
2424 if (!trans->delayed_ref_elem.seq)
2425 return 0;
2426
2427 while (!list_empty(&trans->qgroup_ref_list)) {
2428 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2429 struct qgroup_update, list);
2430 list_del(&qgroup_update->list);
2431 if (!ret)
2432 ret = btrfs_qgroup_account_ref(
2433 trans, fs_info, qgroup_update->node,
2434 qgroup_update->extent_op);
2435 kfree(qgroup_update);
2436 }
2437
2438 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2439
2440 return ret;
2441}
2442
Chris Masonc3e69d52009-03-13 10:17:05 -04002443/*
2444 * this starts processing the delayed reference count updates and
2445 * extent insertions we have queued up so far. count can be
2446 * 0, which means to process everything in the tree at the start
2447 * of the run (but not newly added entries), or it can be some target
2448 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002449 *
2450 * Returns 0 on success or if called with an aborted transaction
2451 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002452 */
2453int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2454 struct btrfs_root *root, unsigned long count)
2455{
2456 struct rb_node *node;
2457 struct btrfs_delayed_ref_root *delayed_refs;
2458 struct btrfs_delayed_ref_node *ref;
2459 struct list_head cluster;
2460 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002461 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002462 int run_all = count == (unsigned long)-1;
2463 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002464 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002465
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002466 /* We'll clean this up in btrfs_cleanup_transaction */
2467 if (trans->aborted)
2468 return 0;
2469
Chris Masonc3e69d52009-03-13 10:17:05 -04002470 if (root == root->fs_info->extent_root)
2471 root = root->fs_info->tree_root;
2472
Jan Schmidtedf39272012-06-28 18:04:55 +02002473 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2474
Chris Masonc3e69d52009-03-13 10:17:05 -04002475 delayed_refs = &trans->transaction->delayed_refs;
2476 INIT_LIST_HEAD(&cluster);
2477again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002478 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002479 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002480
Arne Jansen709c0482011-09-12 12:22:57 +02002481#ifdef SCRAMBLE_DELAYED_REFS
2482 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2483#endif
2484
Chris Masonc3e69d52009-03-13 10:17:05 -04002485 if (count == 0) {
2486 count = delayed_refs->num_entries * 2;
2487 run_most = 1;
2488 }
2489 while (1) {
2490 if (!(run_all || run_most) &&
2491 delayed_refs->num_heads_ready < 64)
2492 break;
2493
2494 /*
2495 * go find something we can process in the rbtree. We start at
2496 * the beginning of the tree, and then build a cluster
2497 * of refs to process starting at the first one we are able to
2498 * lock
2499 */
Jan Schmidta1686502011-12-12 16:10:07 +01002500 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002501 ret = btrfs_find_ref_cluster(trans, &cluster,
2502 delayed_refs->run_delayed_start);
2503 if (ret)
2504 break;
2505
2506 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002507 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002508 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002509 spin_unlock(&delayed_refs->lock);
2510 btrfs_abort_transaction(trans, root, ret);
2511 return ret;
2512 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002513
2514 count -= min_t(unsigned long, ret, count);
2515
2516 if (count == 0)
2517 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002518
Arne Jansen1fa11e22012-08-06 14:18:51 -06002519 if (delayed_start >= delayed_refs->run_delayed_start) {
2520 if (loops == 0) {
2521 /*
2522 * btrfs_find_ref_cluster looped. let's do one
2523 * more cycle. if we don't run any delayed ref
2524 * during that cycle (because we can't because
2525 * all of them are blocked), bail out.
2526 */
2527 loops = 1;
2528 } else {
2529 /*
2530 * no runnable refs left, stop trying
2531 */
2532 BUG_ON(run_all);
2533 break;
2534 }
2535 }
2536 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002537 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002538 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002539 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002540 }
2541
Chris Mason56bec292009-03-13 10:10:06 -04002542 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002543 if (!list_empty(&trans->new_bgs)) {
2544 spin_unlock(&delayed_refs->lock);
2545 btrfs_create_pending_block_groups(trans, root);
2546 spin_lock(&delayed_refs->lock);
2547 }
2548
Chris Mason56bec292009-03-13 10:10:06 -04002549 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002550 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002551 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002552 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002553
2554 while (node) {
2555 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2556 rb_node);
2557 if (btrfs_delayed_ref_is_head(ref)) {
2558 struct btrfs_delayed_ref_head *head;
2559
2560 head = btrfs_delayed_node_to_head(ref);
2561 atomic_inc(&ref->refs);
2562
2563 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002564 /*
2565 * Mutex was contended, block until it's
2566 * released and try again
2567 */
Chris Mason56bec292009-03-13 10:10:06 -04002568 mutex_lock(&head->mutex);
2569 mutex_unlock(&head->mutex);
2570
2571 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002572 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002573 goto again;
2574 }
2575 node = rb_next(node);
2576 }
2577 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002578 schedule_timeout(1);
2579 goto again;
2580 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002581out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002582 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002583 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002584 return 0;
2585}
2586
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002587int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2588 struct btrfs_root *root,
2589 u64 bytenr, u64 num_bytes, u64 flags,
2590 int is_data)
2591{
2592 struct btrfs_delayed_extent_op *extent_op;
2593 int ret;
2594
Miao Xie78a61842012-11-21 02:21:28 +00002595 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002596 if (!extent_op)
2597 return -ENOMEM;
2598
2599 extent_op->flags_to_set = flags;
2600 extent_op->update_flags = 1;
2601 extent_op->update_key = 0;
2602 extent_op->is_data = is_data ? 1 : 0;
2603
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002604 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2605 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002606 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002607 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002608 return ret;
2609}
2610
2611static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2612 struct btrfs_root *root,
2613 struct btrfs_path *path,
2614 u64 objectid, u64 offset, u64 bytenr)
2615{
2616 struct btrfs_delayed_ref_head *head;
2617 struct btrfs_delayed_ref_node *ref;
2618 struct btrfs_delayed_data_ref *data_ref;
2619 struct btrfs_delayed_ref_root *delayed_refs;
2620 struct rb_node *node;
2621 int ret = 0;
2622
2623 ret = -ENOENT;
2624 delayed_refs = &trans->transaction->delayed_refs;
2625 spin_lock(&delayed_refs->lock);
2626 head = btrfs_find_delayed_ref_head(trans, bytenr);
2627 if (!head)
2628 goto out;
2629
2630 if (!mutex_trylock(&head->mutex)) {
2631 atomic_inc(&head->node.refs);
2632 spin_unlock(&delayed_refs->lock);
2633
David Sterbab3b4aa72011-04-21 01:20:15 +02002634 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002635
David Sterba8cc33e52011-05-02 15:29:25 +02002636 /*
2637 * Mutex was contended, block until it's released and let
2638 * caller try again
2639 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002640 mutex_lock(&head->mutex);
2641 mutex_unlock(&head->mutex);
2642 btrfs_put_delayed_ref(&head->node);
2643 return -EAGAIN;
2644 }
2645
2646 node = rb_prev(&head->node.rb_node);
2647 if (!node)
2648 goto out_unlock;
2649
2650 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2651
2652 if (ref->bytenr != bytenr)
2653 goto out_unlock;
2654
2655 ret = 1;
2656 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2657 goto out_unlock;
2658
2659 data_ref = btrfs_delayed_node_to_data_ref(ref);
2660
2661 node = rb_prev(node);
2662 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002663 int seq = ref->seq;
2664
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002665 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002666 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002667 goto out_unlock;
2668 }
2669
2670 if (data_ref->root != root->root_key.objectid ||
2671 data_ref->objectid != objectid || data_ref->offset != offset)
2672 goto out_unlock;
2673
2674 ret = 0;
2675out_unlock:
2676 mutex_unlock(&head->mutex);
2677out:
2678 spin_unlock(&delayed_refs->lock);
2679 return ret;
2680}
2681
2682static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2683 struct btrfs_root *root,
2684 struct btrfs_path *path,
2685 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002686{
2687 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002688 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002689 struct btrfs_extent_data_ref *ref;
2690 struct btrfs_extent_inline_ref *iref;
2691 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002692 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002693 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002694 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002695
Chris Masonbe20aa92007-12-17 20:14:01 -05002696 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002697 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002698 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002699
Chris Masonbe20aa92007-12-17 20:14:01 -05002700 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2701 if (ret < 0)
2702 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002703 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002704
2705 ret = -ENOENT;
2706 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002707 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002708
Zheng Yan31840ae2008-09-23 13:14:14 -04002709 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002710 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002711 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002712
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002713 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002714 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002715
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002716 ret = 1;
2717 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2718#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2719 if (item_size < sizeof(*ei)) {
2720 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2721 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002722 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002723#endif
2724 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2725
2726 if (item_size != sizeof(*ei) +
2727 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2728 goto out;
2729
2730 if (btrfs_extent_generation(leaf, ei) <=
2731 btrfs_root_last_snapshot(&root->root_item))
2732 goto out;
2733
2734 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2735 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2736 BTRFS_EXTENT_DATA_REF_KEY)
2737 goto out;
2738
2739 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2740 if (btrfs_extent_refs(leaf, ei) !=
2741 btrfs_extent_data_ref_count(leaf, ref) ||
2742 btrfs_extent_data_ref_root(leaf, ref) !=
2743 root->root_key.objectid ||
2744 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2745 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2746 goto out;
2747
Yan Zhengf321e492008-07-30 09:26:11 -04002748 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002749out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002750 return ret;
2751}
2752
2753int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2754 struct btrfs_root *root,
2755 u64 objectid, u64 offset, u64 bytenr)
2756{
2757 struct btrfs_path *path;
2758 int ret;
2759 int ret2;
2760
2761 path = btrfs_alloc_path();
2762 if (!path)
2763 return -ENOENT;
2764
2765 do {
2766 ret = check_committed_ref(trans, root, path, objectid,
2767 offset, bytenr);
2768 if (ret && ret != -ENOENT)
2769 goto out;
2770
2771 ret2 = check_delayed_ref(trans, root, path, objectid,
2772 offset, bytenr);
2773 } while (ret2 == -EAGAIN);
2774
2775 if (ret2 && ret2 != -ENOENT) {
2776 ret = ret2;
2777 goto out;
2778 }
2779
2780 if (ret != -ENOENT || ret2 != -ENOENT)
2781 ret = 0;
2782out:
Yan Zhengf321e492008-07-30 09:26:11 -04002783 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002784 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2785 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002786 return ret;
2787}
2788
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002789static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002790 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002791 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002792 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002793{
2794 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002795 u64 num_bytes;
2796 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002797 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002798 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002799 struct btrfs_key key;
2800 struct btrfs_file_extent_item *fi;
2801 int i;
2802 int level;
2803 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002804 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002805 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002806
2807 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002808 nritems = btrfs_header_nritems(buf);
2809 level = btrfs_header_level(buf);
2810
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002811 if (!root->ref_cows && level == 0)
2812 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002813
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002814 if (inc)
2815 process_func = btrfs_inc_extent_ref;
2816 else
2817 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002818
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002819 if (full_backref)
2820 parent = buf->start;
2821 else
2822 parent = 0;
2823
Zheng Yan31840ae2008-09-23 13:14:14 -04002824 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002825 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002826 btrfs_item_key_to_cpu(buf, &key, i);
2827 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002828 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002829 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002830 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002831 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002832 BTRFS_FILE_EXTENT_INLINE)
2833 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002834 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2835 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002836 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002837
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002838 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2839 key.offset -= btrfs_file_extent_offset(buf, fi);
2840 ret = process_func(trans, root, bytenr, num_bytes,
2841 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002842 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002843 if (ret)
2844 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002845 } else {
2846 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002847 num_bytes = btrfs_level_size(root, level - 1);
2848 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002849 parent, ref_root, level - 1, 0,
2850 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002851 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002852 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002853 }
2854 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002855 return 0;
2856fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002857 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002858}
2859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002860int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002861 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002862{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002863 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002864}
Zheng Yan31840ae2008-09-23 13:14:14 -04002865
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002866int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002867 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002868{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002869 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002870}
2871
Chris Mason9078a3e2007-04-26 16:46:15 -04002872static int write_one_cache_group(struct btrfs_trans_handle *trans,
2873 struct btrfs_root *root,
2874 struct btrfs_path *path,
2875 struct btrfs_block_group_cache *cache)
2876{
2877 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002878 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002879 unsigned long bi;
2880 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002881
Chris Mason9078a3e2007-04-26 16:46:15 -04002882 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002883 if (ret < 0)
2884 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002885 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04002886
2887 leaf = path->nodes[0];
2888 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2889 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2890 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002891 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002892fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002893 if (ret) {
2894 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04002895 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002896 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002897 return 0;
2898
2899}
2900
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002901static struct btrfs_block_group_cache *
2902next_block_group(struct btrfs_root *root,
2903 struct btrfs_block_group_cache *cache)
2904{
2905 struct rb_node *node;
2906 spin_lock(&root->fs_info->block_group_cache_lock);
2907 node = rb_next(&cache->cache_node);
2908 btrfs_put_block_group(cache);
2909 if (node) {
2910 cache = rb_entry(node, struct btrfs_block_group_cache,
2911 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002912 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002913 } else
2914 cache = NULL;
2915 spin_unlock(&root->fs_info->block_group_cache_lock);
2916 return cache;
2917}
2918
Josef Bacik0af3d002010-06-21 14:48:16 -04002919static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2920 struct btrfs_trans_handle *trans,
2921 struct btrfs_path *path)
2922{
2923 struct btrfs_root *root = block_group->fs_info->tree_root;
2924 struct inode *inode = NULL;
2925 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002926 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002927 int num_pages = 0;
2928 int retries = 0;
2929 int ret = 0;
2930
2931 /*
2932 * If this block group is smaller than 100 megs don't bother caching the
2933 * block group.
2934 */
2935 if (block_group->key.offset < (100 * 1024 * 1024)) {
2936 spin_lock(&block_group->lock);
2937 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2938 spin_unlock(&block_group->lock);
2939 return 0;
2940 }
2941
2942again:
2943 inode = lookup_free_space_inode(root, block_group, path);
2944 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2945 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002946 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002947 goto out;
2948 }
2949
2950 if (IS_ERR(inode)) {
2951 BUG_ON(retries);
2952 retries++;
2953
2954 if (block_group->ro)
2955 goto out_free;
2956
2957 ret = create_free_space_inode(root, trans, block_group, path);
2958 if (ret)
2959 goto out_free;
2960 goto again;
2961 }
2962
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002963 /* We've already setup this transaction, go ahead and exit */
2964 if (block_group->cache_generation == trans->transid &&
2965 i_size_read(inode)) {
2966 dcs = BTRFS_DC_SETUP;
2967 goto out_put;
2968 }
2969
Josef Bacik0af3d002010-06-21 14:48:16 -04002970 /*
2971 * We want to set the generation to 0, that way if anything goes wrong
2972 * from here on out we know not to trust this cache when we load up next
2973 * time.
2974 */
2975 BTRFS_I(inode)->generation = 0;
2976 ret = btrfs_update_inode(trans, root, inode);
2977 WARN_ON(ret);
2978
2979 if (i_size_read(inode) > 0) {
2980 ret = btrfs_truncate_free_space_cache(root, trans, path,
2981 inode);
2982 if (ret)
2983 goto out_put;
2984 }
2985
2986 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06002987 if (block_group->cached != BTRFS_CACHE_FINISHED ||
2988 !btrfs_test_opt(root, SPACE_CACHE)) {
2989 /*
2990 * don't bother trying to write stuff out _if_
2991 * a) we're not cached,
2992 * b) we're with nospace_cache mount option.
2993 */
Josef Bacik2b209822010-12-03 13:17:53 -05002994 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002995 spin_unlock(&block_group->lock);
2996 goto out_put;
2997 }
2998 spin_unlock(&block_group->lock);
2999
Josef Bacik6fc823b2012-08-06 13:46:38 -06003000 /*
3001 * Try to preallocate enough space based on how big the block group is.
3002 * Keep in mind this has to include any pinned space which could end up
3003 * taking up quite a bit since it's not folded into the other space
3004 * cache.
3005 */
3006 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003007 if (!num_pages)
3008 num_pages = 1;
3009
Josef Bacik0af3d002010-06-21 14:48:16 -04003010 num_pages *= 16;
3011 num_pages *= PAGE_CACHE_SIZE;
3012
3013 ret = btrfs_check_data_free_space(inode, num_pages);
3014 if (ret)
3015 goto out_put;
3016
3017 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3018 num_pages, num_pages,
3019 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003020 if (!ret)
3021 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003022 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003023
Josef Bacik0af3d002010-06-21 14:48:16 -04003024out_put:
3025 iput(inode);
3026out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003027 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003028out:
3029 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003030 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003031 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003032 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003033 spin_unlock(&block_group->lock);
3034
3035 return ret;
3036}
3037
Chris Mason96b51792007-10-15 16:15:19 -04003038int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3039 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003040{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003041 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003042 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003043 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003044 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003045
3046 path = btrfs_alloc_path();
3047 if (!path)
3048 return -ENOMEM;
3049
Josef Bacik0af3d002010-06-21 14:48:16 -04003050again:
3051 while (1) {
3052 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3053 while (cache) {
3054 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3055 break;
3056 cache = next_block_group(root, cache);
3057 }
3058 if (!cache) {
3059 if (last == 0)
3060 break;
3061 last = 0;
3062 continue;
3063 }
3064 err = cache_save_setup(cache, trans, path);
3065 last = cache->key.objectid + cache->key.offset;
3066 btrfs_put_block_group(cache);
3067 }
3068
Chris Masond3977122009-01-05 21:25:51 -05003069 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003070 if (last == 0) {
3071 err = btrfs_run_delayed_refs(trans, root,
3072 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003073 if (err) /* File system offline */
3074 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003075 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003076
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003077 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3078 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003079 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3080 btrfs_put_block_group(cache);
3081 goto again;
3082 }
3083
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003084 if (cache->dirty)
3085 break;
3086 cache = next_block_group(root, cache);
3087 }
3088 if (!cache) {
3089 if (last == 0)
3090 break;
3091 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003092 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003093 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003094
Josef Bacik0cb59c92010-07-02 12:14:14 -04003095 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3096 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003097 cache->dirty = 0;
3098 last = cache->key.objectid + cache->key.offset;
3099
3100 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003101 if (err) /* File system offline */
3102 goto out;
3103
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003104 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003105 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003106
Josef Bacik0cb59c92010-07-02 12:14:14 -04003107 while (1) {
3108 /*
3109 * I don't think this is needed since we're just marking our
3110 * preallocated extent as written, but just in case it can't
3111 * hurt.
3112 */
3113 if (last == 0) {
3114 err = btrfs_run_delayed_refs(trans, root,
3115 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003116 if (err) /* File system offline */
3117 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003118 }
3119
3120 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3121 while (cache) {
3122 /*
3123 * Really this shouldn't happen, but it could if we
3124 * couldn't write the entire preallocated extent and
3125 * splitting the extent resulted in a new block.
3126 */
3127 if (cache->dirty) {
3128 btrfs_put_block_group(cache);
3129 goto again;
3130 }
3131 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3132 break;
3133 cache = next_block_group(root, cache);
3134 }
3135 if (!cache) {
3136 if (last == 0)
3137 break;
3138 last = 0;
3139 continue;
3140 }
3141
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003142 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003143
3144 /*
3145 * If we didn't have an error then the cache state is still
3146 * NEED_WRITE, so we can set it to WRITTEN.
3147 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003148 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003149 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3150 last = cache->key.objectid + cache->key.offset;
3151 btrfs_put_block_group(cache);
3152 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003153out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003154
Chris Mason9078a3e2007-04-26 16:46:15 -04003155 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003156 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003157}
3158
Yan Zhengd2fb3432008-12-11 16:30:39 -05003159int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3160{
3161 struct btrfs_block_group_cache *block_group;
3162 int readonly = 0;
3163
3164 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3165 if (!block_group || block_group->ro)
3166 readonly = 1;
3167 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003168 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003169 return readonly;
3170}
3171
Chris Mason593060d2008-03-25 16:50:33 -04003172static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3173 u64 total_bytes, u64 bytes_used,
3174 struct btrfs_space_info **space_info)
3175{
3176 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003177 int i;
3178 int factor;
3179
3180 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3181 BTRFS_BLOCK_GROUP_RAID10))
3182 factor = 2;
3183 else
3184 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003185
3186 found = __find_space_info(info, flags);
3187 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003188 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003189 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003190 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003191 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003192 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003193 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003194 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003195 *space_info = found;
3196 return 0;
3197 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003198 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003199 if (!found)
3200 return -ENOMEM;
3201
Yan, Zhengb742bb82010-05-16 10:46:24 -04003202 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3203 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003204 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003205 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003206 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003207 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003208 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003209 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003210 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003211 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003212 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003213 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003214 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003215 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003216 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003217 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003218 found->flush = 0;
3219 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003220 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003221 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003222 if (flags & BTRFS_BLOCK_GROUP_DATA)
3223 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003224 return 0;
3225}
3226
Chris Mason8790d502008-04-03 16:29:03 -04003227static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3228{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003229 u64 extra_flags = chunk_to_extended(flags) &
3230 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003231
Miao Xiede98ced2013-01-29 10:13:12 +00003232 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003233 if (flags & BTRFS_BLOCK_GROUP_DATA)
3234 fs_info->avail_data_alloc_bits |= extra_flags;
3235 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3236 fs_info->avail_metadata_alloc_bits |= extra_flags;
3237 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3238 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003239 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003240}
Chris Mason593060d2008-03-25 16:50:33 -04003241
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003242/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003243 * returns target flags in extended format or 0 if restripe for this
3244 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003245 *
3246 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003247 */
3248static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3249{
3250 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3251 u64 target = 0;
3252
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003253 if (!bctl)
3254 return 0;
3255
3256 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3257 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3258 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3259 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3260 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3261 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3262 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3263 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3264 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3265 }
3266
3267 return target;
3268}
3269
3270/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003271 * @flags: available profiles in extended format (see ctree.h)
3272 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003273 * Returns reduced profile in chunk format. If profile changing is in
3274 * progress (either running or paused) picks the target profile (if it's
3275 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003276 */
Yan Zheng2b820322008-11-17 21:11:30 -05003277u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003278{
Chris Masoncd02dca2010-12-13 14:56:23 -05003279 /*
3280 * we add in the count of missing devices because we want
3281 * to make sure that any RAID levels on a degraded FS
3282 * continue to be honored.
3283 */
3284 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3285 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003286 u64 target;
Chris Masona061fc82008-05-07 11:43:44 -04003287
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003288 /*
3289 * see if restripe for this chunk_type is in progress, if so
3290 * try to reduce to the target profile
3291 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003292 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003293 target = get_restripe_target(root->fs_info, flags);
3294 if (target) {
3295 /* pick target profile only if it's already available */
3296 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003297 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003298 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003299 }
3300 }
3301 spin_unlock(&root->fs_info->balance_lock);
3302
Chris Masona061fc82008-05-07 11:43:44 -04003303 if (num_devices == 1)
3304 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3305 if (num_devices < 4)
3306 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3307
Chris Masonec44a352008-04-28 15:29:52 -04003308 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3309 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003310 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003311 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003312 }
Chris Masonec44a352008-04-28 15:29:52 -04003313
3314 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003315 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003316 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003317 }
Chris Masonec44a352008-04-28 15:29:52 -04003318
3319 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3320 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3321 (flags & BTRFS_BLOCK_GROUP_RAID10) |
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003322 (flags & BTRFS_BLOCK_GROUP_DUP))) {
Chris Masonec44a352008-04-28 15:29:52 -04003323 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003324 }
3325
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003326 return extended_to_chunk(flags);
Chris Masonec44a352008-04-28 15:29:52 -04003327}
3328
Yan, Zhengb742bb82010-05-16 10:46:24 -04003329static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003330{
Miao Xiede98ced2013-01-29 10:13:12 +00003331 unsigned seq;
3332
3333 do {
3334 seq = read_seqbegin(&root->fs_info->profiles_lock);
3335
3336 if (flags & BTRFS_BLOCK_GROUP_DATA)
3337 flags |= root->fs_info->avail_data_alloc_bits;
3338 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3339 flags |= root->fs_info->avail_system_alloc_bits;
3340 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3341 flags |= root->fs_info->avail_metadata_alloc_bits;
3342 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003343
Yan, Zhengb742bb82010-05-16 10:46:24 -04003344 return btrfs_reduce_alloc_profile(root, flags);
3345}
Josef Bacik6a632092009-02-20 11:00:09 -05003346
Miao Xie6d07bce2011-01-05 10:07:31 +00003347u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003348{
3349 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003350
Yan, Zhengb742bb82010-05-16 10:46:24 -04003351 if (data)
3352 flags = BTRFS_BLOCK_GROUP_DATA;
3353 else if (root == root->fs_info->chunk_root)
3354 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3355 else
3356 flags = BTRFS_BLOCK_GROUP_METADATA;
3357
3358 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003359}
3360
Josef Bacik6a632092009-02-20 11:00:09 -05003361/*
3362 * This will check the space that the inode allocates from to make sure we have
3363 * enough space for bytes.
3364 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003365int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003366{
3367 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003368 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003369 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003370 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003371 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003372
3373 /* make sure bytes are sectorsize aligned */
3374 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3375
Li Zefan82d59022011-04-20 10:33:24 +08003376 if (root == root->fs_info->tree_root ||
3377 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003378 alloc_chunk = 0;
3379 committed = 1;
3380 }
3381
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003382 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003383 if (!data_sinfo)
3384 goto alloc;
3385
Josef Bacik6a632092009-02-20 11:00:09 -05003386again:
3387 /* make sure we have enough space to handle the data first */
3388 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003389 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3390 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3391 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003392
3393 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003394 struct btrfs_trans_handle *trans;
3395
Josef Bacik6a632092009-02-20 11:00:09 -05003396 /*
3397 * if we don't have enough free bytes in this space then we need
3398 * to alloc a new chunk.
3399 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003400 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003401 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003402
Chris Mason0e4f8f82011-04-15 16:05:44 -04003403 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003404 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003405alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003406 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003407 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003408 if (IS_ERR(trans))
3409 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003410
3411 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003412 alloc_target,
3413 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003414 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003415 if (ret < 0) {
3416 if (ret != -ENOSPC)
3417 return ret;
3418 else
3419 goto commit_trans;
3420 }
Chris Mason33b4d472009-09-22 14:45:50 -04003421
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003422 if (!data_sinfo)
3423 data_sinfo = fs_info->data_sinfo;
3424
Josef Bacik6a632092009-02-20 11:00:09 -05003425 goto again;
3426 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003427
3428 /*
3429 * If we have less pinned bytes than we want to allocate then
3430 * don't bother committing the transaction, it won't help us.
3431 */
3432 if (data_sinfo->bytes_pinned < bytes)
3433 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003434 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003435
3436 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003437commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003438 if (!committed &&
3439 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003440 committed = 1;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003441 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003442 if (IS_ERR(trans))
3443 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003444 ret = btrfs_commit_transaction(trans, root);
3445 if (ret)
3446 return ret;
3447 goto again;
3448 }
3449
Josef Bacik6a632092009-02-20 11:00:09 -05003450 return -ENOSPC;
3451 }
3452 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003453 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003454 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003455 spin_unlock(&data_sinfo->lock);
3456
Josef Bacik9ed74f22009-09-11 16:12:44 -04003457 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003458}
3459
3460/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003461 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003462 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003463void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003464{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003465 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003466 struct btrfs_space_info *data_sinfo;
3467
3468 /* make sure bytes are sectorsize aligned */
3469 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3470
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003471 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003472 spin_lock(&data_sinfo->lock);
3473 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003474 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003475 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003476 spin_unlock(&data_sinfo->lock);
3477}
3478
Josef Bacik97e728d2009-04-21 17:40:57 -04003479static void force_metadata_allocation(struct btrfs_fs_info *info)
3480{
3481 struct list_head *head = &info->space_info;
3482 struct btrfs_space_info *found;
3483
3484 rcu_read_lock();
3485 list_for_each_entry_rcu(found, head, list) {
3486 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003487 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003488 }
3489 rcu_read_unlock();
3490}
3491
Chris Masone5bc2452010-10-26 13:37:56 -04003492static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003493 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003494{
Josef Bacikfb25e912011-07-26 17:00:46 -04003495 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003496 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003497 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003498 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003499
Chris Mason0e4f8f82011-04-15 16:05:44 -04003500 if (force == CHUNK_ALLOC_FORCE)
3501 return 1;
3502
3503 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003504 * We need to take into account the global rsv because for all intents
3505 * and purposes it's used space. Don't worry about locking the
3506 * global_rsv, it doesn't change except when the transaction commits.
3507 */
Josef Bacik54338b52012-08-14 16:20:52 -04003508 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3509 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003510
3511 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003512 * in limited mode, we want to have some free space up to
3513 * about 1% of the FS size.
3514 */
3515 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003516 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003517 thresh = max_t(u64, 64 * 1024 * 1024,
3518 div_factor_fine(thresh, 1));
3519
3520 if (num_bytes - num_allocated < thresh)
3521 return 1;
3522 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003523
Josef Bacik698d0082012-09-12 14:08:47 -04003524 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003525 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003526 return 1;
3527}
3528
Liu Bo15d1ff82012-03-29 09:57:44 -04003529static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3530{
3531 u64 num_dev;
3532
3533 if (type & BTRFS_BLOCK_GROUP_RAID10 ||
3534 type & BTRFS_BLOCK_GROUP_RAID0)
3535 num_dev = root->fs_info->fs_devices->rw_devices;
3536 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3537 num_dev = 2;
3538 else
3539 num_dev = 1; /* DUP or single */
3540
3541 /* metadata for updaing devices and chunk tree */
3542 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3543}
3544
3545static void check_system_chunk(struct btrfs_trans_handle *trans,
3546 struct btrfs_root *root, u64 type)
3547{
3548 struct btrfs_space_info *info;
3549 u64 left;
3550 u64 thresh;
3551
3552 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3553 spin_lock(&info->lock);
3554 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3555 info->bytes_reserved - info->bytes_readonly;
3556 spin_unlock(&info->lock);
3557
3558 thresh = get_system_chunk_thresh(root, type);
3559 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3560 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3561 left, thresh, type);
3562 dump_space_info(info, 0, 0);
3563 }
3564
3565 if (left < thresh) {
3566 u64 flags;
3567
3568 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3569 btrfs_alloc_chunk(trans, root, flags);
3570 }
3571}
3572
Chris Mason6324fbf2008-03-24 15:01:59 -04003573static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003574 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003575{
3576 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003577 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003578 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003579 int ret = 0;
3580
Josef Bacikc6b305a2012-12-18 09:16:16 -05003581 /* Don't re-enter if we're already allocating a chunk */
3582 if (trans->allocating_chunk)
3583 return -ENOSPC;
3584
Chris Mason6324fbf2008-03-24 15:01:59 -04003585 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003586 if (!space_info) {
3587 ret = update_space_info(extent_root->fs_info, flags,
3588 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003589 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003590 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003591 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003592
Josef Bacik6d741192011-04-11 20:20:11 -04003593again:
Josef Bacik25179202008-10-29 14:49:05 -04003594 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003595 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003596 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003597 if (space_info->full) {
3598 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003599 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003600 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003601
Josef Bacik698d0082012-09-12 14:08:47 -04003602 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003603 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003604 return 0;
3605 } else if (space_info->chunk_alloc) {
3606 wait_for_alloc = 1;
3607 } else {
3608 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003609 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003610
Josef Bacik25179202008-10-29 14:49:05 -04003611 spin_unlock(&space_info->lock);
3612
Josef Bacik6d741192011-04-11 20:20:11 -04003613 mutex_lock(&fs_info->chunk_mutex);
3614
3615 /*
3616 * The chunk_mutex is held throughout the entirety of a chunk
3617 * allocation, so once we've acquired the chunk_mutex we know that the
3618 * other guy is done and we need to recheck and see if we should
3619 * allocate.
3620 */
3621 if (wait_for_alloc) {
3622 mutex_unlock(&fs_info->chunk_mutex);
3623 wait_for_alloc = 0;
3624 goto again;
3625 }
3626
Josef Bacikc6b305a2012-12-18 09:16:16 -05003627 trans->allocating_chunk = true;
3628
Josef Bacik97e728d2009-04-21 17:40:57 -04003629 /*
Josef Bacik67377732010-09-16 16:19:09 -04003630 * If we have mixed data/metadata chunks we want to make sure we keep
3631 * allocating mixed chunks instead of individual chunks.
3632 */
3633 if (btrfs_mixed_space_info(space_info))
3634 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3635
3636 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003637 * if we're doing a data chunk, go ahead and make sure that
3638 * we keep a reasonable number of metadata chunks allocated in the
3639 * FS as well.
3640 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003641 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003642 fs_info->data_chunk_allocations++;
3643 if (!(fs_info->data_chunk_allocations %
3644 fs_info->metadata_ratio))
3645 force_metadata_allocation(fs_info);
3646 }
3647
Liu Bo15d1ff82012-03-29 09:57:44 -04003648 /*
3649 * Check if we have enough space in SYSTEM chunk because we may need
3650 * to update devices.
3651 */
3652 check_system_chunk(trans, extent_root, flags);
3653
Yan Zheng2b820322008-11-17 21:11:30 -05003654 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003655 trans->allocating_chunk = false;
Mark Fasheh92b8e892011-07-12 10:57:59 -07003656 if (ret < 0 && ret != -ENOSPC)
3657 goto out;
3658
Josef Bacik9ed74f22009-09-11 16:12:44 -04003659 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003660 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003661 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003662 else
3663 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003664
Chris Mason0e4f8f82011-04-15 16:05:44 -04003665 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003666 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003667 spin_unlock(&space_info->lock);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003668out:
Dan Carpentera25c75d2012-04-18 09:59:29 +03003669 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003670 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003671}
3672
Josef Bacika80c8dc2012-09-06 16:59:33 -04003673static int can_overcommit(struct btrfs_root *root,
3674 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003675 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003676{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003677 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003678 u64 profile = btrfs_get_alloc_profile(root, 0);
Josef Bacik96f1bb52013-01-30 17:02:51 -05003679 u64 rsv_size = 0;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003680 u64 avail;
3681 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003682 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003683
3684 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003685 space_info->bytes_pinned + space_info->bytes_readonly;
3686
3687 spin_lock(&global_rsv->lock);
3688 rsv_size = global_rsv->size;
3689 spin_unlock(&global_rsv->lock);
3690
3691 /*
3692 * We only want to allow over committing if we have lots of actual space
3693 * free, but if we don't have enough space to handle the global reserve
3694 * space then we could end up having a real enospc problem when trying
3695 * to allocate a chunk or some other such important allocation.
3696 */
3697 rsv_size <<= 1;
3698 if (used + rsv_size >= space_info->total_bytes)
3699 return 0;
3700
3701 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003702
3703 spin_lock(&root->fs_info->free_chunk_lock);
3704 avail = root->fs_info->free_chunk_space;
3705 spin_unlock(&root->fs_info->free_chunk_lock);
3706
3707 /*
3708 * If we have dup, raid1 or raid10 then only half of the free
3709 * space is actually useable.
3710 */
3711 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3712 BTRFS_BLOCK_GROUP_RAID1 |
3713 BTRFS_BLOCK_GROUP_RAID10))
3714 avail >>= 1;
3715
Josef Bacik70afa392013-02-06 13:53:19 -05003716 to_add = space_info->total_bytes;
3717
Josef Bacika80c8dc2012-09-06 16:59:33 -04003718 /*
Miao Xie561c2942012-10-16 11:32:18 +00003719 * If we aren't flushing all things, let us overcommit up to
3720 * 1/2th of the space. If we can flush, don't let us overcommit
3721 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003722 */
Miao Xie08e007d2012-10-16 11:33:38 +00003723 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003724 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003725 else
Josef Bacik70afa392013-02-06 13:53:19 -05003726 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003727
Josef Bacik70afa392013-02-06 13:53:19 -05003728 /*
3729 * Limit the overcommit to the amount of free space we could possibly
3730 * allocate for chunks.
3731 */
3732 to_add = min(avail, to_add);
3733
3734 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003735 return 1;
3736 return 0;
3737}
3738
Miao Xieda633a42012-12-20 11:19:09 +00003739static inline int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
3740 unsigned long nr_pages,
3741 enum wb_reason reason)
Josef Bacik1135d6d2012-12-14 13:46:43 -05003742{
Miao Xieda633a42012-12-20 11:19:09 +00003743 /* the flusher is dealing with the dirty inodes now. */
3744 if (writeback_in_progress(sb->s_bdi))
3745 return 1;
3746
3747 if (down_read_trylock(&sb->s_umount)) {
Josef Bacik1135d6d2012-12-14 13:46:43 -05003748 writeback_inodes_sb_nr(sb, nr_pages, reason);
3749 up_read(&sb->s_umount);
3750 return 1;
3751 }
3752
3753 return 0;
3754}
3755
Miao Xieda633a42012-12-20 11:19:09 +00003756void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3757 unsigned long nr_pages)
3758{
3759 struct super_block *sb = root->fs_info->sb;
3760 int started;
3761
3762 /* If we can not start writeback, just sync all the delalloc file. */
3763 started = writeback_inodes_sb_nr_if_idle_safe(sb, nr_pages,
3764 WB_REASON_FS_FREE_SPACE);
3765 if (!started) {
3766 /*
3767 * We needn't worry the filesystem going from r/w to r/o though
3768 * we don't acquire ->s_umount mutex, because the filesystem
3769 * should guarantee the delalloc inodes list be empty after
3770 * the filesystem is readonly(all dirty pages are written to
3771 * the disk).
3772 */
3773 btrfs_start_delalloc_inodes(root, 0);
3774 btrfs_wait_ordered_extents(root, 0);
3775 }
3776}
3777
Yan, Zheng5da9d012010-05-16 10:46:25 -04003778/*
3779 * shrink metadata reservation for delalloc
3780 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003781static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3782 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003783{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003784 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003785 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003786 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003787 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003788 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003789 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003790 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003791 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003792 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003793
Josef Bacik663350a2011-11-03 22:54:25 -04003794 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003795 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003796 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003797
3798 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003799 delalloc_bytes = percpu_counter_sum_positive(
3800 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003801 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003802 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003803 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003804 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003805 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003806 }
3807
Josef Bacikf4c738c2012-07-02 17:10:51 -04003808 while (delalloc_bytes && loops < 3) {
3809 max_reclaim = min(delalloc_bytes, to_reclaim);
3810 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003811 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003812 /*
3813 * We need to wait for the async pages to actually start before
3814 * we do anything.
3815 */
3816 wait_event(root->fs_info->async_submit_wait,
3817 !atomic_read(&root->fs_info->async_delalloc_pages));
3818
Miao Xie08e007d2012-10-16 11:33:38 +00003819 if (!trans)
3820 flush = BTRFS_RESERVE_FLUSH_ALL;
3821 else
3822 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003823 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003824 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003825 spin_unlock(&space_info->lock);
3826 break;
3827 }
Josef Bacik0019f102010-10-15 15:18:40 -04003828 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003829
Chris Mason36e39c42011-03-12 07:08:42 -05003830 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003831 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003832 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003833 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003834 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003835 if (time_left)
3836 break;
3837 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003838 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003839 delalloc_bytes = percpu_counter_sum_positive(
3840 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003841 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003842}
3843
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003844/**
Josef Bacik663350a2011-11-03 22:54:25 -04003845 * maybe_commit_transaction - possibly commit the transaction if its ok to
3846 * @root - the root we're allocating for
3847 * @bytes - the number of bytes we want to reserve
3848 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003849 *
Josef Bacik663350a2011-11-03 22:54:25 -04003850 * This will check to make sure that committing the transaction will actually
3851 * get us somewhere and then commit the transaction if it does. Otherwise it
3852 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003853 */
Josef Bacik663350a2011-11-03 22:54:25 -04003854static int may_commit_transaction(struct btrfs_root *root,
3855 struct btrfs_space_info *space_info,
3856 u64 bytes, int force)
3857{
3858 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3859 struct btrfs_trans_handle *trans;
3860
3861 trans = (struct btrfs_trans_handle *)current->journal_info;
3862 if (trans)
3863 return -EAGAIN;
3864
3865 if (force)
3866 goto commit;
3867
3868 /* See if there is enough pinned space to make this reservation */
3869 spin_lock(&space_info->lock);
3870 if (space_info->bytes_pinned >= bytes) {
3871 spin_unlock(&space_info->lock);
3872 goto commit;
3873 }
3874 spin_unlock(&space_info->lock);
3875
3876 /*
3877 * See if there is some space in the delayed insertion reservation for
3878 * this reservation.
3879 */
3880 if (space_info != delayed_rsv->space_info)
3881 return -ENOSPC;
3882
Liu Bod9b02182012-02-16 18:34:39 +08003883 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003884 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003885 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04003886 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003887 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003888 return -ENOSPC;
3889 }
3890 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003891 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003892
3893commit:
3894 trans = btrfs_join_transaction(root);
3895 if (IS_ERR(trans))
3896 return -ENOSPC;
3897
3898 return btrfs_commit_transaction(trans, root);
3899}
3900
Josef Bacik96c3f432012-06-21 14:05:49 -04003901enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04003902 FLUSH_DELAYED_ITEMS_NR = 1,
3903 FLUSH_DELAYED_ITEMS = 2,
3904 FLUSH_DELALLOC = 3,
3905 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04003906 ALLOC_CHUNK = 5,
3907 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04003908};
3909
3910static int flush_space(struct btrfs_root *root,
3911 struct btrfs_space_info *space_info, u64 num_bytes,
3912 u64 orig_bytes, int state)
3913{
3914 struct btrfs_trans_handle *trans;
3915 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003916 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04003917
3918 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04003919 case FLUSH_DELAYED_ITEMS_NR:
3920 case FLUSH_DELAYED_ITEMS:
3921 if (state == FLUSH_DELAYED_ITEMS_NR) {
3922 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3923
3924 nr = (int)div64_u64(num_bytes, bytes);
3925 if (!nr)
3926 nr = 1;
3927 nr *= 2;
3928 } else {
3929 nr = -1;
3930 }
3931 trans = btrfs_join_transaction(root);
3932 if (IS_ERR(trans)) {
3933 ret = PTR_ERR(trans);
3934 break;
3935 }
3936 ret = btrfs_run_delayed_items_nr(trans, root, nr);
3937 btrfs_end_transaction(trans, root);
3938 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003939 case FLUSH_DELALLOC:
3940 case FLUSH_DELALLOC_WAIT:
3941 shrink_delalloc(root, num_bytes, orig_bytes,
3942 state == FLUSH_DELALLOC_WAIT);
3943 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04003944 case ALLOC_CHUNK:
3945 trans = btrfs_join_transaction(root);
3946 if (IS_ERR(trans)) {
3947 ret = PTR_ERR(trans);
3948 break;
3949 }
3950 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04003951 btrfs_get_alloc_profile(root, 0),
3952 CHUNK_ALLOC_NO_FORCE);
3953 btrfs_end_transaction(trans, root);
3954 if (ret == -ENOSPC)
3955 ret = 0;
3956 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04003957 case COMMIT_TRANS:
3958 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3959 break;
3960 default:
3961 ret = -ENOSPC;
3962 break;
3963 }
3964
3965 return ret;
3966}
Josef Bacik663350a2011-11-03 22:54:25 -04003967/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003968 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3969 * @root - the root we're allocating for
3970 * @block_rsv - the block_rsv we're allocating for
3971 * @orig_bytes - the number of bytes we want
3972 * @flush - wether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003973 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003974 * This will reserve orgi_bytes number of bytes from the space info associated
3975 * with the block_rsv. If there is not enough space it will make an attempt to
3976 * flush out space to make room. It will do this by flushing delalloc if
3977 * possible or committing the transaction. If flush is 0 then no attempts to
3978 * regain reservations will be made and this will fail if there is not enough
3979 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003980 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003981static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003982 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00003983 u64 orig_bytes,
3984 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003985{
3986 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04003987 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003988 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003989 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003990 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003991 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003992
3993again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003994 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003995 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003996 /*
Miao Xie08e007d2012-10-16 11:33:38 +00003997 * We only want to wait if somebody other than us is flushing and we
3998 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003999 */
Miao Xie08e007d2012-10-16 11:33:38 +00004000 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4001 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004002 spin_unlock(&space_info->lock);
4003 /*
4004 * If we have a trans handle we can't wait because the flusher
4005 * may have to commit the transaction, which would mean we would
4006 * deadlock since we are waiting for the flusher to finish, but
4007 * hold the current transaction open.
4008 */
Josef Bacik663350a2011-11-03 22:54:25 -04004009 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004010 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004011 ret = wait_event_killable(space_info->wait, !space_info->flush);
4012 /* Must have been killed, return */
4013 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004014 return -EINTR;
4015
4016 spin_lock(&space_info->lock);
4017 }
4018
4019 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004020 used = space_info->bytes_used + space_info->bytes_reserved +
4021 space_info->bytes_pinned + space_info->bytes_readonly +
4022 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004023
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004024 /*
4025 * The idea here is that we've not already over-reserved the block group
4026 * then we can go ahead and save our reservation first and then start
4027 * flushing if we need to. Otherwise if we've already overcommitted
4028 * lets start flushing stuff first and then come back and try to make
4029 * our reservation.
4030 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004031 if (used <= space_info->total_bytes) {
4032 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004033 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004034 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004035 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004036 ret = 0;
4037 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004038 /*
4039 * Ok set num_bytes to orig_bytes since we aren't
4040 * overocmmitted, this way we only try and reclaim what
4041 * we need.
4042 */
4043 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004044 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004045 } else {
4046 /*
4047 * Ok we're over committed, set num_bytes to the overcommitted
4048 * amount plus the amount of bytes that we need for this
4049 * reservation.
4050 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004051 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004052 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004053 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004054
Josef Bacik44734ed2012-09-28 16:04:19 -04004055 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4056 space_info->bytes_may_use += orig_bytes;
4057 trace_btrfs_space_reservation(root->fs_info, "space_info",
4058 space_info->flags, orig_bytes,
4059 1);
4060 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004061 }
4062
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004063 /*
4064 * Couldn't make our reservation, save our place so while we're trying
4065 * to reclaim space we can actually use it instead of somebody else
4066 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004067 *
4068 * We make the other tasks wait for the flush only when we can flush
4069 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004070 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004071 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004072 flushing = true;
4073 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004074 }
4075
Yan, Zhengf0486c62010-05-16 10:46:25 -04004076 spin_unlock(&space_info->lock);
4077
Miao Xie08e007d2012-10-16 11:33:38 +00004078 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004079 goto out;
4080
Josef Bacik96c3f432012-06-21 14:05:49 -04004081 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4082 flush_state);
4083 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004084
4085 /*
4086 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4087 * would happen. So skip delalloc flush.
4088 */
4089 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4090 (flush_state == FLUSH_DELALLOC ||
4091 flush_state == FLUSH_DELALLOC_WAIT))
4092 flush_state = ALLOC_CHUNK;
4093
Josef Bacik96c3f432012-06-21 14:05:49 -04004094 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004095 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004096 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4097 flush_state < COMMIT_TRANS)
4098 goto again;
4099 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4100 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004101 goto again;
4102
4103out:
Josef Bacik5d803662013-02-07 16:06:02 -05004104 if (ret == -ENOSPC &&
4105 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4106 struct btrfs_block_rsv *global_rsv =
4107 &root->fs_info->global_block_rsv;
4108
4109 if (block_rsv != global_rsv &&
4110 !block_rsv_use_bytes(global_rsv, orig_bytes))
4111 ret = 0;
4112 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004113 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004114 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004115 space_info->flush = 0;
4116 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004117 spin_unlock(&space_info->lock);
4118 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004119 return ret;
4120}
4121
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122static struct btrfs_block_rsv *get_block_rsv(
4123 const struct btrfs_trans_handle *trans,
4124 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004125{
Josef Bacik4c13d752011-08-30 11:31:29 -04004126 struct btrfs_block_rsv *block_rsv = NULL;
4127
Josef Bacik0e721102012-06-26 16:13:18 -04004128 if (root->ref_cows)
4129 block_rsv = trans->block_rsv;
4130
4131 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004132 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004133
4134 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004135 block_rsv = root->block_rsv;
4136
4137 if (!block_rsv)
4138 block_rsv = &root->fs_info->empty_block_rsv;
4139
4140 return block_rsv;
4141}
4142
4143static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4144 u64 num_bytes)
4145{
4146 int ret = -ENOSPC;
4147 spin_lock(&block_rsv->lock);
4148 if (block_rsv->reserved >= num_bytes) {
4149 block_rsv->reserved -= num_bytes;
4150 if (block_rsv->reserved < block_rsv->size)
4151 block_rsv->full = 0;
4152 ret = 0;
4153 }
4154 spin_unlock(&block_rsv->lock);
4155 return ret;
4156}
4157
4158static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4159 u64 num_bytes, int update_size)
4160{
4161 spin_lock(&block_rsv->lock);
4162 block_rsv->reserved += num_bytes;
4163 if (update_size)
4164 block_rsv->size += num_bytes;
4165 else if (block_rsv->reserved >= block_rsv->size)
4166 block_rsv->full = 1;
4167 spin_unlock(&block_rsv->lock);
4168}
4169
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004170static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4171 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004172 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004173{
4174 struct btrfs_space_info *space_info = block_rsv->space_info;
4175
4176 spin_lock(&block_rsv->lock);
4177 if (num_bytes == (u64)-1)
4178 num_bytes = block_rsv->size;
4179 block_rsv->size -= num_bytes;
4180 if (block_rsv->reserved >= block_rsv->size) {
4181 num_bytes = block_rsv->reserved - block_rsv->size;
4182 block_rsv->reserved = block_rsv->size;
4183 block_rsv->full = 1;
4184 } else {
4185 num_bytes = 0;
4186 }
4187 spin_unlock(&block_rsv->lock);
4188
4189 if (num_bytes > 0) {
4190 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004191 spin_lock(&dest->lock);
4192 if (!dest->full) {
4193 u64 bytes_to_add;
4194
4195 bytes_to_add = dest->size - dest->reserved;
4196 bytes_to_add = min(num_bytes, bytes_to_add);
4197 dest->reserved += bytes_to_add;
4198 if (dest->reserved >= dest->size)
4199 dest->full = 1;
4200 num_bytes -= bytes_to_add;
4201 }
4202 spin_unlock(&dest->lock);
4203 }
4204 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004205 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004206 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004207 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004208 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004209 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004210 spin_unlock(&space_info->lock);
4211 }
4212 }
4213}
4214
4215static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4216 struct btrfs_block_rsv *dst, u64 num_bytes)
4217{
4218 int ret;
4219
4220 ret = block_rsv_use_bytes(src, num_bytes);
4221 if (ret)
4222 return ret;
4223
4224 block_rsv_add_bytes(dst, num_bytes, 1);
4225 return 0;
4226}
4227
Miao Xie66d8f3d2012-09-06 04:02:28 -06004228void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004229{
4230 memset(rsv, 0, sizeof(*rsv));
4231 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004232 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004233}
4234
Miao Xie66d8f3d2012-09-06 04:02:28 -06004235struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4236 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004237{
4238 struct btrfs_block_rsv *block_rsv;
4239 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004240
4241 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4242 if (!block_rsv)
4243 return NULL;
4244
Miao Xie66d8f3d2012-09-06 04:02:28 -06004245 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004246 block_rsv->space_info = __find_space_info(fs_info,
4247 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004248 return block_rsv;
4249}
4250
4251void btrfs_free_block_rsv(struct btrfs_root *root,
4252 struct btrfs_block_rsv *rsv)
4253{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004254 if (!rsv)
4255 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004256 btrfs_block_rsv_release(root, rsv, (u64)-1);
4257 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004258}
4259
Miao Xie08e007d2012-10-16 11:33:38 +00004260int btrfs_block_rsv_add(struct btrfs_root *root,
4261 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4262 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004263{
4264 int ret;
4265
4266 if (num_bytes == 0)
4267 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004268
Miao Xie61b520a2011-11-10 20:45:05 -05004269 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004270 if (!ret) {
4271 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4272 return 0;
4273 }
4274
Yan, Zhengf0486c62010-05-16 10:46:25 -04004275 return ret;
4276}
4277
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004278int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004279 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004280{
4281 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004282 int ret = -ENOSPC;
4283
4284 if (!block_rsv)
4285 return 0;
4286
4287 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004288 num_bytes = div_factor(block_rsv->size, min_factor);
4289 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004290 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004291 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004292
Josef Bacik36ba0222011-10-18 12:15:48 -04004293 return ret;
4294}
4295
Miao Xie08e007d2012-10-16 11:33:38 +00004296int btrfs_block_rsv_refill(struct btrfs_root *root,
4297 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4298 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004299{
4300 u64 num_bytes = 0;
4301 int ret = -ENOSPC;
4302
4303 if (!block_rsv)
4304 return 0;
4305
4306 spin_lock(&block_rsv->lock);
4307 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004308 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004309 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004310 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004311 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004312 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004313
Yan, Zhengf0486c62010-05-16 10:46:25 -04004314 if (!ret)
4315 return 0;
4316
Miao Xieaa38a712011-11-18 17:43:00 +08004317 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004318 if (!ret) {
4319 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004320 return 0;
4321 }
4322
Josef Bacik13553e52011-08-08 13:33:21 -04004323 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004324}
4325
4326int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4327 struct btrfs_block_rsv *dst_rsv,
4328 u64 num_bytes)
4329{
4330 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4331}
4332
4333void btrfs_block_rsv_release(struct btrfs_root *root,
4334 struct btrfs_block_rsv *block_rsv,
4335 u64 num_bytes)
4336{
4337 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4338 if (global_rsv->full || global_rsv == block_rsv ||
4339 block_rsv->space_info != global_rsv->space_info)
4340 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004341 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4342 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004343}
4344
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004345/*
4346 * helper to calculate size of global block reservation.
4347 * the desired value is sum of space used by extent tree,
4348 * checksum tree and root tree
4349 */
4350static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4351{
4352 struct btrfs_space_info *sinfo;
4353 u64 num_bytes;
4354 u64 meta_used;
4355 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004356 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004357
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004358 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4359 spin_lock(&sinfo->lock);
4360 data_used = sinfo->bytes_used;
4361 spin_unlock(&sinfo->lock);
4362
4363 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4364 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004365 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4366 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004367 meta_used = sinfo->bytes_used;
4368 spin_unlock(&sinfo->lock);
4369
4370 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4371 csum_size * 2;
4372 num_bytes += div64_u64(data_used + meta_used, 50);
4373
4374 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004375 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004376
4377 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4378}
4379
4380static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4381{
4382 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4383 struct btrfs_space_info *sinfo = block_rsv->space_info;
4384 u64 num_bytes;
4385
4386 num_bytes = calc_global_metadata_size(fs_info);
4387
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004388 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004389 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004390
4391 block_rsv->size = num_bytes;
4392
4393 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004394 sinfo->bytes_reserved + sinfo->bytes_readonly +
4395 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004396
4397 if (sinfo->total_bytes > num_bytes) {
4398 num_bytes = sinfo->total_bytes - num_bytes;
4399 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004400 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004401 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004402 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004403 }
4404
4405 if (block_rsv->reserved >= block_rsv->size) {
4406 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004407 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004408 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004409 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004410 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004411 block_rsv->reserved = block_rsv->size;
4412 block_rsv->full = 1;
4413 }
David Sterba182608c2011-05-05 13:13:16 +02004414
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004415 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004416 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004417}
4418
Yan, Zhengf0486c62010-05-16 10:46:25 -04004419static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4420{
4421 struct btrfs_space_info *space_info;
4422
4423 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4424 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004425
4426 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004427 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004428 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004429 fs_info->trans_block_rsv.space_info = space_info;
4430 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004431 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004432
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004433 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4434 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4435 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4436 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004437 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004438
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004439 update_global_block_rsv(fs_info);
4440}
4441
4442static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4443{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004444 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4445 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004446 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4447 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4448 WARN_ON(fs_info->trans_block_rsv.size > 0);
4449 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4450 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4451 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004452 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4453 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004454}
4455
Yan, Zhenga22285a2010-05-16 10:48:46 -04004456void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4457 struct btrfs_root *root)
4458{
Josef Bacik0e721102012-06-26 16:13:18 -04004459 if (!trans->block_rsv)
4460 return;
4461
Yan, Zhenga22285a2010-05-16 10:48:46 -04004462 if (!trans->bytes_reserved)
4463 return;
4464
Chris Masone77266e2012-02-24 10:39:05 -05004465 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004466 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004467 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004468 trans->bytes_reserved = 0;
4469}
4470
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004471/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004472int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4473 struct inode *inode)
4474{
4475 struct btrfs_root *root = BTRFS_I(inode)->root;
4476 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4477 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4478
4479 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004480 * We need to hold space in order to delete our orphan item once we've
4481 * added it, so this takes the reservation so we can release it later
4482 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004483 */
Chris Masonff5714c2011-05-28 07:00:39 -04004484 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004485 trace_btrfs_space_reservation(root->fs_info, "orphan",
4486 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004487 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4488}
4489
4490void btrfs_orphan_release_metadata(struct inode *inode)
4491{
4492 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004493 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004494 trace_btrfs_space_reservation(root->fs_info, "orphan",
4495 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004496 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4497}
4498
Yan, Zhenga22285a2010-05-16 10:48:46 -04004499int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4500 struct btrfs_pending_snapshot *pending)
4501{
4502 struct btrfs_root *root = pending->root;
4503 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4504 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4505 /*
Miao Xie48c03c42012-09-06 04:03:56 -06004506 * two for root back/forward refs, two for directory entries,
4507 * one for root of the snapshot and one for parent inode.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004508 */
Miao Xie48c03c42012-09-06 04:03:56 -06004509 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004510 dst_rsv->space_info = src_rsv->space_info;
4511 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4512}
4513
Josef Bacik7709cde2011-08-04 10:25:02 -04004514/**
4515 * drop_outstanding_extent - drop an outstanding extent
4516 * @inode: the inode we're dropping the extent for
4517 *
4518 * This is called when we are freeing up an outstanding extent, either called
4519 * after an error or after an extent is written. This will return the number of
4520 * reserved extents that need to be freed. This must be called with
4521 * BTRFS_I(inode)->lock held.
4522 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004523static unsigned drop_outstanding_extent(struct inode *inode)
4524{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004525 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004526 unsigned dropped_extents = 0;
4527
Josef Bacik9e0baf62011-07-15 15:16:44 +00004528 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4529 BTRFS_I(inode)->outstanding_extents--;
4530
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004531 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004532 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4533 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004534 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004535
Josef Bacik9e0baf62011-07-15 15:16:44 +00004536 /*
4537 * If we have more or the same amount of outsanding extents than we have
4538 * reserved then we need to leave the reserved extents count alone.
4539 */
4540 if (BTRFS_I(inode)->outstanding_extents >=
4541 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004542 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004543
4544 dropped_extents = BTRFS_I(inode)->reserved_extents -
4545 BTRFS_I(inode)->outstanding_extents;
4546 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004547 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004548}
4549
Josef Bacik7709cde2011-08-04 10:25:02 -04004550/**
4551 * calc_csum_metadata_size - return the amount of metada space that must be
4552 * reserved/free'd for the given bytes.
4553 * @inode: the inode we're manipulating
4554 * @num_bytes: the number of bytes in question
4555 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4556 *
4557 * This adjusts the number of csum_bytes in the inode and then returns the
4558 * correct amount of metadata that must either be reserved or freed. We
4559 * calculate how many checksums we can fit into one leaf and then divide the
4560 * number of bytes that will need to be checksumed by this value to figure out
4561 * how many checksums will be required. If we are adding bytes then the number
4562 * may go up and we will return the number of additional bytes that must be
4563 * reserved. If it is going down we will return the number of bytes that must
4564 * be freed.
4565 *
4566 * This must be called with BTRFS_I(inode)->lock held.
4567 */
4568static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4569 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004570{
Josef Bacik7709cde2011-08-04 10:25:02 -04004571 struct btrfs_root *root = BTRFS_I(inode)->root;
4572 u64 csum_size;
4573 int num_csums_per_leaf;
4574 int num_csums;
4575 int old_csums;
4576
4577 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4578 BTRFS_I(inode)->csum_bytes == 0)
4579 return 0;
4580
4581 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4582 if (reserve)
4583 BTRFS_I(inode)->csum_bytes += num_bytes;
4584 else
4585 BTRFS_I(inode)->csum_bytes -= num_bytes;
4586 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4587 num_csums_per_leaf = (int)div64_u64(csum_size,
4588 sizeof(struct btrfs_csum_item) +
4589 sizeof(struct btrfs_disk_key));
4590 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4591 num_csums = num_csums + num_csums_per_leaf - 1;
4592 num_csums = num_csums / num_csums_per_leaf;
4593
4594 old_csums = old_csums + num_csums_per_leaf - 1;
4595 old_csums = old_csums / num_csums_per_leaf;
4596
4597 /* No change, no need to reserve more */
4598 if (old_csums == num_csums)
4599 return 0;
4600
4601 if (reserve)
4602 return btrfs_calc_trans_metadata_size(root,
4603 num_csums - old_csums);
4604
4605 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004606}
4607
4608int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4609{
4610 struct btrfs_root *root = BTRFS_I(inode)->root;
4611 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004612 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004613 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004614 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004615 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004616 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004617 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004618 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004619
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004620 /* If we are a free space inode we need to not flush since we will be in
4621 * the middle of a transaction commit. We also don't need the delalloc
4622 * mutex since we won't race with anybody. We need this mostly to make
4623 * lockdep shut its filthy mouth.
4624 */
4625 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004626 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004627 delalloc_lock = false;
4628 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004629
Miao Xie08e007d2012-10-16 11:33:38 +00004630 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4631 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004632 schedule_timeout(1);
4633
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004634 if (delalloc_lock)
4635 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4636
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004637 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004638
Josef Bacik9e0baf62011-07-15 15:16:44 +00004639 spin_lock(&BTRFS_I(inode)->lock);
4640 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004641
Josef Bacik9e0baf62011-07-15 15:16:44 +00004642 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004643 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004644 nr_extents = BTRFS_I(inode)->outstanding_extents -
4645 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004646
4647 /*
4648 * Add an item to reserve for updating the inode when we complete the
4649 * delalloc io.
4650 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004651 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4652 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004653 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004654 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004655 }
4656
4657 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004658 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004659 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004660 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004661
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004662 if (root->fs_info->quota_enabled)
Arne Jansenc5567232011-09-14 15:44:05 +02004663 ret = btrfs_qgroup_reserve(root, num_bytes +
4664 nr_extents * root->leafsize);
Arne Jansenc5567232011-09-14 15:44:05 +02004665
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004666 /*
4667 * ret != 0 here means the qgroup reservation failed, we go straight to
4668 * the shared error handling then.
4669 */
4670 if (ret == 0)
4671 ret = reserve_metadata_bytes(root, block_rsv,
4672 to_reserve, flush);
4673
Josef Bacik9e0baf62011-07-15 15:16:44 +00004674 if (ret) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004675 u64 to_free = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004676 unsigned dropped;
Josef Bacik7ed49f12011-08-19 15:45:52 -04004677
Josef Bacik7709cde2011-08-04 10:25:02 -04004678 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004679 dropped = drop_outstanding_extent(inode);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004680 /*
Josef Bacik660d3f62011-12-09 11:18:51 -05004681 * If the inodes csum_bytes is the same as the original
4682 * csum_bytes then we know we haven't raced with any free()ers
4683 * so we can just reduce our inodes csum bytes and carry on.
4684 * Otherwise we have to do the normal free thing to account for
4685 * the case that the free side didn't free up its reserve
4686 * because of this outstanding reservation.
Josef Bacik7ed49f12011-08-19 15:45:52 -04004687 */
Josef Bacik660d3f62011-12-09 11:18:51 -05004688 if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4689 calc_csum_metadata_size(inode, num_bytes, 0);
4690 else
4691 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4692 spin_unlock(&BTRFS_I(inode)->lock);
4693 if (dropped)
4694 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4695
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004696 if (to_free) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004697 btrfs_block_rsv_release(root, block_rsv, to_free);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004698 trace_btrfs_space_reservation(root->fs_info,
4699 "delalloc",
4700 btrfs_ino(inode),
4701 to_free, 0);
4702 }
Miao Xie4b5829a2012-12-05 10:53:25 +00004703 if (root->fs_info->quota_enabled) {
4704 btrfs_qgroup_free(root, num_bytes +
4705 nr_extents * root->leafsize);
4706 }
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004707 if (delalloc_lock)
4708 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004709 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004710 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004711
Josef Bacik660d3f62011-12-09 11:18:51 -05004712 spin_lock(&BTRFS_I(inode)->lock);
4713 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004714 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4715 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004716 nr_extents--;
4717 }
4718 BTRFS_I(inode)->reserved_extents += nr_extents;
4719 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004720
4721 if (delalloc_lock)
4722 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004723
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004724 if (to_reserve)
4725 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4726 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004727 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4728
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004729 return 0;
4730}
4731
Josef Bacik7709cde2011-08-04 10:25:02 -04004732/**
4733 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4734 * @inode: the inode to release the reservation for
4735 * @num_bytes: the number of bytes we're releasing
4736 *
4737 * This will release the metadata reservation for an inode. This can be called
4738 * once we complete IO for a given set of bytes to release their metadata
4739 * reservations.
4740 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004741void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4742{
4743 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004744 u64 to_free = 0;
4745 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004746
4747 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004748 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004749 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004750
Josef Bacik7709cde2011-08-04 10:25:02 -04004751 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4752 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004753 if (dropped > 0)
4754 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004755
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004756 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4757 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004758 if (root->fs_info->quota_enabled) {
4759 btrfs_qgroup_free(root, num_bytes +
4760 dropped * root->leafsize);
4761 }
4762
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004763 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4764 to_free);
4765}
4766
Josef Bacik7709cde2011-08-04 10:25:02 -04004767/**
4768 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4769 * @inode: inode we're writing to
4770 * @num_bytes: the number of bytes we want to allocate
4771 *
4772 * This will do the following things
4773 *
4774 * o reserve space in the data space info for num_bytes
4775 * o reserve space in the metadata space info based on number of outstanding
4776 * extents and how much csums will be needed
4777 * o add to the inodes ->delalloc_bytes
4778 * o add it to the fs_info's delalloc inodes list.
4779 *
4780 * This will return 0 for success and -ENOSPC if there is no space left.
4781 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004782int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4783{
4784 int ret;
4785
4786 ret = btrfs_check_data_free_space(inode, num_bytes);
4787 if (ret)
4788 return ret;
4789
4790 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4791 if (ret) {
4792 btrfs_free_reserved_data_space(inode, num_bytes);
4793 return ret;
4794 }
4795
4796 return 0;
4797}
4798
Josef Bacik7709cde2011-08-04 10:25:02 -04004799/**
4800 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4801 * @inode: inode we're releasing space for
4802 * @num_bytes: the number of bytes we want to free up
4803 *
4804 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4805 * called in the case that we don't need the metadata AND data reservations
4806 * anymore. So if there is an error or we insert an inline extent.
4807 *
4808 * This function will release the metadata space that was not used and will
4809 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4810 * list if there are no delalloc bytes left.
4811 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004812void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4813{
4814 btrfs_delalloc_release_metadata(inode, num_bytes);
4815 btrfs_free_reserved_data_space(inode, num_bytes);
4816}
4817
Liu Boc53d6132012-12-27 09:01:19 +00004818static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004819 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004820{
Josef Bacik0af3d002010-06-21 14:48:16 -04004821 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004822 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004823 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004824 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004825 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004826 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004827
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004828 /* block accounting for super block */
4829 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02004830 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004831 if (alloc)
4832 old_val += num_bytes;
4833 else
4834 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02004835 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004836 spin_unlock(&info->delalloc_lock);
4837
Chris Masond3977122009-01-05 21:25:51 -05004838 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004839 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004840 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004841 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004842 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4843 BTRFS_BLOCK_GROUP_RAID1 |
4844 BTRFS_BLOCK_GROUP_RAID10))
4845 factor = 2;
4846 else
4847 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004848 /*
4849 * If this block group has free space cache written out, we
4850 * need to make sure to load it if we are removing space. This
4851 * is because we need the unpinning stage to actually add the
4852 * space back to the block group, otherwise we will leak space.
4853 */
4854 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00004855 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004856
Chris Masondb945352007-10-15 16:15:53 -04004857 byte_in_group = bytenr - cache->key.objectid;
4858 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004859
Josef Bacik25179202008-10-29 14:49:05 -04004860 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004861 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004862
Josef Bacik73bc1872011-10-03 14:07:49 -04004863 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04004864 cache->disk_cache_state < BTRFS_DC_CLEAR)
4865 cache->disk_cache_state = BTRFS_DC_CLEAR;
4866
Josef Bacik0f9dd462008-09-23 13:14:11 -04004867 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004868 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004869 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004870 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004871 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004872 btrfs_set_block_group_used(&cache->item, old_val);
4873 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004874 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004875 cache->space_info->bytes_used += num_bytes;
4876 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004877 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004878 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004879 } else {
Chris Masondb945352007-10-15 16:15:53 -04004880 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004881 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004882 cache->pinned += num_bytes;
4883 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004884 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004885 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004886 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004887 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004888
Yan, Zhengf0486c62010-05-16 10:46:25 -04004889 set_extent_dirty(info->pinned_extents,
4890 bytenr, bytenr + num_bytes - 1,
4891 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004892 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004893 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004894 total -= num_bytes;
4895 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004896 }
4897 return 0;
4898}
Chris Mason6324fbf2008-03-24 15:01:59 -04004899
Chris Masona061fc82008-05-07 11:43:44 -04004900static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4901{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004902 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004903 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004904
Liu Boa1897fd2012-12-27 09:01:23 +00004905 spin_lock(&root->fs_info->block_group_cache_lock);
4906 bytenr = root->fs_info->first_logical_byte;
4907 spin_unlock(&root->fs_info->block_group_cache_lock);
4908
4909 if (bytenr < (u64)-1)
4910 return bytenr;
4911
Josef Bacik0f9dd462008-09-23 13:14:11 -04004912 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4913 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004914 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004915
Yan Zhengd2fb3432008-12-11 16:30:39 -05004916 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004917 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004918
4919 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004920}
4921
Yan, Zhengf0486c62010-05-16 10:46:25 -04004922static int pin_down_extent(struct btrfs_root *root,
4923 struct btrfs_block_group_cache *cache,
4924 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004925{
Yan Zheng11833d62009-09-11 16:11:19 -04004926 spin_lock(&cache->space_info->lock);
4927 spin_lock(&cache->lock);
4928 cache->pinned += num_bytes;
4929 cache->space_info->bytes_pinned += num_bytes;
4930 if (reserved) {
4931 cache->reserved -= num_bytes;
4932 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004933 }
Yan Zheng11833d62009-09-11 16:11:19 -04004934 spin_unlock(&cache->lock);
4935 spin_unlock(&cache->space_info->lock);
4936
Yan, Zhengf0486c62010-05-16 10:46:25 -04004937 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4938 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004939 return 0;
4940}
Chris Mason9078a3e2007-04-26 16:46:15 -04004941
Yan, Zhengf0486c62010-05-16 10:46:25 -04004942/*
4943 * this function must be called within transaction
4944 */
4945int btrfs_pin_extent(struct btrfs_root *root,
4946 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004947{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004948 struct btrfs_block_group_cache *cache;
4949
4950 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004951 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04004952
4953 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4954
4955 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004956 return 0;
4957}
Zheng Yane8569812008-09-26 10:05:48 -04004958
Yan, Zhengf0486c62010-05-16 10:46:25 -04004959/*
Chris Masone688b722011-10-31 20:52:39 -04004960 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04004961 */
Liu Bodcfac412012-12-27 09:01:20 +00004962int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b722011-10-31 20:52:39 -04004963 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004964{
Chris Masone688b722011-10-31 20:52:39 -04004965 struct btrfs_block_group_cache *cache;
4966
4967 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004968 BUG_ON(!cache); /* Logic error */
Chris Masone688b722011-10-31 20:52:39 -04004969
4970 /*
4971 * pull in the free space cache (if any) so that our pin
4972 * removes the free space from the cache. We have load_only set
4973 * to one because the slow code to read in the free extents does check
4974 * the pinned extents.
4975 */
Liu Bof6373bf2012-12-27 09:01:18 +00004976 cache_block_group(cache, 1);
Chris Masone688b722011-10-31 20:52:39 -04004977
4978 pin_down_extent(root, cache, bytenr, num_bytes, 0);
4979
4980 /* remove us from the free space cache (if we're there at all) */
4981 btrfs_remove_free_space(cache, bytenr, num_bytes);
4982 btrfs_put_block_group(cache);
4983 return 0;
4984}
4985
Josef Bacikfb25e912011-07-26 17:00:46 -04004986/**
4987 * btrfs_update_reserved_bytes - update the block_group and space info counters
4988 * @cache: The cache we are manipulating
4989 * @num_bytes: The number of bytes in question
4990 * @reserve: One of the reservation enums
4991 *
4992 * This is called by the allocator when it reserves space, or by somebody who is
4993 * freeing space that was never actually used on disk. For example if you
4994 * reserve some space for a new leaf in transaction A and before transaction A
4995 * commits you free that leaf, you call this with reserve set to 0 in order to
4996 * clear the reservation.
4997 *
4998 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4999 * ENOSPC accounting. For data we handle the reservation through clearing the
5000 * delalloc bits in the io_tree. We have to do this since we could end up
5001 * allocating less disk space for the amount of data we have reserved in the
5002 * case of compression.
5003 *
5004 * If this is a reservation and the block group has become read only we cannot
5005 * make the reservation and return -EAGAIN, otherwise this function always
5006 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005007 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005008static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5009 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005010{
Josef Bacikfb25e912011-07-26 17:00:46 -04005011 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005012 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005013
Josef Bacikfb25e912011-07-26 17:00:46 -04005014 spin_lock(&space_info->lock);
5015 spin_lock(&cache->lock);
5016 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005017 if (cache->ro) {
5018 ret = -EAGAIN;
5019 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005020 cache->reserved += num_bytes;
5021 space_info->bytes_reserved += num_bytes;
5022 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005023 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005024 "space_info", space_info->flags,
5025 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005026 space_info->bytes_may_use -= num_bytes;
5027 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005028 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005029 } else {
5030 if (cache->ro)
5031 space_info->bytes_readonly += num_bytes;
5032 cache->reserved -= num_bytes;
5033 space_info->bytes_reserved -= num_bytes;
5034 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005035 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005036 spin_unlock(&cache->lock);
5037 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005038 return ret;
5039}
5040
Jeff Mahoney143bede2012-03-01 14:56:26 +01005041void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005042 struct btrfs_root *root)
5043{
5044 struct btrfs_fs_info *fs_info = root->fs_info;
5045 struct btrfs_caching_control *next;
5046 struct btrfs_caching_control *caching_ctl;
5047 struct btrfs_block_group_cache *cache;
5048
5049 down_write(&fs_info->extent_commit_sem);
5050
5051 list_for_each_entry_safe(caching_ctl, next,
5052 &fs_info->caching_block_groups, list) {
5053 cache = caching_ctl->block_group;
5054 if (block_group_cache_done(cache)) {
5055 cache->last_byte_to_unpin = (u64)-1;
5056 list_del_init(&caching_ctl->list);
5057 put_caching_control(caching_ctl);
5058 } else {
5059 cache->last_byte_to_unpin = caching_ctl->progress;
5060 }
5061 }
5062
5063 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5064 fs_info->pinned_extents = &fs_info->freed_extents[1];
5065 else
5066 fs_info->pinned_extents = &fs_info->freed_extents[0];
5067
5068 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005069
5070 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005071}
5072
5073static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5074{
5075 struct btrfs_fs_info *fs_info = root->fs_info;
5076 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005077 struct btrfs_space_info *space_info;
5078 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005079 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005080 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005081
5082 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005083 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005084 if (!cache ||
5085 start >= cache->key.objectid + cache->key.offset) {
5086 if (cache)
5087 btrfs_put_block_group(cache);
5088 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005089 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005090 }
5091
5092 len = cache->key.objectid + cache->key.offset - start;
5093 len = min(len, end + 1 - start);
5094
5095 if (start < cache->last_byte_to_unpin) {
5096 len = min(len, cache->last_byte_to_unpin - start);
5097 btrfs_add_free_space(cache, start, len);
5098 }
Josef Bacik25179202008-10-29 14:49:05 -04005099
Yan, Zhengf0486c62010-05-16 10:46:25 -04005100 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005101 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005102
Josef Bacik7b398f82012-10-22 15:52:28 -04005103 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005104 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005105 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005106 space_info->bytes_pinned -= len;
5107 if (cache->ro) {
5108 space_info->bytes_readonly += len;
5109 readonly = true;
5110 }
Josef Bacik25179202008-10-29 14:49:05 -04005111 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005112 if (!readonly && global_rsv->space_info == space_info) {
5113 spin_lock(&global_rsv->lock);
5114 if (!global_rsv->full) {
5115 len = min(len, global_rsv->size -
5116 global_rsv->reserved);
5117 global_rsv->reserved += len;
5118 space_info->bytes_may_use += len;
5119 if (global_rsv->reserved >= global_rsv->size)
5120 global_rsv->full = 1;
5121 }
5122 spin_unlock(&global_rsv->lock);
5123 }
5124 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005125 }
5126
5127 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005128 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005129 return 0;
5130}
5131
5132int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005133 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005134{
Yan Zheng11833d62009-09-11 16:11:19 -04005135 struct btrfs_fs_info *fs_info = root->fs_info;
5136 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005137 u64 start;
5138 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005139 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005140
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005141 if (trans->aborted)
5142 return 0;
5143
Yan Zheng11833d62009-09-11 16:11:19 -04005144 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5145 unpin = &fs_info->freed_extents[1];
5146 else
5147 unpin = &fs_info->freed_extents[0];
5148
Chris Masond3977122009-01-05 21:25:51 -05005149 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005150 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005151 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005152 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005153 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005154
Li Dongyang5378e602011-03-24 10:24:27 +00005155 if (btrfs_test_opt(root, DISCARD))
5156 ret = btrfs_discard_extent(root, start,
5157 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005158
Chris Mason1a5bc162007-10-15 16:15:26 -04005159 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005160 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005161 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005162 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005163
Chris Masone20d96d2007-03-22 12:13:20 -04005164 return 0;
5165}
5166
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005167static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5168 struct btrfs_root *root,
5169 u64 bytenr, u64 num_bytes, u64 parent,
5170 u64 root_objectid, u64 owner_objectid,
5171 u64 owner_offset, int refs_to_drop,
5172 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005173{
Chris Masone2fa7222007-03-12 16:22:34 -04005174 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005175 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005176 struct btrfs_fs_info *info = root->fs_info;
5177 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005178 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005179 struct btrfs_extent_item *ei;
5180 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005181 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005182 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005183 int extent_slot = 0;
5184 int found_extent = 0;
5185 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005186 u32 item_size;
5187 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05005188
Chris Mason5caf2a02007-04-02 11:20:42 -04005189 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005190 if (!path)
5191 return -ENOMEM;
5192
Chris Mason3c12ac72008-04-21 12:01:38 -04005193 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005194 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005195
5196 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5197 BUG_ON(!is_data && refs_to_drop != 1);
5198
5199 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5200 bytenr, num_bytes, parent,
5201 root_objectid, owner_objectid,
5202 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005203 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005204 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005205 while (extent_slot >= 0) {
5206 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005207 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005208 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005209 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005210 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5211 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005212 found_extent = 1;
5213 break;
5214 }
5215 if (path->slots[0] - extent_slot > 5)
5216 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005217 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005218 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005219#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5220 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5221 if (found_extent && item_size < sizeof(*ei))
5222 found_extent = 0;
5223#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005224 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005225 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005226 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005227 NULL, refs_to_drop,
5228 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005229 if (ret) {
5230 btrfs_abort_transaction(trans, extent_root, ret);
5231 goto out;
5232 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005233 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005234 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005235
5236 key.objectid = bytenr;
5237 key.type = BTRFS_EXTENT_ITEM_KEY;
5238 key.offset = num_bytes;
5239
Zheng Yan31840ae2008-09-23 13:14:14 -04005240 ret = btrfs_search_slot(trans, extent_root,
5241 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005242 if (ret) {
5243 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05005244 ", was looking for %llu\n", ret,
5245 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005246 if (ret > 0)
5247 btrfs_print_leaf(extent_root,
5248 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005249 }
David Sterba005d6422012-09-18 07:52:32 -06005250 if (ret < 0) {
5251 btrfs_abort_transaction(trans, extent_root, ret);
5252 goto out;
5253 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005254 extent_slot = path->slots[0];
5255 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005256 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005257 btrfs_print_leaf(extent_root, path->nodes[0]);
5258 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05005259 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005260 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05005261 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04005262 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05005263 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005264 (unsigned long long)owner_objectid,
5265 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005266 } else {
David Sterba005d6422012-09-18 07:52:32 -06005267 btrfs_abort_transaction(trans, extent_root, ret);
5268 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005269 }
Chris Mason5f39d392007-10-15 16:14:19 -04005270
5271 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005272 item_size = btrfs_item_size_nr(leaf, extent_slot);
5273#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5274 if (item_size < sizeof(*ei)) {
5275 BUG_ON(found_extent || extent_slot != path->slots[0]);
5276 ret = convert_extent_item_v0(trans, extent_root, path,
5277 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005278 if (ret < 0) {
5279 btrfs_abort_transaction(trans, extent_root, ret);
5280 goto out;
5281 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005282
David Sterbab3b4aa72011-04-21 01:20:15 +02005283 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005284 path->leave_spinning = 1;
5285
5286 key.objectid = bytenr;
5287 key.type = BTRFS_EXTENT_ITEM_KEY;
5288 key.offset = num_bytes;
5289
5290 ret = btrfs_search_slot(trans, extent_root, &key, path,
5291 -1, 1);
5292 if (ret) {
5293 printk(KERN_ERR "umm, got %d back from search"
5294 ", was looking for %llu\n", ret,
5295 (unsigned long long)bytenr);
5296 btrfs_print_leaf(extent_root, path->nodes[0]);
5297 }
David Sterba005d6422012-09-18 07:52:32 -06005298 if (ret < 0) {
5299 btrfs_abort_transaction(trans, extent_root, ret);
5300 goto out;
5301 }
5302
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005303 extent_slot = path->slots[0];
5304 leaf = path->nodes[0];
5305 item_size = btrfs_item_size_nr(leaf, extent_slot);
5306 }
5307#endif
5308 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005309 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005310 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005311 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5312 struct btrfs_tree_block_info *bi;
5313 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5314 bi = (struct btrfs_tree_block_info *)(ei + 1);
5315 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005316 }
5317
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005318 refs = btrfs_extent_refs(leaf, ei);
5319 BUG_ON(refs < refs_to_drop);
5320 refs -= refs_to_drop;
5321
5322 if (refs > 0) {
5323 if (extent_op)
5324 __run_delayed_extent_op(extent_op, leaf, ei);
5325 /*
5326 * In the case of inline back ref, reference count will
5327 * be updated by remove_extent_backref
5328 */
5329 if (iref) {
5330 BUG_ON(!found_extent);
5331 } else {
5332 btrfs_set_extent_refs(leaf, ei, refs);
5333 btrfs_mark_buffer_dirty(leaf);
5334 }
5335 if (found_extent) {
5336 ret = remove_extent_backref(trans, extent_root, path,
5337 iref, refs_to_drop,
5338 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005339 if (ret) {
5340 btrfs_abort_transaction(trans, extent_root, ret);
5341 goto out;
5342 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005343 }
5344 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005345 if (found_extent) {
5346 BUG_ON(is_data && refs_to_drop !=
5347 extent_data_ref_count(root, path, iref));
5348 if (iref) {
5349 BUG_ON(path->slots[0] != extent_slot);
5350 } else {
5351 BUG_ON(path->slots[0] != extent_slot + 1);
5352 path->slots[0] = extent_slot;
5353 num_to_del = 2;
5354 }
Chris Mason78fae272007-03-25 11:35:08 -04005355 }
Chris Masonb9473432009-03-13 11:00:37 -04005356
Chris Mason952fcca2008-02-18 16:33:44 -05005357 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5358 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005359 if (ret) {
5360 btrfs_abort_transaction(trans, extent_root, ret);
5361 goto out;
5362 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005363 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005364
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005365 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005366 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005367 if (ret) {
5368 btrfs_abort_transaction(trans, extent_root, ret);
5369 goto out;
5370 }
Chris Mason459931e2008-12-10 09:10:46 -05005371 }
5372
Liu Boc53d6132012-12-27 09:01:19 +00005373 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005374 if (ret) {
5375 btrfs_abort_transaction(trans, extent_root, ret);
5376 goto out;
5377 }
Chris Masona28ec192007-03-06 20:08:01 -05005378 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005379out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005380 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005381 return ret;
5382}
5383
5384/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005385 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005386 * delayed ref for that extent as well. This searches the delayed ref tree for
5387 * a given extent, and if there are no other delayed refs to be processed, it
5388 * removes it from the tree.
5389 */
5390static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5391 struct btrfs_root *root, u64 bytenr)
5392{
5393 struct btrfs_delayed_ref_head *head;
5394 struct btrfs_delayed_ref_root *delayed_refs;
5395 struct btrfs_delayed_ref_node *ref;
5396 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005397 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005398
5399 delayed_refs = &trans->transaction->delayed_refs;
5400 spin_lock(&delayed_refs->lock);
5401 head = btrfs_find_delayed_ref_head(trans, bytenr);
5402 if (!head)
5403 goto out;
5404
5405 node = rb_prev(&head->node.rb_node);
5406 if (!node)
5407 goto out;
5408
5409 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5410
5411 /* there are still entries for this ref, we can't drop it */
5412 if (ref->bytenr == bytenr)
5413 goto out;
5414
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005415 if (head->extent_op) {
5416 if (!head->must_insert_reserved)
5417 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005418 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005419 head->extent_op = NULL;
5420 }
5421
Chris Mason1887be62009-03-13 10:11:24 -04005422 /*
5423 * waiting for the lock here would deadlock. If someone else has it
5424 * locked they are already in the process of dropping it anyway
5425 */
5426 if (!mutex_trylock(&head->mutex))
5427 goto out;
5428
5429 /*
5430 * at this point we have a head with no other entries. Go
5431 * ahead and process it.
5432 */
5433 head->node.in_tree = 0;
5434 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005435
Chris Mason1887be62009-03-13 10:11:24 -04005436 delayed_refs->num_entries--;
5437
5438 /*
5439 * we don't take a ref on the node because we're removing it from the
5440 * tree, so we just steal the ref the tree was holding.
5441 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005442 delayed_refs->num_heads--;
5443 if (list_empty(&head->cluster))
5444 delayed_refs->num_heads_ready--;
5445
5446 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005447 spin_unlock(&delayed_refs->lock);
5448
Yan, Zhengf0486c62010-05-16 10:46:25 -04005449 BUG_ON(head->extent_op);
5450 if (head->must_insert_reserved)
5451 ret = 1;
5452
5453 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005454 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005455 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005456out:
5457 spin_unlock(&delayed_refs->lock);
5458 return 0;
5459}
5460
Yan, Zhengf0486c62010-05-16 10:46:25 -04005461void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5462 struct btrfs_root *root,
5463 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005464 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005465{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005466 struct btrfs_block_group_cache *cache = NULL;
5467 int ret;
5468
5469 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005470 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5471 buf->start, buf->len,
5472 parent, root->root_key.objectid,
5473 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005474 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005475 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005476 }
5477
5478 if (!last_ref)
5479 return;
5480
Yan, Zhengf0486c62010-05-16 10:46:25 -04005481 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005482
5483 if (btrfs_header_generation(buf) == trans->transid) {
5484 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5485 ret = check_ref_cleanup(trans, root, buf->start);
5486 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005487 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005488 }
5489
5490 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5491 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005492 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005493 }
5494
5495 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5496
5497 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005498 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005499 }
5500out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005501 /*
5502 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5503 * anymore.
5504 */
5505 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005506 btrfs_put_block_group(cache);
5507}
5508
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005509/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005510int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5511 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5512 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005513{
5514 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005515 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005516
Chris Mason56bec292009-03-13 10:10:06 -04005517 /*
5518 * tree log blocks never actually go into the extent allocation
5519 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005520 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005521 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5522 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005523 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005524 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005525 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005526 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005527 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5528 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005529 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005530 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005531 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005532 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5533 num_bytes,
5534 parent, root_objectid, owner,
5535 offset, BTRFS_DROP_DELAYED_REF,
5536 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005537 }
Chris Mason925baed2008-06-25 16:01:30 -04005538 return ret;
5539}
5540
Chris Mason87ee04e2007-11-30 11:30:34 -05005541static u64 stripe_align(struct btrfs_root *root, u64 val)
5542{
5543 u64 mask = ((u64)root->stripesize - 1);
5544 u64 ret = (val + mask) & ~mask;
5545 return ret;
5546}
5547
Chris Masonfec577f2007-02-26 10:40:21 -05005548/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005549 * when we wait for progress in the block group caching, its because
5550 * our allocation attempt failed at least once. So, we must sleep
5551 * and let some progress happen before we try again.
5552 *
5553 * This function will sleep at least once waiting for new free space to
5554 * show up, and then it will check the block group free space numbers
5555 * for our min num_bytes. Another option is to have it go ahead
5556 * and look in the rbtree for a free extent of a given size, but this
5557 * is a good start.
5558 */
5559static noinline int
5560wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5561 u64 num_bytes)
5562{
Yan Zheng11833d62009-09-11 16:11:19 -04005563 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005564
Yan Zheng11833d62009-09-11 16:11:19 -04005565 caching_ctl = get_caching_control(cache);
5566 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005567 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005568
Yan Zheng11833d62009-09-11 16:11:19 -04005569 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb6c2011-03-29 13:46:06 +08005570 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005571
5572 put_caching_control(caching_ctl);
5573 return 0;
5574}
5575
5576static noinline int
5577wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5578{
5579 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04005580
5581 caching_ctl = get_caching_control(cache);
5582 if (!caching_ctl)
5583 return 0;
5584
5585 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5586
5587 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005588 return 0;
5589}
5590
Liu Bo31e50222012-11-21 14:18:10 +00005591int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005592{
Ilya Dryomov7738a532012-03-27 17:09:17 +03005593 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00005594 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005595 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00005596 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005597 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00005598 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005599 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00005600 return BTRFS_RAID_RAID0;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005601 else
Miao Xiee6ec7162013-01-17 05:38:51 +00005602 return BTRFS_RAID_SINGLE;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005603}
5604
Ilya Dryomov7738a532012-03-27 17:09:17 +03005605static int get_block_group_index(struct btrfs_block_group_cache *cache)
5606{
Liu Bo31e50222012-11-21 14:18:10 +00005607 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005608}
5609
Josef Bacik817d52f2009-07-13 21:29:25 -04005610enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005611 LOOP_CACHING_NOWAIT = 0,
5612 LOOP_CACHING_WAIT = 1,
5613 LOOP_ALLOC_CHUNK = 2,
5614 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005615};
5616
5617/*
Chris Masonfec577f2007-02-26 10:40:21 -05005618 * walks the btree of allocated extents and find a hole of a given size.
5619 * The key ins is changed to record the hole:
5620 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005621 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005622 * ins->offset == number of blocks
5623 * Any available blocks before search_start are skipped.
5624 */
Chris Masond3977122009-01-05 21:25:51 -05005625static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005626 struct btrfs_root *orig_root,
5627 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005628 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005629 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005630{
Josef Bacik80eb2342008-10-29 14:49:05 -04005631 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005632 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005633 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005634 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005635 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005636 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005637 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005638 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005639 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005640 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005641 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5642 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005643 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005644 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005645 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005646 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005647 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005648
Chris Masondb945352007-10-15 16:15:53 -04005649 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005650 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005651 ins->objectid = 0;
5652 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005653
Josef Bacik3f7de032011-11-10 08:29:20 -05005654 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5655
Josef Bacik2552d172009-04-03 10:14:19 -04005656 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005657 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00005658 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005659 return -ENOSPC;
5660 }
Josef Bacik2552d172009-04-03 10:14:19 -04005661
Josef Bacik67377732010-09-16 16:19:09 -04005662 /*
5663 * If the space info is for both data and metadata it means we have a
5664 * small filesystem and we can't use the clustering stuff.
5665 */
5666 if (btrfs_mixed_space_info(space_info))
5667 use_cluster = false;
5668
Josef Bacik67377732010-09-16 16:19:09 -04005669 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005670 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005671 if (!btrfs_test_opt(root, SSD))
5672 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005673 }
5674
Josef Bacik67377732010-09-16 16:19:09 -04005675 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5676 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005677 last_ptr = &root->fs_info->data_alloc_cluster;
5678 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005679
Chris Mason239b14b2008-03-24 15:02:07 -04005680 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005681 spin_lock(&last_ptr->lock);
5682 if (last_ptr->block_group)
5683 hint_byte = last_ptr->window_start;
5684 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005685 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005686
Chris Masona061fc82008-05-07 11:43:44 -04005687 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005688 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005689
Josef Bacik817d52f2009-07-13 21:29:25 -04005690 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005691 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005692
Josef Bacik2552d172009-04-03 10:14:19 -04005693 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005694 block_group = btrfs_lookup_block_group(root->fs_info,
5695 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005696 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005697 /*
5698 * we don't want to use the block group if it doesn't match our
5699 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005700 *
5701 * However if we are re-searching with an ideal block group
5702 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005703 */
5704 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005705 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005706 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005707 if (list_empty(&block_group->list) ||
5708 block_group->ro) {
5709 /*
5710 * someone is removing this block group,
5711 * we can't jump into the have_block_group
5712 * target because our list pointers are not
5713 * valid
5714 */
5715 btrfs_put_block_group(block_group);
5716 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005717 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005718 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005719 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005720 }
Josef Bacik2552d172009-04-03 10:14:19 -04005721 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005722 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005723 }
Chris Mason42e70e72008-11-07 18:17:11 -05005724 }
Josef Bacik2552d172009-04-03 10:14:19 -04005725search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005726 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005727 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005728 list_for_each_entry(block_group, &space_info->block_groups[index],
5729 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005730 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005731 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005732
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005733 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005734 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005735 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005736
Chris Mason83a50de2010-12-13 15:06:46 -05005737 /*
5738 * this can happen if we end up cycling through all the
5739 * raid types, but we want to make sure we only allocate
5740 * for the proper type.
5741 */
5742 if (!block_group_bits(block_group, data)) {
5743 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5744 BTRFS_BLOCK_GROUP_RAID1 |
5745 BTRFS_BLOCK_GROUP_RAID10;
5746
5747 /*
5748 * if they asked for extra copies and this block group
5749 * doesn't provide them, bail. This does allow us to
5750 * fill raid0 from raid1.
5751 */
5752 if ((data & extra) && !(block_group->flags & extra))
5753 goto loop;
5754 }
5755
Josef Bacik2552d172009-04-03 10:14:19 -04005756have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05005757 cached = block_group_cache_done(block_group);
5758 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05005759 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00005760 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005761 BUG_ON(ret < 0);
5762 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005763 }
5764
Josef Bacikea6a4782008-11-20 12:16:16 -05005765 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005766 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005767
Josef Bacik0a243252009-09-11 16:11:20 -04005768 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005769 * Ok we want to try and use the cluster allocator, so
5770 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005771 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005772 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005773 /*
5774 * the refill lock keeps out other
5775 * people trying to start a new cluster
5776 */
5777 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005778 used_block_group = last_ptr->block_group;
5779 if (used_block_group != block_group &&
5780 (!used_block_group ||
5781 used_block_group->ro ||
5782 !block_group_bits(used_block_group, data))) {
5783 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04005784 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005785 }
Chris Mason44fb5512009-06-04 15:34:51 -04005786
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005787 if (used_block_group != block_group)
5788 btrfs_get_block_group(used_block_group);
5789
5790 offset = btrfs_alloc_from_cluster(used_block_group,
5791 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005792 if (offset) {
5793 /* we have a block, we're done */
5794 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005795 trace_btrfs_reserve_extent_cluster(root,
5796 block_group, search_start, num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005797 goto checks;
5798 }
5799
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005800 WARN_ON(last_ptr->block_group != used_block_group);
5801 if (used_block_group != block_group) {
5802 btrfs_put_block_group(used_block_group);
5803 used_block_group = block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005804 }
Chris Mason44fb5512009-06-04 15:34:51 -04005805refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005806 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005807 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5808 * set up a new clusters, so lets just skip it
5809 * and let the allocator find whatever block
5810 * it can find. If we reach this point, we
5811 * will have tried the cluster allocator
5812 * plenty of times and not have found
5813 * anything, so we are likely way too
5814 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005815 * anything.
5816 *
5817 * However, if the cluster is taken from the
5818 * current block group, release the cluster
5819 * first, so that we stand a better chance of
5820 * succeeding in the unclustered
5821 * allocation. */
5822 if (loop >= LOOP_NO_EMPTY_SIZE &&
5823 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005824 spin_unlock(&last_ptr->refill_lock);
5825 goto unclustered_alloc;
5826 }
5827
Chris Masonfa9c0d72009-04-03 09:47:43 -04005828 /*
5829 * this cluster didn't work out, free it and
5830 * start over
5831 */
5832 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5833
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005834 if (loop >= LOOP_NO_EMPTY_SIZE) {
5835 spin_unlock(&last_ptr->refill_lock);
5836 goto unclustered_alloc;
5837 }
5838
Chris Masonfa9c0d72009-04-03 09:47:43 -04005839 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005840 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005841 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05005842 search_start, num_bytes,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005843 empty_cluster + empty_size);
5844 if (ret == 0) {
5845 /*
5846 * now pull our allocation out of this
5847 * cluster
5848 */
5849 offset = btrfs_alloc_from_cluster(block_group,
5850 last_ptr, num_bytes,
5851 search_start);
5852 if (offset) {
5853 /* we found one, proceed */
5854 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005855 trace_btrfs_reserve_extent_cluster(root,
5856 block_group, search_start,
5857 num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005858 goto checks;
5859 }
Josef Bacik0a243252009-09-11 16:11:20 -04005860 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5861 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005862 spin_unlock(&last_ptr->refill_lock);
5863
Josef Bacik0a243252009-09-11 16:11:20 -04005864 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005865 wait_block_group_cache_progress(block_group,
5866 num_bytes + empty_cluster + empty_size);
5867 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005868 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005869
Chris Masonfa9c0d72009-04-03 09:47:43 -04005870 /*
5871 * at this point we either didn't find a cluster
5872 * or we weren't able to allocate a block from our
5873 * cluster. Free the cluster we've been trying
5874 * to use, and go to the next block group
5875 */
Josef Bacik0a243252009-09-11 16:11:20 -04005876 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005877 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005878 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005879 }
5880
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005881unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005882 spin_lock(&block_group->free_space_ctl->tree_lock);
5883 if (cached &&
5884 block_group->free_space_ctl->free_space <
5885 num_bytes + empty_cluster + empty_size) {
5886 spin_unlock(&block_group->free_space_ctl->tree_lock);
5887 goto loop;
5888 }
5889 spin_unlock(&block_group->free_space_ctl->tree_lock);
5890
Josef Bacik6226cb02009-04-03 10:14:18 -04005891 offset = btrfs_find_space_for_alloc(block_group, search_start,
5892 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005893 /*
5894 * If we didn't find a chunk, and we haven't failed on this
5895 * block group before, and this block group is in the middle of
5896 * caching and we are ok with waiting, then go ahead and wait
5897 * for progress to be made, and set failed_alloc to true.
5898 *
5899 * If failed_alloc is true then we've already waited on this
5900 * block group once and should move on to the next block group.
5901 */
5902 if (!offset && !failed_alloc && !cached &&
5903 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005904 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005905 num_bytes + empty_size);
5906 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005907 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005908 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08005909 if (!cached)
5910 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005911 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005912 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005913checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005914 search_start = stripe_align(root, offset);
Chris Masone37c9e62007-05-09 20:13:14 -04005915
Josef Bacik2552d172009-04-03 10:14:19 -04005916 /* move on to the next group */
5917 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005918 used_block_group->key.objectid + used_block_group->key.offset) {
5919 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005920 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005921 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005922
Josef Bacik6226cb02009-04-03 10:14:18 -04005923 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005924 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04005925 search_start - offset);
5926 BUG_ON(offset > search_start);
5927
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005928 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04005929 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005930 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005931 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005932 goto loop;
5933 }
Yan Zheng11833d62009-09-11 16:11:19 -04005934
Josef Bacik2552d172009-04-03 10:14:19 -04005935 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005936 ins->objectid = search_start;
5937 ins->offset = num_bytes;
5938
Josef Bacik3f7de032011-11-10 08:29:20 -05005939 trace_btrfs_reserve_extent(orig_root, block_group,
5940 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005941 if (used_block_group != block_group)
5942 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005943 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005944 break;
5945loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005946 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005947 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005948 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005949 if (used_block_group != block_group)
5950 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005951 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005952 }
5953 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005954
Miao Xie60d2adb2011-09-09 17:34:35 +08005955 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
5956 goto search;
5957
Yan, Zhengb742bb82010-05-16 10:46:24 -04005958 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5959 goto search;
5960
Josef Bacik285ff5a2012-01-13 15:27:45 -05005961 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005962 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5963 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005964 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5965 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5966 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5967 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005968 */
Josef Bacik723bda22011-05-27 16:11:38 -04005969 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005970 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04005971 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005972 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04005973 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04005974 CHUNK_ALLOC_FORCE);
5975 /*
5976 * Do not bail out on ENOSPC since we
5977 * can do more things.
5978 */
5979 if (ret < 0 && ret != -ENOSPC) {
5980 btrfs_abort_transaction(trans,
5981 root, ret);
5982 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04005983 }
Josef Bacik723bda22011-05-27 16:11:38 -04005984 }
5985
5986 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005987 empty_size = 0;
5988 empty_cluster = 0;
5989 }
Chris Mason42e70e72008-11-07 18:17:11 -05005990
Josef Bacik723bda22011-05-27 16:11:38 -04005991 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005992 } else if (!ins->objectid) {
5993 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005994 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005995 ret = 0;
5996 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005997out:
Chris Mason0b86a832008-03-24 15:01:56 -04005998
Chris Mason0f70abe2007-02-28 16:46:22 -05005999 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006000}
Chris Masonec44a352008-04-28 15:29:52 -04006001
Josef Bacik9ed74f22009-09-11 16:12:44 -04006002static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6003 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006004{
6005 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006006 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006007
Josef Bacik9ed74f22009-09-11 16:12:44 -04006008 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006009 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6010 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006011 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006012 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006013 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006014 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006015 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6016 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006017 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006018 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006019 (unsigned long long)info->bytes_pinned,
6020 (unsigned long long)info->bytes_reserved,
6021 (unsigned long long)info->bytes_may_use,
6022 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006023 spin_unlock(&info->lock);
6024
6025 if (!dump_block_groups)
6026 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006027
Josef Bacik80eb2342008-10-29 14:49:05 -04006028 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006029again:
6030 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006031 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006032 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006033 (unsigned long long)cache->key.objectid,
6034 (unsigned long long)cache->key.offset,
6035 (unsigned long long)btrfs_block_group_used(&cache->item),
6036 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006037 (unsigned long long)cache->reserved,
6038 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006039 btrfs_dump_free_space(cache, bytes);
6040 spin_unlock(&cache->lock);
6041 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006042 if (++index < BTRFS_NR_RAID_TYPES)
6043 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006044 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006045}
Zheng Yane8569812008-09-26 10:05:48 -04006046
Yan Zheng11833d62009-09-11 16:11:19 -04006047int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6048 struct btrfs_root *root,
6049 u64 num_bytes, u64 min_alloc_size,
6050 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006051 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006052{
Miao Xie9e622d62012-01-26 15:01:12 -05006053 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006054 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006055
Josef Bacik6a632092009-02-20 11:00:09 -05006056 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006057again:
Chris Masondb945352007-10-15 16:15:53 -04006058 WARN_ON(num_bytes < root->sectorsize);
6059 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006060 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006061
Miao Xie9e622d62012-01-26 15:01:12 -05006062 if (ret == -ENOSPC) {
6063 if (!final_tried) {
6064 num_bytes = num_bytes >> 1;
6065 num_bytes = num_bytes & ~(root->sectorsize - 1);
6066 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006067 if (num_bytes == min_alloc_size)
6068 final_tried = true;
6069 goto again;
6070 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6071 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006072
Miao Xie9e622d62012-01-26 15:01:12 -05006073 sinfo = __find_space_info(root->fs_info, data);
6074 printk(KERN_ERR "btrfs allocation failed flags %llu, "
6075 "wanted %llu\n", (unsigned long long)data,
6076 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006077 if (sinfo)
6078 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006079 }
Chris Mason925baed2008-06-25 16:01:30 -04006080 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006081
liubo1abe9b82011-03-24 11:18:59 +00006082 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6083
Josef Bacik0f9dd462008-09-23 13:14:11 -04006084 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006085}
6086
Chris Masone688b722011-10-31 20:52:39 -04006087static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6088 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006089{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006090 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006091 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006092
Josef Bacik0f9dd462008-09-23 13:14:11 -04006093 cache = btrfs_lookup_block_group(root->fs_info, start);
6094 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05006095 printk(KERN_ERR "Unable to find block group for %llu\n",
6096 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006097 return -ENOSPC;
6098 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006099
Li Dongyang5378e602011-03-24 10:24:27 +00006100 if (btrfs_test_opt(root, DISCARD))
6101 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006102
Chris Masone688b722011-10-31 20:52:39 -04006103 if (pin)
6104 pin_down_extent(root, cache, start, len, 1);
6105 else {
6106 btrfs_add_free_space(cache, start, len);
6107 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6108 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006109 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006110
liubo1abe9b82011-03-24 11:18:59 +00006111 trace_btrfs_reserved_extent_free(root, start, len);
6112
Chris Masone6dcd2d2008-07-17 12:53:50 -04006113 return ret;
6114}
6115
Chris Masone688b722011-10-31 20:52:39 -04006116int btrfs_free_reserved_extent(struct btrfs_root *root,
6117 u64 start, u64 len)
6118{
6119 return __btrfs_free_reserved_extent(root, start, len, 0);
6120}
6121
6122int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6123 u64 start, u64 len)
6124{
6125 return __btrfs_free_reserved_extent(root, start, len, 1);
6126}
6127
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006128static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6129 struct btrfs_root *root,
6130 u64 parent, u64 root_objectid,
6131 u64 flags, u64 owner, u64 offset,
6132 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006133{
6134 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006135 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006136 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006137 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006138 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006139 struct extent_buffer *leaf;
6140 int type;
6141 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006142
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006143 if (parent > 0)
6144 type = BTRFS_SHARED_DATA_REF_KEY;
6145 else
6146 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006147
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006148 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006149
6150 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006151 if (!path)
6152 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006153
Chris Masonb9473432009-03-13 11:00:37 -04006154 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006155 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6156 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006157 if (ret) {
6158 btrfs_free_path(path);
6159 return ret;
6160 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006161
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006162 leaf = path->nodes[0];
6163 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006164 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006165 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6166 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6167 btrfs_set_extent_flags(leaf, extent_item,
6168 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006169
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006170 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6171 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6172 if (parent > 0) {
6173 struct btrfs_shared_data_ref *ref;
6174 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6175 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6176 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6177 } else {
6178 struct btrfs_extent_data_ref *ref;
6179 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6180 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6181 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6182 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6183 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6184 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006185
6186 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006187 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006188
Liu Boc53d6132012-12-27 09:01:19 +00006189 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006190 if (ret) { /* -ENOENT, logic error */
Chris Masond3977122009-01-05 21:25:51 -05006191 printk(KERN_ERR "btrfs update block group failed for %llu "
6192 "%llu\n", (unsigned long long)ins->objectid,
6193 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006194 BUG();
6195 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006196 return ret;
6197}
6198
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006199static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6200 struct btrfs_root *root,
6201 u64 parent, u64 root_objectid,
6202 u64 flags, struct btrfs_disk_key *key,
6203 int level, struct btrfs_key *ins)
6204{
6205 int ret;
6206 struct btrfs_fs_info *fs_info = root->fs_info;
6207 struct btrfs_extent_item *extent_item;
6208 struct btrfs_tree_block_info *block_info;
6209 struct btrfs_extent_inline_ref *iref;
6210 struct btrfs_path *path;
6211 struct extent_buffer *leaf;
6212 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
6213
6214 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006215 if (!path)
6216 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006217
6218 path->leave_spinning = 1;
6219 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6220 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006221 if (ret) {
6222 btrfs_free_path(path);
6223 return ret;
6224 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006225
6226 leaf = path->nodes[0];
6227 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6228 struct btrfs_extent_item);
6229 btrfs_set_extent_refs(leaf, extent_item, 1);
6230 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6231 btrfs_set_extent_flags(leaf, extent_item,
6232 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6233 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6234
6235 btrfs_set_tree_block_key(leaf, block_info, key);
6236 btrfs_set_tree_block_level(leaf, block_info, level);
6237
6238 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6239 if (parent > 0) {
6240 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6241 btrfs_set_extent_inline_ref_type(leaf, iref,
6242 BTRFS_SHARED_BLOCK_REF_KEY);
6243 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6244 } else {
6245 btrfs_set_extent_inline_ref_type(leaf, iref,
6246 BTRFS_TREE_BLOCK_REF_KEY);
6247 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6248 }
6249
6250 btrfs_mark_buffer_dirty(leaf);
6251 btrfs_free_path(path);
6252
Liu Boc53d6132012-12-27 09:01:19 +00006253 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006254 if (ret) { /* -ENOENT, logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006255 printk(KERN_ERR "btrfs update block group failed for %llu "
6256 "%llu\n", (unsigned long long)ins->objectid,
6257 (unsigned long long)ins->offset);
6258 BUG();
6259 }
6260 return ret;
6261}
6262
6263int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6264 struct btrfs_root *root,
6265 u64 root_objectid, u64 owner,
6266 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006267{
6268 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006269
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006270 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006271
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006272 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6273 ins->offset, 0,
6274 root_objectid, owner, offset,
6275 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006276 return ret;
6277}
Chris Masone02119d2008-09-05 16:13:11 -04006278
6279/*
6280 * this is used by the tree logging recovery code. It records that
6281 * an extent has been allocated and makes sure to clear the free
6282 * space cache bits as well
6283 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006284int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6285 struct btrfs_root *root,
6286 u64 root_objectid, u64 owner, u64 offset,
6287 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006288{
6289 int ret;
6290 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006291 struct btrfs_caching_control *caching_ctl;
6292 u64 start = ins->objectid;
6293 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006294
Chris Masone02119d2008-09-05 16:13:11 -04006295 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006296 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006297 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006298
Yan Zheng11833d62009-09-11 16:11:19 -04006299 if (!caching_ctl) {
6300 BUG_ON(!block_group_cache_done(block_group));
6301 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006302 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006303 } else {
6304 mutex_lock(&caching_ctl->mutex);
6305
6306 if (start >= caching_ctl->progress) {
6307 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006308 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006309 } else if (start + num_bytes <= caching_ctl->progress) {
6310 ret = btrfs_remove_free_space(block_group,
6311 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006312 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006313 } else {
6314 num_bytes = caching_ctl->progress - start;
6315 ret = btrfs_remove_free_space(block_group,
6316 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006317 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006318
6319 start = caching_ctl->progress;
6320 num_bytes = ins->objectid + ins->offset -
6321 caching_ctl->progress;
6322 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006323 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006324 }
6325
6326 mutex_unlock(&caching_ctl->mutex);
6327 put_caching_control(caching_ctl);
6328 }
6329
Josef Bacikfb25e912011-07-26 17:00:46 -04006330 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6331 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006332 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d72009-04-03 09:47:43 -04006333 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006334 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6335 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006336 return ret;
6337}
6338
Chris Mason65b51a02008-08-01 15:11:20 -04006339struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6340 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006341 u64 bytenr, u32 blocksize,
6342 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006343{
6344 struct extent_buffer *buf;
6345
6346 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6347 if (!buf)
6348 return ERR_PTR(-ENOMEM);
6349 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006350 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006351 btrfs_tree_lock(buf);
6352 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006353 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006354
6355 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006356 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006357
Chris Masond0c803c2008-09-11 16:17:57 -04006358 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006359 /*
6360 * we allow two log transactions at a time, use different
6361 * EXENT bit to differentiate dirty pages.
6362 */
6363 if (root->log_transid % 2 == 0)
6364 set_extent_dirty(&root->dirty_log_pages, buf->start,
6365 buf->start + buf->len - 1, GFP_NOFS);
6366 else
6367 set_extent_new(&root->dirty_log_pages, buf->start,
6368 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006369 } else {
6370 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6371 buf->start + buf->len - 1, GFP_NOFS);
6372 }
Chris Mason65b51a02008-08-01 15:11:20 -04006373 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006374 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006375 return buf;
6376}
6377
Yan, Zhengf0486c62010-05-16 10:46:25 -04006378static struct btrfs_block_rsv *
6379use_block_rsv(struct btrfs_trans_handle *trans,
6380 struct btrfs_root *root, u32 blocksize)
6381{
6382 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006383 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006384 int ret;
6385
6386 block_rsv = get_block_rsv(trans, root);
6387
6388 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006389 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6390 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006391 /*
6392 * If we couldn't reserve metadata bytes try and use some from
6393 * the global reserve.
6394 */
6395 if (ret && block_rsv != global_rsv) {
6396 ret = block_rsv_use_bytes(global_rsv, blocksize);
6397 if (!ret)
6398 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006399 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006400 } else if (ret) {
6401 return ERR_PTR(ret);
6402 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006403 return block_rsv;
6404 }
6405
6406 ret = block_rsv_use_bytes(block_rsv, blocksize);
6407 if (!ret)
6408 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006409 if (ret && !block_rsv->failfast) {
David Sterbadff51cd2011-06-14 12:52:17 +02006410 static DEFINE_RATELIMIT_STATE(_rs,
6411 DEFAULT_RATELIMIT_INTERVAL,
6412 /*DEFAULT_RATELIMIT_BURST*/ 2);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006413 if (__ratelimit(&_rs))
6414 WARN(1, KERN_DEBUG "btrfs: block rsv returned %d\n",
6415 ret);
Miao Xie08e007d2012-10-16 11:33:38 +00006416 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6417 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006418 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006419 return block_rsv;
6420 } else if (ret && block_rsv != global_rsv) {
6421 ret = block_rsv_use_bytes(global_rsv, blocksize);
6422 if (!ret)
6423 return global_rsv;
6424 }
6425 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006426
Yan, Zhengf0486c62010-05-16 10:46:25 -04006427 return ERR_PTR(-ENOSPC);
6428}
6429
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006430static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6431 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006432{
6433 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006434 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006435}
6436
Chris Masonfec577f2007-02-26 10:40:21 -05006437/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006438 * finds a free extent and does all the dirty work required for allocation
6439 * returns the key for the extent through ins, and a tree buffer for
6440 * the first block of the extent through buf.
6441 *
Chris Masonfec577f2007-02-26 10:40:21 -05006442 * returns the tree buffer or NULL.
6443 */
Chris Mason5f39d392007-10-15 16:14:19 -04006444struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006445 struct btrfs_root *root, u32 blocksize,
6446 u64 parent, u64 root_objectid,
6447 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006448 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006449{
Chris Masone2fa7222007-03-12 16:22:34 -04006450 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006451 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006452 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006453 u64 flags = 0;
6454 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006455
Yan, Zhengf0486c62010-05-16 10:46:25 -04006456
6457 block_rsv = use_block_rsv(trans, root, blocksize);
6458 if (IS_ERR(block_rsv))
6459 return ERR_CAST(block_rsv);
6460
6461 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006462 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006463 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006464 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006465 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006466 }
Chris Mason55c69072008-01-09 15:55:33 -05006467
Chris Mason4008c042009-02-12 14:09:45 -05006468 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6469 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006470 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006471
6472 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6473 if (parent == 0)
6474 parent = ins.objectid;
6475 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6476 } else
6477 BUG_ON(parent > 0);
6478
6479 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6480 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006481 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006482 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006483 if (key)
6484 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6485 else
6486 memset(&extent_op->key, 0, sizeof(extent_op->key));
6487 extent_op->flags_to_set = flags;
6488 extent_op->update_key = 1;
6489 extent_op->update_flags = 1;
6490 extent_op->is_data = 0;
6491
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006492 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6493 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006494 ins.offset, parent, root_objectid,
6495 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006496 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006497 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006498 }
Chris Masonfec577f2007-02-26 10:40:21 -05006499 return buf;
6500}
Chris Masona28ec192007-03-06 20:08:01 -05006501
Yan Zheng2c47e6052009-06-27 21:07:35 -04006502struct walk_control {
6503 u64 refs[BTRFS_MAX_LEVEL];
6504 u64 flags[BTRFS_MAX_LEVEL];
6505 struct btrfs_key update_progress;
6506 int stage;
6507 int level;
6508 int shared_level;
6509 int update_ref;
6510 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006511 int reada_slot;
6512 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006513 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006514};
6515
6516#define DROP_REFERENCE 1
6517#define UPDATE_BACKREF 2
6518
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006519static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6520 struct btrfs_root *root,
6521 struct walk_control *wc,
6522 struct btrfs_path *path)
6523{
6524 u64 bytenr;
6525 u64 generation;
6526 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006527 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006528 u32 nritems;
6529 u32 blocksize;
6530 struct btrfs_key key;
6531 struct extent_buffer *eb;
6532 int ret;
6533 int slot;
6534 int nread = 0;
6535
6536 if (path->slots[wc->level] < wc->reada_slot) {
6537 wc->reada_count = wc->reada_count * 2 / 3;
6538 wc->reada_count = max(wc->reada_count, 2);
6539 } else {
6540 wc->reada_count = wc->reada_count * 3 / 2;
6541 wc->reada_count = min_t(int, wc->reada_count,
6542 BTRFS_NODEPTRS_PER_BLOCK(root));
6543 }
6544
6545 eb = path->nodes[wc->level];
6546 nritems = btrfs_header_nritems(eb);
6547 blocksize = btrfs_level_size(root, wc->level - 1);
6548
6549 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6550 if (nread >= wc->reada_count)
6551 break;
6552
6553 cond_resched();
6554 bytenr = btrfs_node_blockptr(eb, slot);
6555 generation = btrfs_node_ptr_generation(eb, slot);
6556
6557 if (slot == path->slots[wc->level])
6558 goto reada;
6559
6560 if (wc->stage == UPDATE_BACKREF &&
6561 generation <= root->root_key.offset)
6562 continue;
6563
Yan, Zheng94fcca92009-10-09 09:25:16 -04006564 /* We don't lock the tree block, it's OK to be racy here */
6565 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6566 &refs, &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006567 /* We don't care about errors in readahead. */
6568 if (ret < 0)
6569 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006570 BUG_ON(refs == 0);
6571
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006572 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006573 if (refs == 1)
6574 goto reada;
6575
Yan, Zheng94fcca92009-10-09 09:25:16 -04006576 if (wc->level == 1 &&
6577 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6578 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006579 if (!wc->update_ref ||
6580 generation <= root->root_key.offset)
6581 continue;
6582 btrfs_node_key_to_cpu(eb, &key, slot);
6583 ret = btrfs_comp_cpu_keys(&key,
6584 &wc->update_progress);
6585 if (ret < 0)
6586 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006587 } else {
6588 if (wc->level == 1 &&
6589 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6590 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006591 }
6592reada:
6593 ret = readahead_tree_block(root, bytenr, blocksize,
6594 generation);
6595 if (ret)
6596 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006597 nread++;
6598 }
6599 wc->reada_slot = slot;
6600}
6601
Chris Mason9aca1d52007-03-13 11:09:37 -04006602/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006603 * hepler to process tree block while walking down the tree.
6604 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006605 * when wc->stage == UPDATE_BACKREF, this function updates
6606 * back refs for pointers in the block.
6607 *
6608 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006609 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006610static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6611 struct btrfs_root *root,
6612 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006613 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006614{
6615 int level = wc->level;
6616 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006617 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6618 int ret;
6619
6620 if (wc->stage == UPDATE_BACKREF &&
6621 btrfs_header_owner(eb) != root->root_key.objectid)
6622 return 1;
6623
6624 /*
6625 * when reference count of tree block is 1, it won't increase
6626 * again. once full backref flag is set, we never clear it.
6627 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006628 if (lookup_info &&
6629 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6630 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006631 BUG_ON(!path->locks[level]);
6632 ret = btrfs_lookup_extent_info(trans, root,
6633 eb->start, eb->len,
6634 &wc->refs[level],
6635 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006636 BUG_ON(ret == -ENOMEM);
6637 if (ret)
6638 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006639 BUG_ON(wc->refs[level] == 0);
6640 }
6641
Yan Zheng2c47e6052009-06-27 21:07:35 -04006642 if (wc->stage == DROP_REFERENCE) {
6643 if (wc->refs[level] > 1)
6644 return 1;
6645
6646 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006647 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006648 path->locks[level] = 0;
6649 }
6650 return 0;
6651 }
6652
6653 /* wc->stage == UPDATE_BACKREF */
6654 if (!(wc->flags[level] & flag)) {
6655 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006656 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006657 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006658 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006659 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006660 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6661 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006662 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006663 wc->flags[level] |= flag;
6664 }
6665
6666 /*
6667 * the block is shared by multiple trees, so it's not good to
6668 * keep the tree lock
6669 */
6670 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006671 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006672 path->locks[level] = 0;
6673 }
6674 return 0;
6675}
6676
6677/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006678 * hepler to process tree block pointer.
6679 *
6680 * when wc->stage == DROP_REFERENCE, this function checks
6681 * reference count of the block pointed to. if the block
6682 * is shared and we need update back refs for the subtree
6683 * rooted at the block, this function changes wc->stage to
6684 * UPDATE_BACKREF. if the block is shared and there is no
6685 * need to update back, this function drops the reference
6686 * to the block.
6687 *
6688 * NOTE: return value 1 means we should stop walking down.
6689 */
6690static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6691 struct btrfs_root *root,
6692 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006693 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006694{
6695 u64 bytenr;
6696 u64 generation;
6697 u64 parent;
6698 u32 blocksize;
6699 struct btrfs_key key;
6700 struct extent_buffer *next;
6701 int level = wc->level;
6702 int reada = 0;
6703 int ret = 0;
6704
6705 generation = btrfs_node_ptr_generation(path->nodes[level],
6706 path->slots[level]);
6707 /*
6708 * if the lower level block was created before the snapshot
6709 * was created, we know there is no need to update back refs
6710 * for the subtree
6711 */
6712 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006713 generation <= root->root_key.offset) {
6714 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006715 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006716 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006717
6718 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6719 blocksize = btrfs_level_size(root, level - 1);
6720
6721 next = btrfs_find_tree_block(root, bytenr, blocksize);
6722 if (!next) {
6723 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006724 if (!next)
6725 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006726 reada = 1;
6727 }
6728 btrfs_tree_lock(next);
6729 btrfs_set_lock_blocking(next);
6730
Yan, Zheng94fcca92009-10-09 09:25:16 -04006731 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6732 &wc->refs[level - 1],
6733 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006734 if (ret < 0) {
6735 btrfs_tree_unlock(next);
6736 return ret;
6737 }
6738
Yan, Zheng94fcca92009-10-09 09:25:16 -04006739 BUG_ON(wc->refs[level - 1] == 0);
6740 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006741
Yan, Zheng94fcca92009-10-09 09:25:16 -04006742 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006743 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006744 if (level == 1 &&
6745 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6746 goto skip;
6747
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006748 if (!wc->update_ref ||
6749 generation <= root->root_key.offset)
6750 goto skip;
6751
6752 btrfs_node_key_to_cpu(path->nodes[level], &key,
6753 path->slots[level]);
6754 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6755 if (ret < 0)
6756 goto skip;
6757
6758 wc->stage = UPDATE_BACKREF;
6759 wc->shared_level = level - 1;
6760 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006761 } else {
6762 if (level == 1 &&
6763 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6764 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006765 }
6766
Chris Masonb9fab912012-05-06 07:23:47 -04006767 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006768 btrfs_tree_unlock(next);
6769 free_extent_buffer(next);
6770 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006771 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006772 }
6773
6774 if (!next) {
6775 if (reada && level == 1)
6776 reada_walk_down(trans, root, wc, path);
6777 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006778 if (!next)
6779 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006780 btrfs_tree_lock(next);
6781 btrfs_set_lock_blocking(next);
6782 }
6783
6784 level--;
6785 BUG_ON(level != btrfs_header_level(next));
6786 path->nodes[level] = next;
6787 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006788 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006789 wc->level = level;
6790 if (wc->level == 1)
6791 wc->reada_slot = 0;
6792 return 0;
6793skip:
6794 wc->refs[level - 1] = 0;
6795 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006796 if (wc->stage == DROP_REFERENCE) {
6797 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6798 parent = path->nodes[level]->start;
6799 } else {
6800 BUG_ON(root->root_key.objectid !=
6801 btrfs_header_owner(path->nodes[level]));
6802 parent = 0;
6803 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006804
Yan, Zheng94fcca92009-10-09 09:25:16 -04006805 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006806 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006807 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006808 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006809 btrfs_tree_unlock(next);
6810 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006811 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006812 return 1;
6813}
6814
6815/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006816 * hepler to process tree block while walking up the tree.
6817 *
6818 * when wc->stage == DROP_REFERENCE, this function drops
6819 * reference count on the block.
6820 *
6821 * when wc->stage == UPDATE_BACKREF, this function changes
6822 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6823 * to UPDATE_BACKREF previously while processing the block.
6824 *
6825 * NOTE: return value 1 means we should stop walking up.
6826 */
6827static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6828 struct btrfs_root *root,
6829 struct btrfs_path *path,
6830 struct walk_control *wc)
6831{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006832 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006833 int level = wc->level;
6834 struct extent_buffer *eb = path->nodes[level];
6835 u64 parent = 0;
6836
6837 if (wc->stage == UPDATE_BACKREF) {
6838 BUG_ON(wc->shared_level < level);
6839 if (level < wc->shared_level)
6840 goto out;
6841
Yan Zheng2c47e6052009-06-27 21:07:35 -04006842 ret = find_next_key(path, level + 1, &wc->update_progress);
6843 if (ret > 0)
6844 wc->update_ref = 0;
6845
6846 wc->stage = DROP_REFERENCE;
6847 wc->shared_level = -1;
6848 path->slots[level] = 0;
6849
6850 /*
6851 * check reference count again if the block isn't locked.
6852 * we should start walking down the tree again if reference
6853 * count is one.
6854 */
6855 if (!path->locks[level]) {
6856 BUG_ON(level == 0);
6857 btrfs_tree_lock(eb);
6858 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006859 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006860
6861 ret = btrfs_lookup_extent_info(trans, root,
6862 eb->start, eb->len,
6863 &wc->refs[level],
6864 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006865 if (ret < 0) {
6866 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006867 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006868 return ret;
6869 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006870 BUG_ON(wc->refs[level] == 0);
6871 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006872 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006873 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006874 return 1;
6875 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006876 }
6877 }
6878
6879 /* wc->stage == DROP_REFERENCE */
6880 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6881
6882 if (wc->refs[level] == 1) {
6883 if (level == 0) {
6884 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006885 ret = btrfs_dec_ref(trans, root, eb, 1,
6886 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006887 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006888 ret = btrfs_dec_ref(trans, root, eb, 0,
6889 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006890 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006891 }
6892 /* make block locked assertion in clean_tree_block happy */
6893 if (!path->locks[level] &&
6894 btrfs_header_generation(eb) == trans->transid) {
6895 btrfs_tree_lock(eb);
6896 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006897 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006898 }
6899 clean_tree_block(trans, root, eb);
6900 }
6901
6902 if (eb == root->node) {
6903 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6904 parent = eb->start;
6905 else
6906 BUG_ON(root->root_key.objectid !=
6907 btrfs_header_owner(eb));
6908 } else {
6909 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6910 parent = path->nodes[level + 1]->start;
6911 else
6912 BUG_ON(root->root_key.objectid !=
6913 btrfs_header_owner(path->nodes[level + 1]));
6914 }
6915
Jan Schmidt5581a512012-05-16 17:04:52 +02006916 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006917out:
6918 wc->refs[level] = 0;
6919 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006920 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006921}
6922
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006923static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6924 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006925 struct btrfs_path *path,
6926 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006927{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006928 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006929 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006930 int ret;
6931
Yan Zheng2c47e6052009-06-27 21:07:35 -04006932 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006933 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006934 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006935 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006936
Yan Zheng2c47e6052009-06-27 21:07:35 -04006937 if (level == 0)
6938 break;
6939
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006940 if (path->slots[level] >=
6941 btrfs_header_nritems(path->nodes[level]))
6942 break;
6943
Yan, Zheng94fcca92009-10-09 09:25:16 -04006944 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006945 if (ret > 0) {
6946 path->slots[level]++;
6947 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006948 } else if (ret < 0)
6949 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006950 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006951 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006952 return 0;
6953}
6954
Chris Masond3977122009-01-05 21:25:51 -05006955static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006956 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006957 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006958 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006959{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006960 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006961 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006962
Yan Zheng2c47e6052009-06-27 21:07:35 -04006963 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6964 while (level < max_level && path->nodes[level]) {
6965 wc->level = level;
6966 if (path->slots[level] + 1 <
6967 btrfs_header_nritems(path->nodes[level])) {
6968 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006969 return 0;
6970 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006971 ret = walk_up_proc(trans, root, path, wc);
6972 if (ret > 0)
6973 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006974
Yan Zheng2c47e6052009-06-27 21:07:35 -04006975 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006976 btrfs_tree_unlock_rw(path->nodes[level],
6977 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006978 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006979 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006980 free_extent_buffer(path->nodes[level]);
6981 path->nodes[level] = NULL;
6982 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006983 }
6984 }
6985 return 1;
6986}
6987
Chris Mason9aca1d52007-03-13 11:09:37 -04006988/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006989 * drop a subvolume tree.
6990 *
6991 * this function traverses the tree freeing any blocks that only
6992 * referenced by the tree.
6993 *
6994 * when a shared tree block is found. this function decreases its
6995 * reference count by one. if update_ref is true, this function
6996 * also make sure backrefs for the shared block and all lower level
6997 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006998 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04006999int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007000 struct btrfs_block_rsv *block_rsv, int update_ref,
7001 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007002{
Chris Mason5caf2a02007-04-02 11:20:42 -04007003 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007004 struct btrfs_trans_handle *trans;
7005 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007006 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007007 struct walk_control *wc;
7008 struct btrfs_key key;
7009 int err = 0;
7010 int ret;
7011 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007012
Chris Mason5caf2a02007-04-02 11:20:42 -04007013 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007014 if (!path) {
7015 err = -ENOMEM;
7016 goto out;
7017 }
Chris Mason20524f02007-03-10 06:35:47 -05007018
Yan Zheng2c47e6052009-06-27 21:07:35 -04007019 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007020 if (!wc) {
7021 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007022 err = -ENOMEM;
7023 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007024 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007025
Yan, Zhenga22285a2010-05-16 10:48:46 -04007026 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007027 if (IS_ERR(trans)) {
7028 err = PTR_ERR(trans);
7029 goto out_free;
7030 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007031
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007032 if (block_rsv)
7033 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007034
Chris Mason9f3a7422007-08-07 15:52:19 -04007035 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007036 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007037 path->nodes[level] = btrfs_lock_root_node(root);
7038 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007039 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007040 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007041 memset(&wc->update_progress, 0,
7042 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007043 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007044 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007045 memcpy(&wc->update_progress, &key,
7046 sizeof(wc->update_progress));
7047
Chris Mason6702ed42007-08-07 16:15:09 -04007048 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007049 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007050 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007051 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7052 path->lowest_level = 0;
7053 if (ret < 0) {
7054 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007055 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007056 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007057 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007058
Chris Mason7d9eb122008-07-08 14:19:17 -04007059 /*
7060 * unlock our path, this is safe because only this
7061 * function is allowed to delete this snapshot
7062 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007063 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007064
Yan Zheng2c47e6052009-06-27 21:07:35 -04007065 level = btrfs_header_level(root->node);
7066 while (1) {
7067 btrfs_tree_lock(path->nodes[level]);
7068 btrfs_set_lock_blocking(path->nodes[level]);
7069
7070 ret = btrfs_lookup_extent_info(trans, root,
7071 path->nodes[level]->start,
7072 path->nodes[level]->len,
7073 &wc->refs[level],
7074 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007075 if (ret < 0) {
7076 err = ret;
7077 goto out_end_trans;
7078 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007079 BUG_ON(wc->refs[level] == 0);
7080
7081 if (level == root_item->drop_level)
7082 break;
7083
7084 btrfs_tree_unlock(path->nodes[level]);
7085 WARN_ON(wc->refs[level] != 1);
7086 level--;
7087 }
7088 }
7089
7090 wc->level = level;
7091 wc->shared_level = -1;
7092 wc->stage = DROP_REFERENCE;
7093 wc->update_ref = update_ref;
7094 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007095 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007096 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007097
7098 while (1) {
7099 ret = walk_down_tree(trans, root, path, wc);
7100 if (ret < 0) {
7101 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007102 break;
7103 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007104
7105 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7106 if (ret < 0) {
7107 err = ret;
7108 break;
7109 }
7110
7111 if (ret > 0) {
7112 BUG_ON(wc->stage != DROP_REFERENCE);
7113 break;
7114 }
7115
7116 if (wc->stage == DROP_REFERENCE) {
7117 level = wc->level;
7118 btrfs_node_key(path->nodes[level],
7119 &root_item->drop_progress,
7120 path->slots[level]);
7121 root_item->drop_level = level;
7122 }
7123
7124 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007125 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007126 ret = btrfs_update_root(trans, tree_root,
7127 &root->root_key,
7128 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007129 if (ret) {
7130 btrfs_abort_transaction(trans, tree_root, ret);
7131 err = ret;
7132 goto out_end_trans;
7133 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007134
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007135 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007136 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007137 if (IS_ERR(trans)) {
7138 err = PTR_ERR(trans);
7139 goto out_free;
7140 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007141 if (block_rsv)
7142 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007143 }
Chris Mason20524f02007-03-10 06:35:47 -05007144 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007145 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007146 if (err)
7147 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007148
7149 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007150 if (ret) {
7151 btrfs_abort_transaction(trans, tree_root, ret);
7152 goto out_end_trans;
7153 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007154
Yan, Zheng76dda932009-09-21 16:00:26 -04007155 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7156 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7157 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007158 if (ret < 0) {
7159 btrfs_abort_transaction(trans, tree_root, ret);
7160 err = ret;
7161 goto out_end_trans;
7162 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007163 /* if we fail to delete the orphan item this time
7164 * around, it'll get picked up the next time.
7165 *
7166 * The most common failure here is just -ENOENT.
7167 */
7168 btrfs_del_orphan_item(trans, tree_root,
7169 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007170 }
7171 }
7172
7173 if (root->in_radix) {
7174 btrfs_free_fs_root(tree_root->fs_info, root);
7175 } else {
7176 free_extent_buffer(root->node);
7177 free_extent_buffer(root->commit_root);
7178 kfree(root);
7179 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007180out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007181 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007182out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007183 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007184 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007185out:
7186 if (err)
7187 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007188 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007189}
Chris Mason9078a3e2007-04-26 16:46:15 -04007190
Yan Zheng2c47e6052009-06-27 21:07:35 -04007191/*
7192 * drop subtree rooted at tree block 'node'.
7193 *
7194 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007195 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007196 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007197int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7198 struct btrfs_root *root,
7199 struct extent_buffer *node,
7200 struct extent_buffer *parent)
7201{
7202 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007203 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007204 int level;
7205 int parent_level;
7206 int ret = 0;
7207 int wret;
7208
Yan Zheng2c47e6052009-06-27 21:07:35 -04007209 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7210
Yan Zhengf82d02d2008-10-29 14:49:05 -04007211 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007212 if (!path)
7213 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007214
Yan Zheng2c47e6052009-06-27 21:07:35 -04007215 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007216 if (!wc) {
7217 btrfs_free_path(path);
7218 return -ENOMEM;
7219 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007220
Chris Masonb9447ef2009-03-09 11:45:38 -04007221 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007222 parent_level = btrfs_header_level(parent);
7223 extent_buffer_get(parent);
7224 path->nodes[parent_level] = parent;
7225 path->slots[parent_level] = btrfs_header_nritems(parent);
7226
Chris Masonb9447ef2009-03-09 11:45:38 -04007227 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007228 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007229 path->nodes[level] = node;
7230 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007231 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007232
7233 wc->refs[parent_level] = 1;
7234 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7235 wc->level = level;
7236 wc->shared_level = -1;
7237 wc->stage = DROP_REFERENCE;
7238 wc->update_ref = 0;
7239 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007240 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007241 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007242
7243 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007244 wret = walk_down_tree(trans, root, path, wc);
7245 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007246 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007247 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007248 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007249
Yan Zheng2c47e6052009-06-27 21:07:35 -04007250 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007251 if (wret < 0)
7252 ret = wret;
7253 if (wret != 0)
7254 break;
7255 }
7256
Yan Zheng2c47e6052009-06-27 21:07:35 -04007257 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007258 btrfs_free_path(path);
7259 return ret;
7260}
7261
Chris Masonec44a352008-04-28 15:29:52 -04007262static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7263{
7264 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007265 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007266
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007267 /*
7268 * if restripe for this chunk_type is on pick target profile and
7269 * return, otherwise do the usual balance
7270 */
7271 stripped = get_restripe_target(root->fs_info, flags);
7272 if (stripped)
7273 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007274
Chris Masoncd02dca2010-12-13 14:56:23 -05007275 /*
7276 * we add in the count of missing devices because we want
7277 * to make sure that any RAID levels on a degraded FS
7278 * continue to be honored.
7279 */
7280 num_devices = root->fs_info->fs_devices->rw_devices +
7281 root->fs_info->fs_devices->missing_devices;
7282
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007283 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7284 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7285
Chris Masonec44a352008-04-28 15:29:52 -04007286 if (num_devices == 1) {
7287 stripped |= BTRFS_BLOCK_GROUP_DUP;
7288 stripped = flags & ~stripped;
7289
7290 /* turn raid0 into single device chunks */
7291 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7292 return stripped;
7293
7294 /* turn mirroring into duplication */
7295 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7296 BTRFS_BLOCK_GROUP_RAID10))
7297 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007298 } else {
7299 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007300 if (flags & stripped)
7301 return flags;
7302
7303 stripped |= BTRFS_BLOCK_GROUP_DUP;
7304 stripped = flags & ~stripped;
7305
7306 /* switch duplicated blocks with raid1 */
7307 if (flags & BTRFS_BLOCK_GROUP_DUP)
7308 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7309
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007310 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007311 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007312
Chris Masonec44a352008-04-28 15:29:52 -04007313 return flags;
7314}
7315
Miao Xie199c36e2011-07-15 10:34:36 +00007316static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007317{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007318 struct btrfs_space_info *sinfo = cache->space_info;
7319 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007320 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007321 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007322
Chris Masonc286ac42008-07-22 23:06:41 -04007323
Miao Xie199c36e2011-07-15 10:34:36 +00007324 /*
7325 * We need some metadata space and system metadata space for
7326 * allocating chunks in some corner cases until we force to set
7327 * it to be readonly.
7328 */
7329 if ((sinfo->flags &
7330 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7331 !force)
7332 min_allocable_bytes = 1 * 1024 * 1024;
7333 else
7334 min_allocable_bytes = 0;
7335
Yan, Zhengf0486c62010-05-16 10:46:25 -04007336 spin_lock(&sinfo->lock);
7337 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007338
7339 if (cache->ro) {
7340 ret = 0;
7341 goto out;
7342 }
7343
Yan, Zhengf0486c62010-05-16 10:46:25 -04007344 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7345 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007346
Yan, Zhengf0486c62010-05-16 10:46:25 -04007347 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007348 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7349 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007350 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007351 cache->ro = 1;
7352 ret = 0;
7353 }
WuBo61cfea92011-07-26 03:30:11 +00007354out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007355 spin_unlock(&cache->lock);
7356 spin_unlock(&sinfo->lock);
7357 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007358}
7359
Yan, Zhengf0486c62010-05-16 10:46:25 -04007360int btrfs_set_block_group_ro(struct btrfs_root *root,
7361 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007362
7363{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007364 struct btrfs_trans_handle *trans;
7365 u64 alloc_flags;
7366 int ret;
7367
7368 BUG_ON(cache->ro);
7369
Josef Bacik7a7eaa402011-04-13 12:54:33 -04007370 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007371 if (IS_ERR(trans))
7372 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007373
7374 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007375 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007376 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007377 CHUNK_ALLOC_FORCE);
7378 if (ret < 0)
7379 goto out;
7380 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007381
Miao Xie199c36e2011-07-15 10:34:36 +00007382 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007383 if (!ret)
7384 goto out;
7385 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007386 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007387 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007388 if (ret < 0)
7389 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007390 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007391out:
7392 btrfs_end_transaction(trans, root);
7393 return ret;
7394}
7395
Chris Masonc87f08c2011-02-16 13:57:04 -05007396int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7397 struct btrfs_root *root, u64 type)
7398{
7399 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007400 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007401 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007402}
7403
Miao Xie6d07bce2011-01-05 10:07:31 +00007404/*
7405 * helper to account the unused space of all the readonly block group in the
7406 * list. takes mirrors into account.
7407 */
7408static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7409{
7410 struct btrfs_block_group_cache *block_group;
7411 u64 free_bytes = 0;
7412 int factor;
7413
7414 list_for_each_entry(block_group, groups_list, list) {
7415 spin_lock(&block_group->lock);
7416
7417 if (!block_group->ro) {
7418 spin_unlock(&block_group->lock);
7419 continue;
7420 }
7421
7422 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7423 BTRFS_BLOCK_GROUP_RAID10 |
7424 BTRFS_BLOCK_GROUP_DUP))
7425 factor = 2;
7426 else
7427 factor = 1;
7428
7429 free_bytes += (block_group->key.offset -
7430 btrfs_block_group_used(&block_group->item)) *
7431 factor;
7432
7433 spin_unlock(&block_group->lock);
7434 }
7435
7436 return free_bytes;
7437}
7438
7439/*
7440 * helper to account the unused space of all the readonly block group in the
7441 * space_info. takes mirrors into account.
7442 */
7443u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7444{
7445 int i;
7446 u64 free_bytes = 0;
7447
7448 spin_lock(&sinfo->lock);
7449
7450 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7451 if (!list_empty(&sinfo->block_groups[i]))
7452 free_bytes += __btrfs_get_ro_block_group_free_space(
7453 &sinfo->block_groups[i]);
7454
7455 spin_unlock(&sinfo->lock);
7456
7457 return free_bytes;
7458}
7459
Jeff Mahoney143bede2012-03-01 14:56:26 +01007460void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007461 struct btrfs_block_group_cache *cache)
7462{
7463 struct btrfs_space_info *sinfo = cache->space_info;
7464 u64 num_bytes;
7465
7466 BUG_ON(!cache->ro);
7467
7468 spin_lock(&sinfo->lock);
7469 spin_lock(&cache->lock);
7470 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7471 cache->bytes_super - btrfs_block_group_used(&cache->item);
7472 sinfo->bytes_readonly -= num_bytes;
7473 cache->ro = 0;
7474 spin_unlock(&cache->lock);
7475 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007476}
7477
Josef Bacikba1bf482009-09-11 16:11:19 -04007478/*
7479 * checks to see if its even possible to relocate this block group.
7480 *
7481 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7482 * ok to go ahead and try.
7483 */
7484int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007485{
Zheng Yan1a40e232008-09-26 10:09:34 -04007486 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007487 struct btrfs_space_info *space_info;
7488 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7489 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007490 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007491 u64 dev_min = 1;
7492 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007493 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007494 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007495 int full = 0;
7496 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007497
Josef Bacikba1bf482009-09-11 16:11:19 -04007498 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007499
Josef Bacikba1bf482009-09-11 16:11:19 -04007500 /* odd, couldn't find the block group, leave it alone */
7501 if (!block_group)
7502 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007503
liubocdcb7252011-08-03 10:15:25 +00007504 min_free = btrfs_block_group_used(&block_group->item);
7505
Josef Bacikba1bf482009-09-11 16:11:19 -04007506 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007507 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007508 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007509
Josef Bacikba1bf482009-09-11 16:11:19 -04007510 space_info = block_group->space_info;
7511 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007512
Josef Bacikba1bf482009-09-11 16:11:19 -04007513 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007514
Josef Bacikba1bf482009-09-11 16:11:19 -04007515 /*
7516 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007517 * relocate it unless we're able to allocate a new chunk below.
7518 *
7519 * Otherwise, we need to make sure we have room in the space to handle
7520 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007521 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007522 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007523 (space_info->bytes_used + space_info->bytes_reserved +
7524 space_info->bytes_pinned + space_info->bytes_readonly +
7525 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007526 spin_unlock(&space_info->lock);
7527 goto out;
7528 }
7529 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007530
Josef Bacikba1bf482009-09-11 16:11:19 -04007531 /*
7532 * ok we don't have enough space, but maybe we have free space on our
7533 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007534 * alloc devices and guess if we have enough space. if this block
7535 * group is going to be restriped, run checks against the target
7536 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007537 */
7538 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007539
liubocdcb7252011-08-03 10:15:25 +00007540 /*
7541 * index:
7542 * 0: raid10
7543 * 1: raid1
7544 * 2: dup
7545 * 3: raid0
7546 * 4: single
7547 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007548 target = get_restripe_target(root->fs_info, block_group->flags);
7549 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007550 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007551 } else {
7552 /*
7553 * this is just a balance, so if we were marked as full
7554 * we know there is no space for a new chunk
7555 */
7556 if (full)
7557 goto out;
7558
7559 index = get_block_group_index(block_group);
7560 }
7561
Miao Xiee6ec7162013-01-17 05:38:51 +00007562 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00007563 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007564 /* Divide by 2 */
7565 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007566 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00007567 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00007568 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04007569 /* Multiply by 2 */
7570 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007571 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00007572 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007573 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007574 }
7575
Josef Bacikba1bf482009-09-11 16:11:19 -04007576 mutex_lock(&root->fs_info->chunk_mutex);
7577 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007578 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007579
Josef Bacikba1bf482009-09-11 16:11:19 -04007580 /*
7581 * check to make sure we can actually find a chunk with enough
7582 * space to fit our block group in.
7583 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007584 if (device->total_bytes > device->bytes_used + min_free &&
7585 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007586 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007587 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007588 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007589 dev_nr++;
7590
7591 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007592 break;
liubocdcb7252011-08-03 10:15:25 +00007593
Josef Bacikba1bf482009-09-11 16:11:19 -04007594 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007595 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007596 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007597 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007598out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007599 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007600 return ret;
7601}
7602
Christoph Hellwigb2950862008-12-02 09:54:17 -05007603static int find_first_block_group(struct btrfs_root *root,
7604 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007605{
Chris Mason925baed2008-06-25 16:01:30 -04007606 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007607 struct btrfs_key found_key;
7608 struct extent_buffer *leaf;
7609 int slot;
7610
7611 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7612 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007613 goto out;
7614
Chris Masond3977122009-01-05 21:25:51 -05007615 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007616 slot = path->slots[0];
7617 leaf = path->nodes[0];
7618 if (slot >= btrfs_header_nritems(leaf)) {
7619 ret = btrfs_next_leaf(root, path);
7620 if (ret == 0)
7621 continue;
7622 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007623 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007624 break;
7625 }
7626 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7627
7628 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007629 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7630 ret = 0;
7631 goto out;
7632 }
Chris Mason0b86a832008-03-24 15:01:56 -04007633 path->slots[0]++;
7634 }
Chris Mason925baed2008-06-25 16:01:30 -04007635out:
Chris Mason0b86a832008-03-24 15:01:56 -04007636 return ret;
7637}
7638
Josef Bacik0af3d002010-06-21 14:48:16 -04007639void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7640{
7641 struct btrfs_block_group_cache *block_group;
7642 u64 last = 0;
7643
7644 while (1) {
7645 struct inode *inode;
7646
7647 block_group = btrfs_lookup_first_block_group(info, last);
7648 while (block_group) {
7649 spin_lock(&block_group->lock);
7650 if (block_group->iref)
7651 break;
7652 spin_unlock(&block_group->lock);
7653 block_group = next_block_group(info->tree_root,
7654 block_group);
7655 }
7656 if (!block_group) {
7657 if (last == 0)
7658 break;
7659 last = 0;
7660 continue;
7661 }
7662
7663 inode = block_group->inode;
7664 block_group->iref = 0;
7665 block_group->inode = NULL;
7666 spin_unlock(&block_group->lock);
7667 iput(inode);
7668 last = block_group->key.objectid + block_group->key.offset;
7669 btrfs_put_block_group(block_group);
7670 }
7671}
7672
Zheng Yan1a40e232008-09-26 10:09:34 -04007673int btrfs_free_block_groups(struct btrfs_fs_info *info)
7674{
7675 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007676 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007677 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007678 struct rb_node *n;
7679
Yan Zheng11833d62009-09-11 16:11:19 -04007680 down_write(&info->extent_commit_sem);
7681 while (!list_empty(&info->caching_block_groups)) {
7682 caching_ctl = list_entry(info->caching_block_groups.next,
7683 struct btrfs_caching_control, list);
7684 list_del(&caching_ctl->list);
7685 put_caching_control(caching_ctl);
7686 }
7687 up_write(&info->extent_commit_sem);
7688
Zheng Yan1a40e232008-09-26 10:09:34 -04007689 spin_lock(&info->block_group_cache_lock);
7690 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7691 block_group = rb_entry(n, struct btrfs_block_group_cache,
7692 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007693 rb_erase(&block_group->cache_node,
7694 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007695 spin_unlock(&info->block_group_cache_lock);
7696
Josef Bacik80eb2342008-10-29 14:49:05 -04007697 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007698 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007699 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007700
Josef Bacik817d52f2009-07-13 21:29:25 -04007701 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007702 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007703
Josef Bacik3c148742011-02-02 15:53:47 +00007704 /*
7705 * We haven't cached this block group, which means we could
7706 * possibly have excluded extents on this block group.
7707 */
7708 if (block_group->cached == BTRFS_CACHE_NO)
7709 free_excluded_extents(info->extent_root, block_group);
7710
Josef Bacik817d52f2009-07-13 21:29:25 -04007711 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007712 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007713
7714 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007715 }
7716 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007717
7718 /* now that all the block groups are freed, go through and
7719 * free all the space_info structs. This is only called during
7720 * the final stages of unmount, and so we know nobody is
7721 * using them. We call synchronize_rcu() once before we start,
7722 * just to be on the safe side.
7723 */
7724 synchronize_rcu();
7725
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007726 release_global_block_rsv(info);
7727
Chris Mason4184ea72009-03-10 12:39:20 -04007728 while(!list_empty(&info->space_info)) {
7729 space_info = list_entry(info->space_info.next,
7730 struct btrfs_space_info,
7731 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007732 if (space_info->bytes_pinned > 0 ||
Josef Bacikfb25e912011-07-26 17:00:46 -04007733 space_info->bytes_reserved > 0 ||
7734 space_info->bytes_may_use > 0) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007735 WARN_ON(1);
7736 dump_space_info(space_info, 0, 0);
7737 }
Chris Mason4184ea72009-03-10 12:39:20 -04007738 list_del(&space_info->list);
7739 kfree(space_info);
7740 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007741 return 0;
7742}
7743
Yan, Zhengb742bb82010-05-16 10:46:24 -04007744static void __link_block_group(struct btrfs_space_info *space_info,
7745 struct btrfs_block_group_cache *cache)
7746{
7747 int index = get_block_group_index(cache);
7748
7749 down_write(&space_info->groups_sem);
7750 list_add_tail(&cache->list, &space_info->block_groups[index]);
7751 up_write(&space_info->groups_sem);
7752}
7753
Chris Mason9078a3e2007-04-26 16:46:15 -04007754int btrfs_read_block_groups(struct btrfs_root *root)
7755{
7756 struct btrfs_path *path;
7757 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007758 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007759 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007760 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007761 struct btrfs_key key;
7762 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007763 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007764 int need_clear = 0;
7765 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007766
Chris Masonbe744172007-05-06 10:15:01 -04007767 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007768 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007769 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007770 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007771 path = btrfs_alloc_path();
7772 if (!path)
7773 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007774 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007775
David Sterba6c417612011-04-13 15:41:04 +02007776 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007777 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02007778 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04007779 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007780 if (btrfs_test_opt(root, CLEAR_CACHE))
7781 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007782
Chris Masond3977122009-01-05 21:25:51 -05007783 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007784 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007785 if (ret > 0)
7786 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007787 if (ret != 0)
7788 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007789 leaf = path->nodes[0];
7790 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007791 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007792 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007793 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007794 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007795 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007796 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7797 GFP_NOFS);
7798 if (!cache->free_space_ctl) {
7799 kfree(cache);
7800 ret = -ENOMEM;
7801 goto error;
7802 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007803
Yan Zhengd2fb3432008-12-11 16:30:39 -05007804 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007805 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007806 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007807 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007808 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007809
Liu Bocf7c1ef2012-07-06 03:31:34 -06007810 if (need_clear) {
7811 /*
7812 * When we mount with old space cache, we need to
7813 * set BTRFS_DC_CLEAR and set dirty flag.
7814 *
7815 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7816 * truncate the old free space cache inode and
7817 * setup a new one.
7818 * b) Setting 'dirty flag' makes sure that we flush
7819 * the new space cache info onto disk.
7820 */
Josef Bacik0af3d002010-06-21 14:48:16 -04007821 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007822 if (btrfs_test_opt(root, SPACE_CACHE))
7823 cache->dirty = 1;
7824 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007825
Chris Mason5f39d392007-10-15 16:14:19 -04007826 read_extent_buffer(leaf, &cache->item,
7827 btrfs_item_ptr_offset(leaf, path->slots[0]),
7828 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007829 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007830
Chris Mason9078a3e2007-04-26 16:46:15 -04007831 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007832 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007833 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007834 cache->sectorsize = root->sectorsize;
7835
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007836 btrfs_init_free_space_ctl(cache);
7837
Josef Bacik817d52f2009-07-13 21:29:25 -04007838 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007839 * We need to exclude the super stripes now so that the space
7840 * info has super bytes accounted for, otherwise we'll think
7841 * we have more space than we actually do.
7842 */
7843 exclude_super_stripes(root, cache);
7844
7845 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007846 * check for two cases, either we are full, and therefore
7847 * don't need to bother with the caching work since we won't
7848 * find any space, or we are empty, and we can just add all
7849 * the space in and be done with it. This saves us _alot_ of
7850 * time, particularly in the full case.
7851 */
7852 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007853 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007854 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007855 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007856 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007857 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007858 cache->cached = BTRFS_CACHE_FINISHED;
7859 add_new_free_space(cache, root->fs_info,
7860 found_key.objectid,
7861 found_key.objectid +
7862 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007863 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007864 }
Chris Mason96b51792007-10-15 16:15:19 -04007865
Chris Mason6324fbf2008-03-24 15:01:59 -04007866 ret = update_space_info(info, cache->flags, found_key.offset,
7867 btrfs_block_group_used(&cache->item),
7868 &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007869 BUG_ON(ret); /* -ENOMEM */
Chris Mason6324fbf2008-03-24 15:01:59 -04007870 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007871 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007872 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007873 spin_unlock(&cache->space_info->lock);
7874
Yan, Zhengb742bb82010-05-16 10:46:24 -04007875 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007876
Josef Bacik0f9dd462008-09-23 13:14:11 -04007877 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007878 BUG_ON(ret); /* Logic error */
Chris Mason75ccf472008-09-30 19:24:06 -04007879
7880 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007881 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007882 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007883 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007884
7885 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7886 if (!(get_alloc_profile(root, space_info->flags) &
7887 (BTRFS_BLOCK_GROUP_RAID10 |
7888 BTRFS_BLOCK_GROUP_RAID1 |
7889 BTRFS_BLOCK_GROUP_DUP)))
7890 continue;
7891 /*
7892 * avoid allocating from un-mirrored block group if there are
7893 * mirrored block groups.
7894 */
7895 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007896 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007897 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007898 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007899 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007900
7901 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007902 ret = 0;
7903error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007904 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007905 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007906}
Chris Mason6324fbf2008-03-24 15:01:59 -04007907
Josef Bacikea658ba2012-09-11 16:57:25 -04007908void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
7909 struct btrfs_root *root)
7910{
7911 struct btrfs_block_group_cache *block_group, *tmp;
7912 struct btrfs_root *extent_root = root->fs_info->extent_root;
7913 struct btrfs_block_group_item item;
7914 struct btrfs_key key;
7915 int ret = 0;
7916
7917 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
7918 new_bg_list) {
7919 list_del_init(&block_group->new_bg_list);
7920
7921 if (ret)
7922 continue;
7923
7924 spin_lock(&block_group->lock);
7925 memcpy(&item, &block_group->item, sizeof(item));
7926 memcpy(&key, &block_group->key, sizeof(key));
7927 spin_unlock(&block_group->lock);
7928
7929 ret = btrfs_insert_item(trans, extent_root, &key, &item,
7930 sizeof(item));
7931 if (ret)
7932 btrfs_abort_transaction(trans, extent_root, ret);
7933 }
7934}
7935
Chris Mason6324fbf2008-03-24 15:01:59 -04007936int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7937 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007938 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007939 u64 size)
7940{
7941 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007942 struct btrfs_root *extent_root;
7943 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007944
7945 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007946
Chris Mason12fcfd22009-03-24 10:24:20 -04007947 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007948
Chris Mason8f18cf12008-04-25 16:53:30 -04007949 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007950 if (!cache)
7951 return -ENOMEM;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007952 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7953 GFP_NOFS);
7954 if (!cache->free_space_ctl) {
7955 kfree(cache);
7956 return -ENOMEM;
7957 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007958
Chris Masone17cade2008-04-15 15:41:47 -04007959 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007960 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007961 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007962 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007963 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007964
Yan Zhengd2fb3432008-12-11 16:30:39 -05007965 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007966 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007967 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007968 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04007969 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007970
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007971 btrfs_init_free_space_ctl(cache);
7972
Chris Mason6324fbf2008-03-24 15:01:59 -04007973 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007974 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7975 cache->flags = type;
7976 btrfs_set_block_group_flags(&cache->item, type);
7977
Yan Zheng11833d62009-09-11 16:11:19 -04007978 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007979 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007980 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007981
Josef Bacik817d52f2009-07-13 21:29:25 -04007982 add_new_free_space(cache, root->fs_info, chunk_offset,
7983 chunk_offset + size);
7984
Yan Zheng11833d62009-09-11 16:11:19 -04007985 free_excluded_extents(root, cache);
7986
Chris Mason6324fbf2008-03-24 15:01:59 -04007987 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7988 &cache->space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007989 BUG_ON(ret); /* -ENOMEM */
Li Zefanc7c144d2011-12-07 10:39:22 +08007990 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04007991
7992 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007993 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007994 spin_unlock(&cache->space_info->lock);
7995
Yan, Zhengb742bb82010-05-16 10:46:24 -04007996 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007997
Josef Bacik0f9dd462008-09-23 13:14:11 -04007998 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007999 BUG_ON(ret); /* Logic error */
Chris Masonc286ac42008-07-22 23:06:41 -04008000
Josef Bacikea658ba2012-09-11 16:57:25 -04008001 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008002
Chris Masond18a2c42008-04-04 15:40:00 -04008003 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008004
Chris Mason6324fbf2008-03-24 15:01:59 -04008005 return 0;
8006}
Zheng Yan1a40e232008-09-26 10:09:34 -04008007
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008008static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8009{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008010 u64 extra_flags = chunk_to_extended(flags) &
8011 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008012
Miao Xiede98ced2013-01-29 10:13:12 +00008013 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008014 if (flags & BTRFS_BLOCK_GROUP_DATA)
8015 fs_info->avail_data_alloc_bits &= ~extra_flags;
8016 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8017 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8018 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8019 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008020 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008021}
8022
Zheng Yan1a40e232008-09-26 10:09:34 -04008023int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8024 struct btrfs_root *root, u64 group_start)
8025{
8026 struct btrfs_path *path;
8027 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008028 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008029 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008030 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008031 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008032 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008033 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008034 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008035
Zheng Yan1a40e232008-09-26 10:09:34 -04008036 root = root->fs_info->extent_root;
8037
8038 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8039 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008040 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008041
liubo9f7c43c2011-03-07 02:13:33 +00008042 /*
8043 * Free the reserved super bytes from this block group before
8044 * remove it.
8045 */
8046 free_excluded_extents(root, block_group);
8047
Zheng Yan1a40e232008-09-26 10:09:34 -04008048 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008049 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008050 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8051 BTRFS_BLOCK_GROUP_RAID1 |
8052 BTRFS_BLOCK_GROUP_RAID10))
8053 factor = 2;
8054 else
8055 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008056
Chris Mason44fb5512009-06-04 15:34:51 -04008057 /* make sure this block group isn't part of an allocation cluster */
8058 cluster = &root->fs_info->data_alloc_cluster;
8059 spin_lock(&cluster->refill_lock);
8060 btrfs_return_cluster_to_free_space(block_group, cluster);
8061 spin_unlock(&cluster->refill_lock);
8062
8063 /*
8064 * make sure this block group isn't part of a metadata
8065 * allocation cluster
8066 */
8067 cluster = &root->fs_info->meta_alloc_cluster;
8068 spin_lock(&cluster->refill_lock);
8069 btrfs_return_cluster_to_free_space(block_group, cluster);
8070 spin_unlock(&cluster->refill_lock);
8071
Zheng Yan1a40e232008-09-26 10:09:34 -04008072 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008073 if (!path) {
8074 ret = -ENOMEM;
8075 goto out;
8076 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008077
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008078 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008079 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008080 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008081 if (ret) {
8082 btrfs_add_delayed_iput(inode);
8083 goto out;
8084 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008085 clear_nlink(inode);
8086 /* One for the block groups ref */
8087 spin_lock(&block_group->lock);
8088 if (block_group->iref) {
8089 block_group->iref = 0;
8090 block_group->inode = NULL;
8091 spin_unlock(&block_group->lock);
8092 iput(inode);
8093 } else {
8094 spin_unlock(&block_group->lock);
8095 }
8096 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008097 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008098 }
8099
8100 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8101 key.offset = block_group->key.objectid;
8102 key.type = 0;
8103
8104 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8105 if (ret < 0)
8106 goto out;
8107 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008108 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008109 if (ret == 0) {
8110 ret = btrfs_del_item(trans, tree_root, path);
8111 if (ret)
8112 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008113 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008114 }
8115
Yan Zheng3dfdb932009-01-21 10:49:16 -05008116 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008117 rb_erase(&block_group->cache_node,
8118 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008119
8120 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8121 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008122 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008123
Josef Bacik80eb2342008-10-29 14:49:05 -04008124 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008125 /*
8126 * we must use list_del_init so people can check to see if they
8127 * are still on the list after taking the semaphore
8128 */
8129 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008130 if (list_empty(&block_group->space_info->block_groups[index]))
8131 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008132 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008133
Josef Bacik817d52f2009-07-13 21:29:25 -04008134 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008135 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008136
8137 btrfs_remove_free_space_cache(block_group);
8138
Yan Zhengc146afa2008-11-12 14:34:12 -05008139 spin_lock(&block_group->space_info->lock);
8140 block_group->space_info->total_bytes -= block_group->key.offset;
8141 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008142 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008143 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008144
Josef Bacik0af3d002010-06-21 14:48:16 -04008145 memcpy(&key, &block_group->key, sizeof(key));
8146
Chris Mason283bb192009-07-24 16:30:55 -04008147 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008148
Chris Masonfa9c0d72009-04-03 09:47:43 -04008149 btrfs_put_block_group(block_group);
8150 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008151
8152 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8153 if (ret > 0)
8154 ret = -EIO;
8155 if (ret < 0)
8156 goto out;
8157
8158 ret = btrfs_del_item(trans, root, path);
8159out:
8160 btrfs_free_path(path);
8161 return ret;
8162}
liuboacce9522011-01-06 19:30:25 +08008163
liuboc59021f2011-03-07 02:13:14 +00008164int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8165{
8166 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008167 struct btrfs_super_block *disk_super;
8168 u64 features;
8169 u64 flags;
8170 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008171 int ret;
8172
David Sterba6c417612011-04-13 15:41:04 +02008173 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008174 if (!btrfs_super_root(disk_super))
8175 return 1;
liuboc59021f2011-03-07 02:13:14 +00008176
liubo1aba86d2011-04-08 08:44:37 +00008177 features = btrfs_super_incompat_flags(disk_super);
8178 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8179 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008180
liubo1aba86d2011-04-08 08:44:37 +00008181 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8182 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008183 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008184 goto out;
liuboc59021f2011-03-07 02:13:14 +00008185
liubo1aba86d2011-04-08 08:44:37 +00008186 if (mixed) {
8187 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8188 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8189 } else {
8190 flags = BTRFS_BLOCK_GROUP_METADATA;
8191 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8192 if (ret)
8193 goto out;
8194
8195 flags = BTRFS_BLOCK_GROUP_DATA;
8196 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8197 }
8198out:
liuboc59021f2011-03-07 02:13:14 +00008199 return ret;
8200}
8201
liuboacce9522011-01-06 19:30:25 +08008202int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8203{
8204 return unpin_extent_range(root, start, end);
8205}
8206
8207int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008208 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008209{
Li Dongyang5378e602011-03-24 10:24:27 +00008210 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008211}
Li Dongyangf7039b12011-03-24 10:24:28 +00008212
8213int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8214{
8215 struct btrfs_fs_info *fs_info = root->fs_info;
8216 struct btrfs_block_group_cache *cache = NULL;
8217 u64 group_trimmed;
8218 u64 start;
8219 u64 end;
8220 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008221 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008222 int ret = 0;
8223
Liu Bo2cac13e2012-02-09 18:17:41 +08008224 /*
8225 * try to trim all FS space, our block group may start from non-zero.
8226 */
8227 if (range->len == total_bytes)
8228 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8229 else
8230 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008231
8232 while (cache) {
8233 if (cache->key.objectid >= (range->start + range->len)) {
8234 btrfs_put_block_group(cache);
8235 break;
8236 }
8237
8238 start = max(range->start, cache->key.objectid);
8239 end = min(range->start + range->len,
8240 cache->key.objectid + cache->key.offset);
8241
8242 if (end - start >= range->minlen) {
8243 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008244 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008245 if (!ret)
8246 wait_block_group_cache_done(cache);
8247 }
8248 ret = btrfs_trim_block_group(cache,
8249 &group_trimmed,
8250 start,
8251 end,
8252 range->minlen);
8253
8254 trimmed += group_trimmed;
8255 if (ret) {
8256 btrfs_put_block_group(cache);
8257 break;
8258 }
8259 }
8260
8261 cache = next_block_group(fs_info->tree_root, cache);
8262 }
8263
8264 range->len = trimmed;
8265 return ret;
8266}