blob: 115d1646bf508480fcd0e0d92d9058f405d47504 [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 Bacik6a632092009-02-20 11:00:09 -0500105
Josef Bacik817d52f2009-07-13 21:29:25 -0400106static noinline int
107block_group_cache_done(struct btrfs_block_group_cache *cache)
108{
109 smp_mb();
110 return cache->cached == BTRFS_CACHE_FINISHED;
111}
112
Josef Bacik0f9dd462008-09-23 13:14:11 -0400113static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
114{
115 return (cache->flags & bits) == bits;
116}
117
David Sterba62a45b62011-04-20 15:52:26 +0200118static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000119{
120 atomic_inc(&cache->count);
121}
122
123void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
124{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400125 if (atomic_dec_and_test(&cache->count)) {
126 WARN_ON(cache->pinned > 0);
127 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800128 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000129 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400130 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000131}
132
Josef Bacik0f9dd462008-09-23 13:14:11 -0400133/*
134 * this adds the block group to the fs_info rb tree for the block group
135 * cache
136 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500137static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400138 struct btrfs_block_group_cache *block_group)
139{
140 struct rb_node **p;
141 struct rb_node *parent = NULL;
142 struct btrfs_block_group_cache *cache;
143
144 spin_lock(&info->block_group_cache_lock);
145 p = &info->block_group_cache_tree.rb_node;
146
147 while (*p) {
148 parent = *p;
149 cache = rb_entry(parent, struct btrfs_block_group_cache,
150 cache_node);
151 if (block_group->key.objectid < cache->key.objectid) {
152 p = &(*p)->rb_left;
153 } else if (block_group->key.objectid > cache->key.objectid) {
154 p = &(*p)->rb_right;
155 } else {
156 spin_unlock(&info->block_group_cache_lock);
157 return -EEXIST;
158 }
159 }
160
161 rb_link_node(&block_group->cache_node, parent, p);
162 rb_insert_color(&block_group->cache_node,
163 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000164
165 if (info->first_logical_byte > block_group->key.objectid)
166 info->first_logical_byte = block_group->key.objectid;
167
Josef Bacik0f9dd462008-09-23 13:14:11 -0400168 spin_unlock(&info->block_group_cache_lock);
169
170 return 0;
171}
172
173/*
174 * This will return the block group at or after bytenr if contains is 0, else
175 * it will return the block group that contains the bytenr
176 */
177static struct btrfs_block_group_cache *
178block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
179 int contains)
180{
181 struct btrfs_block_group_cache *cache, *ret = NULL;
182 struct rb_node *n;
183 u64 end, start;
184
185 spin_lock(&info->block_group_cache_lock);
186 n = info->block_group_cache_tree.rb_node;
187
188 while (n) {
189 cache = rb_entry(n, struct btrfs_block_group_cache,
190 cache_node);
191 end = cache->key.objectid + cache->key.offset - 1;
192 start = cache->key.objectid;
193
194 if (bytenr < start) {
195 if (!contains && (!ret || start < ret->key.objectid))
196 ret = cache;
197 n = n->rb_left;
198 } else if (bytenr > start) {
199 if (contains && bytenr <= end) {
200 ret = cache;
201 break;
202 }
203 n = n->rb_right;
204 } else {
205 ret = cache;
206 break;
207 }
208 }
Liu Boa1897fd2012-12-27 09:01:23 +0000209 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000210 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000211 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
212 info->first_logical_byte = ret->key.objectid;
213 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400214 spin_unlock(&info->block_group_cache_lock);
215
216 return ret;
217}
218
Yan Zheng11833d62009-09-11 16:11:19 -0400219static int add_excluded_extent(struct btrfs_root *root,
220 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400221{
Yan Zheng11833d62009-09-11 16:11:19 -0400222 u64 end = start + num_bytes - 1;
223 set_extent_bits(&root->fs_info->freed_extents[0],
224 start, end, EXTENT_UPTODATE, GFP_NOFS);
225 set_extent_bits(&root->fs_info->freed_extents[1],
226 start, end, EXTENT_UPTODATE, GFP_NOFS);
227 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400228}
229
Yan Zheng11833d62009-09-11 16:11:19 -0400230static void free_excluded_extents(struct btrfs_root *root,
231 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400232{
Yan Zheng11833d62009-09-11 16:11:19 -0400233 u64 start, end;
234
235 start = cache->key.objectid;
236 end = start + cache->key.offset - 1;
237
238 clear_extent_bits(&root->fs_info->freed_extents[0],
239 start, end, EXTENT_UPTODATE, GFP_NOFS);
240 clear_extent_bits(&root->fs_info->freed_extents[1],
241 start, end, EXTENT_UPTODATE, GFP_NOFS);
242}
243
244static int exclude_super_stripes(struct btrfs_root *root,
245 struct btrfs_block_group_cache *cache)
246{
Josef Bacik817d52f2009-07-13 21:29:25 -0400247 u64 bytenr;
248 u64 *logical;
249 int stripe_len;
250 int i, nr, ret;
251
Yan, Zheng06b23312009-11-26 09:31:11 +0000252 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
253 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
254 cache->bytes_super += stripe_len;
255 ret = add_excluded_extent(root, cache->key.objectid,
256 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100257 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng06b23312009-11-26 09:31:11 +0000258 }
259
Josef Bacik817d52f2009-07-13 21:29:25 -0400260 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
261 bytenr = btrfs_sb_offset(i);
262 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
263 cache->key.objectid, bytenr,
264 0, &logical, &nr, &stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100265 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -0400266
Josef Bacik817d52f2009-07-13 21:29:25 -0400267 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400268 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400269 ret = add_excluded_extent(root, logical[nr],
270 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100271 BUG_ON(ret); /* -ENOMEM */
Josef Bacik817d52f2009-07-13 21:29:25 -0400272 }
Yan Zheng11833d62009-09-11 16:11:19 -0400273
Josef Bacik817d52f2009-07-13 21:29:25 -0400274 kfree(logical);
275 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400276 return 0;
277}
278
Yan Zheng11833d62009-09-11 16:11:19 -0400279static struct btrfs_caching_control *
280get_caching_control(struct btrfs_block_group_cache *cache)
281{
282 struct btrfs_caching_control *ctl;
283
284 spin_lock(&cache->lock);
285 if (cache->cached != BTRFS_CACHE_STARTED) {
286 spin_unlock(&cache->lock);
287 return NULL;
288 }
289
Josef Bacikdde5abe2010-09-16 16:17:03 -0400290 /* We're loading it the fast way, so we don't have a caching_ctl. */
291 if (!cache->caching_ctl) {
292 spin_unlock(&cache->lock);
293 return NULL;
294 }
295
Yan Zheng11833d62009-09-11 16:11:19 -0400296 ctl = cache->caching_ctl;
297 atomic_inc(&ctl->count);
298 spin_unlock(&cache->lock);
299 return ctl;
300}
301
302static void put_caching_control(struct btrfs_caching_control *ctl)
303{
304 if (atomic_dec_and_test(&ctl->count))
305 kfree(ctl);
306}
307
Josef Bacik0f9dd462008-09-23 13:14:11 -0400308/*
309 * this is only called by cache_block_group, since we could have freed extents
310 * we need to check the pinned_extents for any extents that can't be used yet
311 * since their free space will be released as soon as the transaction commits.
312 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400313static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314 struct btrfs_fs_info *info, u64 start, u64 end)
315{
Josef Bacik817d52f2009-07-13 21:29:25 -0400316 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 int ret;
318
319 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400320 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400321 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400322 EXTENT_DIRTY | EXTENT_UPTODATE,
323 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400324 if (ret)
325 break;
326
Yan, Zheng06b23312009-11-26 09:31:11 +0000327 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400328 start = extent_end + 1;
329 } else if (extent_start > start && extent_start < end) {
330 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400331 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500332 ret = btrfs_add_free_space(block_group, start,
333 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100334 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400335 start = extent_end + 1;
336 } else {
337 break;
338 }
339 }
340
341 if (start < end) {
342 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400343 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500344 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100345 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400346 }
347
Josef Bacik817d52f2009-07-13 21:29:25 -0400348 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400349}
350
Josef Bacikbab39bf2011-06-30 14:42:28 -0400351static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400352{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400353 struct btrfs_block_group_cache *block_group;
354 struct btrfs_fs_info *fs_info;
355 struct btrfs_caching_control *caching_ctl;
356 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400357 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400358 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400359 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400360 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400361 u64 last = 0;
362 u32 nritems;
363 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400364
Josef Bacikbab39bf2011-06-30 14:42:28 -0400365 caching_ctl = container_of(work, struct btrfs_caching_control, work);
366 block_group = caching_ctl->block_group;
367 fs_info = block_group->fs_info;
368 extent_root = fs_info->extent_root;
369
Chris Masone37c9e62007-05-09 20:13:14 -0400370 path = btrfs_alloc_path();
371 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400372 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400373
Josef Bacik817d52f2009-07-13 21:29:25 -0400374 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400375
Chris Mason5cd57b22008-06-25 16:01:30 -0400376 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400377 * We don't want to deadlock with somebody trying to allocate a new
378 * extent for the extent root while also trying to search the extent
379 * root to add free space. So we skip locking and search the commit
380 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400381 */
382 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400383 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400384 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400385
Yan Zhenge4404d62008-12-12 10:03:26 -0500386 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400387 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400388 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400389again:
Yan Zheng11833d62009-09-11 16:11:19 -0400390 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400391 /* need to make sure the commit_root doesn't disappear */
392 down_read(&fs_info->extent_commit_sem);
393
Yan Zheng11833d62009-09-11 16:11:19 -0400394 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400395 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400396 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500397
Yan Zheng11833d62009-09-11 16:11:19 -0400398 leaf = path->nodes[0];
399 nritems = btrfs_header_nritems(leaf);
400
Chris Masond3977122009-01-05 21:25:51 -0500401 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200402 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400403 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400404 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400405 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400406
Yan Zheng11833d62009-09-11 16:11:19 -0400407 if (path->slots[0] < nritems) {
408 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
409 } else {
410 ret = find_next_key(path, 0, &key);
411 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400412 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400413
Josef Bacik589d8ad2011-05-11 17:30:53 -0400414 if (need_resched() ||
415 btrfs_next_leaf(extent_root, path)) {
416 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400417 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400418 up_read(&fs_info->extent_commit_sem);
419 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400420 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400421 goto again;
422 }
423 leaf = path->nodes[0];
424 nritems = btrfs_header_nritems(leaf);
425 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400426 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400427
Yan Zheng11833d62009-09-11 16:11:19 -0400428 if (key.objectid < block_group->key.objectid) {
429 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400431 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400432
Chris Masone37c9e62007-05-09 20:13:14 -0400433 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400434 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400435 break;
Yan7d7d6062007-09-14 16:15:28 -0400436
Yan Zheng11833d62009-09-11 16:11:19 -0400437 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400438 total_found += add_new_free_space(block_group,
439 fs_info, last,
440 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400441 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400442
Yan Zheng11833d62009-09-11 16:11:19 -0400443 if (total_found > (1024 * 1024 * 2)) {
444 total_found = 0;
445 wake_up(&caching_ctl->wait);
446 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400447 }
Chris Masone37c9e62007-05-09 20:13:14 -0400448 path->slots[0]++;
449 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400450 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400451
452 total_found += add_new_free_space(block_group, fs_info, last,
453 block_group->key.objectid +
454 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400455 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400456
457 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400458 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400459 block_group->cached = BTRFS_CACHE_FINISHED;
460 spin_unlock(&block_group->lock);
461
Chris Mason54aa1f42007-06-22 14:16:25 -0400462err:
Chris Masone37c9e62007-05-09 20:13:14 -0400463 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400464 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400465
Yan Zheng11833d62009-09-11 16:11:19 -0400466 free_excluded_extents(extent_root, block_group);
467
468 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400469out:
Yan Zheng11833d62009-09-11 16:11:19 -0400470 wake_up(&caching_ctl->wait);
471
472 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000473 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400474}
475
Josef Bacik9d66e232010-08-25 16:54:15 -0400476static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400477 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400478{
Josef Bacik291c7d22011-11-14 13:52:14 -0500479 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400480 struct btrfs_fs_info *fs_info = cache->fs_info;
481 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400482 int ret = 0;
483
Josef Bacik291c7d22011-11-14 13:52:14 -0500484 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100485 if (!caching_ctl)
486 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500487
488 INIT_LIST_HEAD(&caching_ctl->list);
489 mutex_init(&caching_ctl->mutex);
490 init_waitqueue_head(&caching_ctl->wait);
491 caching_ctl->block_group = cache;
492 caching_ctl->progress = cache->key.objectid;
493 atomic_set(&caching_ctl->count, 1);
494 caching_ctl->work.func = caching_thread;
495
496 spin_lock(&cache->lock);
497 /*
498 * This should be a rare occasion, but this could happen I think in the
499 * case where one thread starts to load the space cache info, and then
500 * some other thread starts a transaction commit which tries to do an
501 * allocation while the other thread is still loading the space cache
502 * info. The previous loop should have kept us from choosing this block
503 * group, but if we've moved to the state where we will wait on caching
504 * block groups we need to first check if we're doing a fast load here,
505 * so we can wait for it to finish, otherwise we could end up allocating
506 * from a block group who's cache gets evicted for one reason or
507 * another.
508 */
509 while (cache->cached == BTRFS_CACHE_FAST) {
510 struct btrfs_caching_control *ctl;
511
512 ctl = cache->caching_ctl;
513 atomic_inc(&ctl->count);
514 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
515 spin_unlock(&cache->lock);
516
517 schedule();
518
519 finish_wait(&ctl->wait, &wait);
520 put_caching_control(ctl);
521 spin_lock(&cache->lock);
522 }
523
524 if (cache->cached != BTRFS_CACHE_NO) {
525 spin_unlock(&cache->lock);
526 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400527 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500528 }
529 WARN_ON(cache->caching_ctl);
530 cache->caching_ctl = caching_ctl;
531 cache->cached = BTRFS_CACHE_FAST;
532 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400533
Josef Bacikd53ba472012-04-12 16:03:57 -0400534 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400535 ret = load_free_space_cache(fs_info, cache);
536
537 spin_lock(&cache->lock);
538 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500539 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400540 cache->cached = BTRFS_CACHE_FINISHED;
541 cache->last_byte_to_unpin = (u64)-1;
542 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500543 if (load_cache_only) {
544 cache->caching_ctl = NULL;
545 cache->cached = BTRFS_CACHE_NO;
546 } else {
547 cache->cached = BTRFS_CACHE_STARTED;
548 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400549 }
550 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500551 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000552 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500553 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000554 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400555 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000556 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500557 } else {
558 /*
559 * We are not going to do the fast caching, set cached to the
560 * appropriate value and wakeup any waiters.
561 */
562 spin_lock(&cache->lock);
563 if (load_cache_only) {
564 cache->caching_ctl = NULL;
565 cache->cached = BTRFS_CACHE_NO;
566 } else {
567 cache->cached = BTRFS_CACHE_STARTED;
568 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400569 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500570 wake_up(&caching_ctl->wait);
571 }
572
573 if (load_cache_only) {
574 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400575 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400576 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400577
Yan Zheng11833d62009-09-11 16:11:19 -0400578 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500579 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400580 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
581 up_write(&fs_info->extent_commit_sem);
582
Josef Bacik11dfe352009-11-13 20:12:59 +0000583 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400584
Josef Bacikbab39bf2011-06-30 14:42:28 -0400585 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400586
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400587 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400588}
589
Josef Bacik0f9dd462008-09-23 13:14:11 -0400590/*
591 * return the block group that starts at or after bytenr
592 */
Chris Masond3977122009-01-05 21:25:51 -0500593static struct btrfs_block_group_cache *
594btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400595{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400596 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400597
Josef Bacik0f9dd462008-09-23 13:14:11 -0400598 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400599
Josef Bacik0f9dd462008-09-23 13:14:11 -0400600 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400601}
602
Josef Bacik0f9dd462008-09-23 13:14:11 -0400603/*
Sankar P9f556842009-05-14 13:52:22 -0400604 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400605 */
Chris Masond3977122009-01-05 21:25:51 -0500606struct btrfs_block_group_cache *btrfs_lookup_block_group(
607 struct btrfs_fs_info *info,
608 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400609{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400610 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400611
Josef Bacik0f9dd462008-09-23 13:14:11 -0400612 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400613
Josef Bacik0f9dd462008-09-23 13:14:11 -0400614 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400615}
Chris Mason0b86a832008-03-24 15:01:56 -0400616
Josef Bacik0f9dd462008-09-23 13:14:11 -0400617static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
618 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400619{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400620 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400621 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400622
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200623 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400624
Chris Mason4184ea72009-03-10 12:39:20 -0400625 rcu_read_lock();
626 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400627 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400628 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400629 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400630 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400631 }
Chris Mason4184ea72009-03-10 12:39:20 -0400632 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400633 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400634}
635
Chris Mason4184ea72009-03-10 12:39:20 -0400636/*
637 * after adding space to the filesystem, we need to clear the full flags
638 * on all the space infos.
639 */
640void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
641{
642 struct list_head *head = &info->space_info;
643 struct btrfs_space_info *found;
644
645 rcu_read_lock();
646 list_for_each_entry_rcu(found, head, list)
647 found->full = 0;
648 rcu_read_unlock();
649}
650
Yan Zhengd2fb3432008-12-11 16:30:39 -0500651u64 btrfs_find_block_group(struct btrfs_root *root,
652 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400653{
Chris Mason96b51792007-10-15 16:15:19 -0400654 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400655 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500656 u64 last = max(search_hint, search_start);
657 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400658 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500659 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400660 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400661again:
Zheng Yane8569812008-09-26 10:05:48 -0400662 while (1) {
663 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400664 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400665 break;
Chris Mason96b51792007-10-15 16:15:19 -0400666
Chris Masonc286ac42008-07-22 23:06:41 -0400667 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400668 last = cache->key.objectid + cache->key.offset;
669 used = btrfs_block_group_used(&cache->item);
670
Yan Zhengd2fb3432008-12-11 16:30:39 -0500671 if ((full_search || !cache->ro) &&
672 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400673 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500674 div_factor(cache->key.offset, factor)) {
675 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400676 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400677 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400678 goto found;
679 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400680 }
Chris Masonc286ac42008-07-22 23:06:41 -0400681 spin_unlock(&cache->lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -0400682 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400683 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400684 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400685 if (!wrapped) {
686 last = search_start;
687 wrapped = 1;
688 goto again;
689 }
690 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400691 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400692 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400693 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400694 goto again;
695 }
Chris Masonbe744172007-05-06 10:15:01 -0400696found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500697 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400698}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400699
Chris Masone02119d2008-09-05 16:13:11 -0400700/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400701int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400702{
703 int ret;
704 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400705 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400706
Zheng Yan31840ae2008-09-23 13:14:14 -0400707 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700708 if (!path)
709 return -ENOMEM;
710
Chris Masone02119d2008-09-05 16:13:11 -0400711 key.objectid = start;
712 key.offset = len;
713 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
714 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
715 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400716 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500717 return ret;
718}
719
Chris Masond8d5f3e2007-12-11 12:42:00 -0500720/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400721 * helper function to lookup reference count and flags of extent.
722 *
723 * the head node for delayed ref is used to store the sum of all the
724 * reference count modifications queued up in the rbtree. the head
725 * node may also store the extent flags to set. This way you can check
726 * to see what the reference count and extent flags would be if all of
727 * the delayed refs are not processed.
728 */
729int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
730 struct btrfs_root *root, u64 bytenr,
731 u64 num_bytes, u64 *refs, u64 *flags)
732{
733 struct btrfs_delayed_ref_head *head;
734 struct btrfs_delayed_ref_root *delayed_refs;
735 struct btrfs_path *path;
736 struct btrfs_extent_item *ei;
737 struct extent_buffer *leaf;
738 struct btrfs_key key;
739 u32 item_size;
740 u64 num_refs;
741 u64 extent_flags;
742 int ret;
743
744 path = btrfs_alloc_path();
745 if (!path)
746 return -ENOMEM;
747
748 key.objectid = bytenr;
749 key.type = BTRFS_EXTENT_ITEM_KEY;
750 key.offset = num_bytes;
751 if (!trans) {
752 path->skip_locking = 1;
753 path->search_commit_root = 1;
754 }
755again:
756 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
757 &key, path, 0, 0);
758 if (ret < 0)
759 goto out_free;
760
761 if (ret == 0) {
762 leaf = path->nodes[0];
763 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
764 if (item_size >= sizeof(*ei)) {
765 ei = btrfs_item_ptr(leaf, path->slots[0],
766 struct btrfs_extent_item);
767 num_refs = btrfs_extent_refs(leaf, ei);
768 extent_flags = btrfs_extent_flags(leaf, ei);
769 } else {
770#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
771 struct btrfs_extent_item_v0 *ei0;
772 BUG_ON(item_size != sizeof(*ei0));
773 ei0 = btrfs_item_ptr(leaf, path->slots[0],
774 struct btrfs_extent_item_v0);
775 num_refs = btrfs_extent_refs_v0(leaf, ei0);
776 /* FIXME: this isn't correct for data */
777 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
778#else
779 BUG();
780#endif
781 }
782 BUG_ON(num_refs == 0);
783 } else {
784 num_refs = 0;
785 extent_flags = 0;
786 ret = 0;
787 }
788
789 if (!trans)
790 goto out;
791
792 delayed_refs = &trans->transaction->delayed_refs;
793 spin_lock(&delayed_refs->lock);
794 head = btrfs_find_delayed_ref_head(trans, bytenr);
795 if (head) {
796 if (!mutex_trylock(&head->mutex)) {
797 atomic_inc(&head->node.refs);
798 spin_unlock(&delayed_refs->lock);
799
David Sterbab3b4aa72011-04-21 01:20:15 +0200800 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400801
David Sterba8cc33e52011-05-02 15:29:25 +0200802 /*
803 * Mutex was contended, block until it's released and try
804 * again
805 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400806 mutex_lock(&head->mutex);
807 mutex_unlock(&head->mutex);
808 btrfs_put_delayed_ref(&head->node);
809 goto again;
810 }
811 if (head->extent_op && head->extent_op->update_flags)
812 extent_flags |= head->extent_op->flags_to_set;
813 else
814 BUG_ON(num_refs == 0);
815
816 num_refs += head->node.ref_mod;
817 mutex_unlock(&head->mutex);
818 }
819 spin_unlock(&delayed_refs->lock);
820out:
821 WARN_ON(num_refs == 0);
822 if (refs)
823 *refs = num_refs;
824 if (flags)
825 *flags = extent_flags;
826out_free:
827 btrfs_free_path(path);
828 return ret;
829}
830
831/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500832 * Back reference rules. Back refs have three main goals:
833 *
834 * 1) differentiate between all holders of references to an extent so that
835 * when a reference is dropped we can make sure it was a valid reference
836 * before freeing the extent.
837 *
838 * 2) Provide enough information to quickly find the holders of an extent
839 * if we notice a given block is corrupted or bad.
840 *
841 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
842 * maintenance. This is actually the same as #2, but with a slightly
843 * different use case.
844 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845 * There are two kinds of back refs. The implicit back refs is optimized
846 * for pointers in non-shared tree blocks. For a given pointer in a block,
847 * back refs of this kind provide information about the block's owner tree
848 * and the pointer's key. These information allow us to find the block by
849 * b-tree searching. The full back refs is for pointers in tree blocks not
850 * referenced by their owner trees. The location of tree block is recorded
851 * in the back refs. Actually the full back refs is generic, and can be
852 * used in all cases the implicit back refs is used. The major shortcoming
853 * of the full back refs is its overhead. Every time a tree block gets
854 * COWed, we have to update back refs entry for all pointers in it.
855 *
856 * For a newly allocated tree block, we use implicit back refs for
857 * pointers in it. This means most tree related operations only involve
858 * implicit back refs. For a tree block created in old transaction, the
859 * only way to drop a reference to it is COW it. So we can detect the
860 * event that tree block loses its owner tree's reference and do the
861 * back refs conversion.
862 *
863 * When a tree block is COW'd through a tree, there are four cases:
864 *
865 * The reference count of the block is one and the tree is the block's
866 * owner tree. Nothing to do in this case.
867 *
868 * The reference count of the block is one and the tree is not the
869 * block's owner tree. In this case, full back refs is used for pointers
870 * in the block. Remove these full back refs, add implicit back refs for
871 * every pointers in the new block.
872 *
873 * The reference count of the block is greater than one and the tree is
874 * the block's owner tree. In this case, implicit back refs is used for
875 * pointers in the block. Add full back refs for every pointers in the
876 * block, increase lower level extents' reference counts. The original
877 * implicit back refs are entailed to the new block.
878 *
879 * The reference count of the block is greater than one and the tree is
880 * not the block's owner tree. Add implicit back refs for every pointer in
881 * the new block, increase lower level extents' reference count.
882 *
883 * Back Reference Key composing:
884 *
885 * The key objectid corresponds to the first byte in the extent,
886 * The key type is used to differentiate between types of back refs.
887 * There are different meanings of the key offset for different types
888 * of back refs.
889 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500890 * File extents can be referenced by:
891 *
892 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400893 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500894 * - different offsets inside a file (bookend extents in file.c)
895 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400896 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500897 *
898 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500899 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900 * - original offset in the file
901 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400902 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400903 * The key offset for the implicit back refs is hash of the first
904 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500905 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500907 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500909 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910 * The key offset for the implicit back refs is the first byte of
911 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500912 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 * When a file extent is allocated, The implicit back refs is used.
914 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500915 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 * (root_key.objectid, inode objectid, offset in file, 1)
917 *
918 * When a file extent is removed file truncation, we find the
919 * corresponding implicit back refs and check the following fields:
920 *
921 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500922 *
923 * Btree extents can be referenced by:
924 *
925 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500926 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400927 * Both the implicit back refs and the full back refs for tree blocks
928 * only consist of key. The key offset for the implicit back refs is
929 * objectid of block's owner tree. The key offset for the full back refs
930 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500931 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400932 * When implicit back refs is used, information about the lowest key and
933 * level of the tree block are required. These information are stored in
934 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500935 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400936
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400937#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
938static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
939 struct btrfs_root *root,
940 struct btrfs_path *path,
941 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500942{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400943 struct btrfs_extent_item *item;
944 struct btrfs_extent_item_v0 *ei0;
945 struct btrfs_extent_ref_v0 *ref0;
946 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400947 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400948 struct btrfs_key key;
949 struct btrfs_key found_key;
950 u32 new_size = sizeof(*item);
951 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500952 int ret;
953
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400954 leaf = path->nodes[0];
955 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500956
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
958 ei0 = btrfs_item_ptr(leaf, path->slots[0],
959 struct btrfs_extent_item_v0);
960 refs = btrfs_extent_refs_v0(leaf, ei0);
961
962 if (owner == (u64)-1) {
963 while (1) {
964 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
965 ret = btrfs_next_leaf(root, path);
966 if (ret < 0)
967 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100968 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 leaf = path->nodes[0];
970 }
971 btrfs_item_key_to_cpu(leaf, &found_key,
972 path->slots[0]);
973 BUG_ON(key.objectid != found_key.objectid);
974 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
975 path->slots[0]++;
976 continue;
977 }
978 ref0 = btrfs_item_ptr(leaf, path->slots[0],
979 struct btrfs_extent_ref_v0);
980 owner = btrfs_ref_objectid_v0(leaf, ref0);
981 break;
982 }
983 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200984 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985
986 if (owner < BTRFS_FIRST_FREE_OBJECTID)
987 new_size += sizeof(*bi);
988
989 new_size -= sizeof(*ei0);
990 ret = btrfs_search_slot(trans, root, &key, path,
991 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400992 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400993 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995
Jeff Mahoney143bede2012-03-01 14:56:26 +0100996 btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997
998 leaf = path->nodes[0];
999 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1000 btrfs_set_extent_refs(leaf, item, refs);
1001 /* FIXME: get real generation */
1002 btrfs_set_extent_generation(leaf, item, 0);
1003 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1004 btrfs_set_extent_flags(leaf, item,
1005 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1006 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1007 bi = (struct btrfs_tree_block_info *)(item + 1);
1008 /* FIXME: get first key of the block */
1009 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1010 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1011 } else {
1012 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1013 }
1014 btrfs_mark_buffer_dirty(leaf);
1015 return 0;
1016}
1017#endif
1018
1019static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1020{
1021 u32 high_crc = ~(u32)0;
1022 u32 low_crc = ~(u32)0;
1023 __le64 lenum;
1024
1025 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001026 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001028 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001029 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001030 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031
1032 return ((u64)high_crc << 31) ^ (u64)low_crc;
1033}
1034
1035static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1036 struct btrfs_extent_data_ref *ref)
1037{
1038 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1039 btrfs_extent_data_ref_objectid(leaf, ref),
1040 btrfs_extent_data_ref_offset(leaf, ref));
1041}
1042
1043static int match_extent_data_ref(struct extent_buffer *leaf,
1044 struct btrfs_extent_data_ref *ref,
1045 u64 root_objectid, u64 owner, u64 offset)
1046{
1047 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1048 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1049 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1050 return 0;
1051 return 1;
1052}
1053
1054static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1055 struct btrfs_root *root,
1056 struct btrfs_path *path,
1057 u64 bytenr, u64 parent,
1058 u64 root_objectid,
1059 u64 owner, u64 offset)
1060{
1061 struct btrfs_key key;
1062 struct btrfs_extent_data_ref *ref;
1063 struct extent_buffer *leaf;
1064 u32 nritems;
1065 int ret;
1066 int recow;
1067 int err = -ENOENT;
1068
1069 key.objectid = bytenr;
1070 if (parent) {
1071 key.type = BTRFS_SHARED_DATA_REF_KEY;
1072 key.offset = parent;
1073 } else {
1074 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1075 key.offset = hash_extent_data_ref(root_objectid,
1076 owner, offset);
1077 }
1078again:
1079 recow = 0;
1080 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1081 if (ret < 0) {
1082 err = ret;
1083 goto fail;
1084 }
1085
1086 if (parent) {
1087 if (!ret)
1088 return 0;
1089#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1090 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001091 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001092 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1093 if (ret < 0) {
1094 err = ret;
1095 goto fail;
1096 }
1097 if (!ret)
1098 return 0;
1099#endif
1100 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001101 }
1102
1103 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 nritems = btrfs_header_nritems(leaf);
1105 while (1) {
1106 if (path->slots[0] >= nritems) {
1107 ret = btrfs_next_leaf(root, path);
1108 if (ret < 0)
1109 err = ret;
1110 if (ret)
1111 goto fail;
1112
1113 leaf = path->nodes[0];
1114 nritems = btrfs_header_nritems(leaf);
1115 recow = 1;
1116 }
1117
1118 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1119 if (key.objectid != bytenr ||
1120 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1121 goto fail;
1122
1123 ref = btrfs_item_ptr(leaf, path->slots[0],
1124 struct btrfs_extent_data_ref);
1125
1126 if (match_extent_data_ref(leaf, ref, root_objectid,
1127 owner, offset)) {
1128 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001129 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 goto again;
1131 }
1132 err = 0;
1133 break;
1134 }
1135 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001136 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001137fail:
1138 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001139}
1140
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001141static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1142 struct btrfs_root *root,
1143 struct btrfs_path *path,
1144 u64 bytenr, u64 parent,
1145 u64 root_objectid, u64 owner,
1146 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001147{
1148 struct btrfs_key key;
1149 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001150 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001151 u32 num_refs;
1152 int ret;
1153
1154 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001155 if (parent) {
1156 key.type = BTRFS_SHARED_DATA_REF_KEY;
1157 key.offset = parent;
1158 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001159 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001160 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1161 key.offset = hash_extent_data_ref(root_objectid,
1162 owner, offset);
1163 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001164 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001165
1166 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1167 if (ret && ret != -EEXIST)
1168 goto fail;
1169
1170 leaf = path->nodes[0];
1171 if (parent) {
1172 struct btrfs_shared_data_ref *ref;
1173 ref = btrfs_item_ptr(leaf, path->slots[0],
1174 struct btrfs_shared_data_ref);
1175 if (ret == 0) {
1176 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1177 } else {
1178 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1179 num_refs += refs_to_add;
1180 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1181 }
1182 } else {
1183 struct btrfs_extent_data_ref *ref;
1184 while (ret == -EEXIST) {
1185 ref = btrfs_item_ptr(leaf, path->slots[0],
1186 struct btrfs_extent_data_ref);
1187 if (match_extent_data_ref(leaf, ref, root_objectid,
1188 owner, offset))
1189 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001190 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 key.offset++;
1192 ret = btrfs_insert_empty_item(trans, root, path, &key,
1193 size);
1194 if (ret && ret != -EEXIST)
1195 goto fail;
1196
1197 leaf = path->nodes[0];
1198 }
1199 ref = btrfs_item_ptr(leaf, path->slots[0],
1200 struct btrfs_extent_data_ref);
1201 if (ret == 0) {
1202 btrfs_set_extent_data_ref_root(leaf, ref,
1203 root_objectid);
1204 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1205 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1206 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1207 } else {
1208 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1209 num_refs += refs_to_add;
1210 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1211 }
1212 }
1213 btrfs_mark_buffer_dirty(leaf);
1214 ret = 0;
1215fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001216 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001217 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001218}
1219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1221 struct btrfs_root *root,
1222 struct btrfs_path *path,
1223 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001224{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001225 struct btrfs_key key;
1226 struct btrfs_extent_data_ref *ref1 = NULL;
1227 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001228 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001229 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001230 int ret = 0;
1231
1232 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001233 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1234
1235 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1236 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1237 struct btrfs_extent_data_ref);
1238 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1239 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1240 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1241 struct btrfs_shared_data_ref);
1242 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1243#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1244 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1245 struct btrfs_extent_ref_v0 *ref0;
1246 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1247 struct btrfs_extent_ref_v0);
1248 num_refs = btrfs_ref_count_v0(leaf, ref0);
1249#endif
1250 } else {
1251 BUG();
1252 }
1253
Chris Mason56bec292009-03-13 10:10:06 -04001254 BUG_ON(num_refs < refs_to_drop);
1255 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001256
Zheng Yan31840ae2008-09-23 13:14:14 -04001257 if (num_refs == 0) {
1258 ret = btrfs_del_item(trans, root, path);
1259 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001260 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1261 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1262 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1263 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1264#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1265 else {
1266 struct btrfs_extent_ref_v0 *ref0;
1267 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1268 struct btrfs_extent_ref_v0);
1269 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1270 }
1271#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001272 btrfs_mark_buffer_dirty(leaf);
1273 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001274 return ret;
1275}
1276
1277static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1278 struct btrfs_path *path,
1279 struct btrfs_extent_inline_ref *iref)
1280{
1281 struct btrfs_key key;
1282 struct extent_buffer *leaf;
1283 struct btrfs_extent_data_ref *ref1;
1284 struct btrfs_shared_data_ref *ref2;
1285 u32 num_refs = 0;
1286
1287 leaf = path->nodes[0];
1288 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1289 if (iref) {
1290 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1291 BTRFS_EXTENT_DATA_REF_KEY) {
1292 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1293 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1294 } else {
1295 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1296 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1297 }
1298 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1299 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1300 struct btrfs_extent_data_ref);
1301 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1302 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1303 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1304 struct btrfs_shared_data_ref);
1305 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1306#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1307 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1308 struct btrfs_extent_ref_v0 *ref0;
1309 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1310 struct btrfs_extent_ref_v0);
1311 num_refs = btrfs_ref_count_v0(leaf, ref0);
1312#endif
1313 } else {
1314 WARN_ON(1);
1315 }
1316 return num_refs;
1317}
1318
1319static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1320 struct btrfs_root *root,
1321 struct btrfs_path *path,
1322 u64 bytenr, u64 parent,
1323 u64 root_objectid)
1324{
1325 struct btrfs_key key;
1326 int ret;
1327
1328 key.objectid = bytenr;
1329 if (parent) {
1330 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1331 key.offset = parent;
1332 } else {
1333 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1334 key.offset = root_objectid;
1335 }
1336
1337 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1338 if (ret > 0)
1339 ret = -ENOENT;
1340#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1341 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001342 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001343 key.type = BTRFS_EXTENT_REF_V0_KEY;
1344 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1345 if (ret > 0)
1346 ret = -ENOENT;
1347 }
1348#endif
1349 return ret;
1350}
1351
1352static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1353 struct btrfs_root *root,
1354 struct btrfs_path *path,
1355 u64 bytenr, u64 parent,
1356 u64 root_objectid)
1357{
1358 struct btrfs_key key;
1359 int ret;
1360
1361 key.objectid = bytenr;
1362 if (parent) {
1363 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1364 key.offset = parent;
1365 } else {
1366 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1367 key.offset = root_objectid;
1368 }
1369
1370 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001371 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001372 return ret;
1373}
1374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001375static inline int extent_ref_type(u64 parent, u64 owner)
1376{
1377 int type;
1378 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1379 if (parent > 0)
1380 type = BTRFS_SHARED_BLOCK_REF_KEY;
1381 else
1382 type = BTRFS_TREE_BLOCK_REF_KEY;
1383 } else {
1384 if (parent > 0)
1385 type = BTRFS_SHARED_DATA_REF_KEY;
1386 else
1387 type = BTRFS_EXTENT_DATA_REF_KEY;
1388 }
1389 return type;
1390}
1391
Yan Zheng2c47e6052009-06-27 21:07:35 -04001392static int find_next_key(struct btrfs_path *path, int level,
1393 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394
1395{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001396 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 if (!path->nodes[level])
1398 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001399 if (path->slots[level] + 1 >=
1400 btrfs_header_nritems(path->nodes[level]))
1401 continue;
1402 if (level == 0)
1403 btrfs_item_key_to_cpu(path->nodes[level], key,
1404 path->slots[level] + 1);
1405 else
1406 btrfs_node_key_to_cpu(path->nodes[level], key,
1407 path->slots[level] + 1);
1408 return 0;
1409 }
1410 return 1;
1411}
1412
1413/*
1414 * look for inline back ref. if back ref is found, *ref_ret is set
1415 * to the address of inline back ref, and 0 is returned.
1416 *
1417 * if back ref isn't found, *ref_ret is set to the address where it
1418 * should be inserted, and -ENOENT is returned.
1419 *
1420 * if insert is true and there are too many inline back refs, the path
1421 * points to the extent item, and -EAGAIN is returned.
1422 *
1423 * NOTE: inline back refs are ordered in the same way that back ref
1424 * items in the tree are ordered.
1425 */
1426static noinline_for_stack
1427int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1428 struct btrfs_root *root,
1429 struct btrfs_path *path,
1430 struct btrfs_extent_inline_ref **ref_ret,
1431 u64 bytenr, u64 num_bytes,
1432 u64 parent, u64 root_objectid,
1433 u64 owner, u64 offset, int insert)
1434{
1435 struct btrfs_key key;
1436 struct extent_buffer *leaf;
1437 struct btrfs_extent_item *ei;
1438 struct btrfs_extent_inline_ref *iref;
1439 u64 flags;
1440 u64 item_size;
1441 unsigned long ptr;
1442 unsigned long end;
1443 int extra_size;
1444 int type;
1445 int want;
1446 int ret;
1447 int err = 0;
1448
1449 key.objectid = bytenr;
1450 key.type = BTRFS_EXTENT_ITEM_KEY;
1451 key.offset = num_bytes;
1452
1453 want = extent_ref_type(parent, owner);
1454 if (insert) {
1455 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001456 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001457 } else
1458 extra_size = -1;
1459 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1460 if (ret < 0) {
1461 err = ret;
1462 goto out;
1463 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001464 if (ret && !insert) {
1465 err = -ENOENT;
1466 goto out;
1467 }
1468 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001469
1470 leaf = path->nodes[0];
1471 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1472#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1473 if (item_size < sizeof(*ei)) {
1474 if (!insert) {
1475 err = -ENOENT;
1476 goto out;
1477 }
1478 ret = convert_extent_item_v0(trans, root, path, owner,
1479 extra_size);
1480 if (ret < 0) {
1481 err = ret;
1482 goto out;
1483 }
1484 leaf = path->nodes[0];
1485 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1486 }
1487#endif
1488 BUG_ON(item_size < sizeof(*ei));
1489
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001490 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1491 flags = btrfs_extent_flags(leaf, ei);
1492
1493 ptr = (unsigned long)(ei + 1);
1494 end = (unsigned long)ei + item_size;
1495
1496 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1497 ptr += sizeof(struct btrfs_tree_block_info);
1498 BUG_ON(ptr > end);
1499 } else {
1500 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1501 }
1502
1503 err = -ENOENT;
1504 while (1) {
1505 if (ptr >= end) {
1506 WARN_ON(ptr > end);
1507 break;
1508 }
1509 iref = (struct btrfs_extent_inline_ref *)ptr;
1510 type = btrfs_extent_inline_ref_type(leaf, iref);
1511 if (want < type)
1512 break;
1513 if (want > type) {
1514 ptr += btrfs_extent_inline_ref_size(type);
1515 continue;
1516 }
1517
1518 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1519 struct btrfs_extent_data_ref *dref;
1520 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1521 if (match_extent_data_ref(leaf, dref, root_objectid,
1522 owner, offset)) {
1523 err = 0;
1524 break;
1525 }
1526 if (hash_extent_data_ref_item(leaf, dref) <
1527 hash_extent_data_ref(root_objectid, owner, offset))
1528 break;
1529 } else {
1530 u64 ref_offset;
1531 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1532 if (parent > 0) {
1533 if (parent == ref_offset) {
1534 err = 0;
1535 break;
1536 }
1537 if (ref_offset < parent)
1538 break;
1539 } else {
1540 if (root_objectid == ref_offset) {
1541 err = 0;
1542 break;
1543 }
1544 if (ref_offset < root_objectid)
1545 break;
1546 }
1547 }
1548 ptr += btrfs_extent_inline_ref_size(type);
1549 }
1550 if (err == -ENOENT && insert) {
1551 if (item_size + extra_size >=
1552 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1553 err = -EAGAIN;
1554 goto out;
1555 }
1556 /*
1557 * To add new inline back ref, we have to make sure
1558 * there is no corresponding back ref item.
1559 * For simplicity, we just do not add new inline back
1560 * ref if there is any kind of item for this block
1561 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001562 if (find_next_key(path, 0, &key) == 0 &&
1563 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001564 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001565 err = -EAGAIN;
1566 goto out;
1567 }
1568 }
1569 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1570out:
Yan Zheng85d41982009-06-11 08:51:10 -04001571 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001572 path->keep_locks = 0;
1573 btrfs_unlock_up_safe(path, 1);
1574 }
1575 return err;
1576}
1577
1578/*
1579 * helper to add new inline back ref
1580 */
1581static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001582void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1583 struct btrfs_root *root,
1584 struct btrfs_path *path,
1585 struct btrfs_extent_inline_ref *iref,
1586 u64 parent, u64 root_objectid,
1587 u64 owner, u64 offset, int refs_to_add,
1588 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001589{
1590 struct extent_buffer *leaf;
1591 struct btrfs_extent_item *ei;
1592 unsigned long ptr;
1593 unsigned long end;
1594 unsigned long item_offset;
1595 u64 refs;
1596 int size;
1597 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001598
1599 leaf = path->nodes[0];
1600 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1601 item_offset = (unsigned long)iref - (unsigned long)ei;
1602
1603 type = extent_ref_type(parent, owner);
1604 size = btrfs_extent_inline_ref_size(type);
1605
Jeff Mahoney143bede2012-03-01 14:56:26 +01001606 btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001607
1608 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1609 refs = btrfs_extent_refs(leaf, ei);
1610 refs += refs_to_add;
1611 btrfs_set_extent_refs(leaf, ei, refs);
1612 if (extent_op)
1613 __run_delayed_extent_op(extent_op, leaf, ei);
1614
1615 ptr = (unsigned long)ei + item_offset;
1616 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1617 if (ptr < end - size)
1618 memmove_extent_buffer(leaf, ptr + size, ptr,
1619 end - size - ptr);
1620
1621 iref = (struct btrfs_extent_inline_ref *)ptr;
1622 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1623 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1624 struct btrfs_extent_data_ref *dref;
1625 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1626 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1627 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1628 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1629 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1630 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1631 struct btrfs_shared_data_ref *sref;
1632 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1633 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1634 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1635 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1636 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1637 } else {
1638 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1639 }
1640 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001641}
1642
1643static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1644 struct btrfs_root *root,
1645 struct btrfs_path *path,
1646 struct btrfs_extent_inline_ref **ref_ret,
1647 u64 bytenr, u64 num_bytes, u64 parent,
1648 u64 root_objectid, u64 owner, u64 offset)
1649{
1650 int ret;
1651
1652 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1653 bytenr, num_bytes, parent,
1654 root_objectid, owner, offset, 0);
1655 if (ret != -ENOENT)
1656 return ret;
1657
David Sterbab3b4aa72011-04-21 01:20:15 +02001658 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001659 *ref_ret = NULL;
1660
1661 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1662 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1663 root_objectid);
1664 } else {
1665 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1666 root_objectid, owner, offset);
1667 }
1668 return ret;
1669}
1670
1671/*
1672 * helper to update/remove inline back ref
1673 */
1674static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001675void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1676 struct btrfs_root *root,
1677 struct btrfs_path *path,
1678 struct btrfs_extent_inline_ref *iref,
1679 int refs_to_mod,
1680 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001681{
1682 struct extent_buffer *leaf;
1683 struct btrfs_extent_item *ei;
1684 struct btrfs_extent_data_ref *dref = NULL;
1685 struct btrfs_shared_data_ref *sref = NULL;
1686 unsigned long ptr;
1687 unsigned long end;
1688 u32 item_size;
1689 int size;
1690 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001691 u64 refs;
1692
1693 leaf = path->nodes[0];
1694 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1695 refs = btrfs_extent_refs(leaf, ei);
1696 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1697 refs += refs_to_mod;
1698 btrfs_set_extent_refs(leaf, ei, refs);
1699 if (extent_op)
1700 __run_delayed_extent_op(extent_op, leaf, ei);
1701
1702 type = btrfs_extent_inline_ref_type(leaf, iref);
1703
1704 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1705 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1706 refs = btrfs_extent_data_ref_count(leaf, dref);
1707 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1708 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1709 refs = btrfs_shared_data_ref_count(leaf, sref);
1710 } else {
1711 refs = 1;
1712 BUG_ON(refs_to_mod != -1);
1713 }
1714
1715 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1716 refs += refs_to_mod;
1717
1718 if (refs > 0) {
1719 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1720 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1721 else
1722 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1723 } else {
1724 size = btrfs_extent_inline_ref_size(type);
1725 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1726 ptr = (unsigned long)iref;
1727 end = (unsigned long)ei + item_size;
1728 if (ptr + size < end)
1729 memmove_extent_buffer(leaf, ptr, ptr + size,
1730 end - ptr - size);
1731 item_size -= size;
Jeff Mahoney143bede2012-03-01 14:56:26 +01001732 btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001733 }
1734 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735}
1736
1737static noinline_for_stack
1738int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1739 struct btrfs_root *root,
1740 struct btrfs_path *path,
1741 u64 bytenr, u64 num_bytes, u64 parent,
1742 u64 root_objectid, u64 owner,
1743 u64 offset, int refs_to_add,
1744 struct btrfs_delayed_extent_op *extent_op)
1745{
1746 struct btrfs_extent_inline_ref *iref;
1747 int ret;
1748
1749 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1750 bytenr, num_bytes, parent,
1751 root_objectid, owner, offset, 1);
1752 if (ret == 0) {
1753 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001754 update_inline_extent_backref(trans, root, path, iref,
1755 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001756 } else if (ret == -ENOENT) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001757 setup_inline_extent_backref(trans, root, path, iref, parent,
1758 root_objectid, owner, offset,
1759 refs_to_add, extent_op);
1760 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001761 }
1762 return ret;
1763}
1764
1765static int insert_extent_backref(struct btrfs_trans_handle *trans,
1766 struct btrfs_root *root,
1767 struct btrfs_path *path,
1768 u64 bytenr, u64 parent, u64 root_objectid,
1769 u64 owner, u64 offset, int refs_to_add)
1770{
1771 int ret;
1772 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1773 BUG_ON(refs_to_add != 1);
1774 ret = insert_tree_block_ref(trans, root, path, bytenr,
1775 parent, root_objectid);
1776 } else {
1777 ret = insert_extent_data_ref(trans, root, path, bytenr,
1778 parent, root_objectid,
1779 owner, offset, refs_to_add);
1780 }
1781 return ret;
1782}
1783
1784static int remove_extent_backref(struct btrfs_trans_handle *trans,
1785 struct btrfs_root *root,
1786 struct btrfs_path *path,
1787 struct btrfs_extent_inline_ref *iref,
1788 int refs_to_drop, int is_data)
1789{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001790 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001791
1792 BUG_ON(!is_data && refs_to_drop != 1);
1793 if (iref) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001794 update_inline_extent_backref(trans, root, path, iref,
1795 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001796 } else if (is_data) {
1797 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1798 } else {
1799 ret = btrfs_del_item(trans, root, path);
1800 }
1801 return ret;
1802}
1803
Li Dongyang5378e602011-03-24 10:24:27 +00001804static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001805 u64 start, u64 len)
1806{
Li Dongyang5378e602011-03-24 10:24:27 +00001807 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001808}
Chris Mason15916de2008-11-19 21:17:22 -05001809
Liu Hui1f3c79a2009-01-05 15:57:51 -05001810static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001811 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001812{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001813 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001814 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001815 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001816
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001817
Liu Hui1f3c79a2009-01-05 15:57:51 -05001818 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001819 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001820 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001821 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001822 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001823 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001824 int i;
1825
Liu Hui1f3c79a2009-01-05 15:57:51 -05001826
Jan Schmidta1d3c472011-08-04 17:15:33 +02001827 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001828 if (!stripe->dev->can_discard)
1829 continue;
1830
Li Dongyang5378e602011-03-24 10:24:27 +00001831 ret = btrfs_issue_discard(stripe->dev->bdev,
1832 stripe->physical,
1833 stripe->length);
1834 if (!ret)
1835 discarded_bytes += stripe->length;
1836 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001837 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001838
1839 /*
1840 * Just in case we get back EOPNOTSUPP for some reason,
1841 * just ignore the return value so we don't screw up
1842 * people calling discard_extent.
1843 */
1844 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001845 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001846 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001847 }
Li Dongyang5378e602011-03-24 10:24:27 +00001848
1849 if (actual_bytes)
1850 *actual_bytes = discarded_bytes;
1851
Liu Hui1f3c79a2009-01-05 15:57:51 -05001852
1853 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001854}
1855
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001856/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001857int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root,
1859 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001860 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001861{
1862 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001863 struct btrfs_fs_info *fs_info = root->fs_info;
1864
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001865 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1866 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001869 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1870 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001871 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001872 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001874 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1875 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001877 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001879 return ret;
1880}
1881
Chris Mason925baed2008-06-25 16:01:30 -04001882static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883 struct btrfs_root *root,
1884 u64 bytenr, u64 num_bytes,
1885 u64 parent, u64 root_objectid,
1886 u64 owner, u64 offset, int refs_to_add,
1887 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001888{
Chris Mason5caf2a02007-04-02 11:20:42 -04001889 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001890 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001891 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892 u64 refs;
1893 int ret;
1894 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001895
Chris Mason5caf2a02007-04-02 11:20:42 -04001896 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001897 if (!path)
1898 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001899
Chris Mason3c12ac72008-04-21 12:01:38 -04001900 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001901 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001902 /* this will setup the path even if it fails to insert the back ref */
1903 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1904 path, bytenr, num_bytes, parent,
1905 root_objectid, owner, offset,
1906 refs_to_add, extent_op);
1907 if (ret == 0)
1908 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001909
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001910 if (ret != -EAGAIN) {
1911 err = ret;
1912 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001913 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001914
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001915 leaf = path->nodes[0];
1916 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1917 refs = btrfs_extent_refs(leaf, item);
1918 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1919 if (extent_op)
1920 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001921
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001922 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001923 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001924
Chris Mason3c12ac72008-04-21 12:01:38 -04001925 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001926 path->leave_spinning = 1;
1927
Chris Mason56bec292009-03-13 10:10:06 -04001928 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001929 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001930 path, bytenr, parent, root_objectid,
1931 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001932 if (ret)
1933 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001934out:
Chris Mason74493f72007-12-11 09:25:06 -05001935 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001936 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001937}
1938
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001939static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1940 struct btrfs_root *root,
1941 struct btrfs_delayed_ref_node *node,
1942 struct btrfs_delayed_extent_op *extent_op,
1943 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001944{
Chris Mason56bec292009-03-13 10:10:06 -04001945 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946 struct btrfs_delayed_data_ref *ref;
1947 struct btrfs_key ins;
1948 u64 parent = 0;
1949 u64 ref_root = 0;
1950 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001951
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952 ins.objectid = node->bytenr;
1953 ins.offset = node->num_bytes;
1954 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001955
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001956 ref = btrfs_delayed_node_to_data_ref(node);
1957 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1958 parent = ref->parent;
1959 else
1960 ref_root = ref->root;
1961
1962 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1963 if (extent_op) {
1964 BUG_ON(extent_op->update_key);
1965 flags |= extent_op->flags_to_set;
1966 }
1967 ret = alloc_reserved_file_extent(trans, root,
1968 parent, ref_root, flags,
1969 ref->objectid, ref->offset,
1970 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001971 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1972 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1973 node->num_bytes, parent,
1974 ref_root, ref->objectid,
1975 ref->offset, node->ref_mod,
1976 extent_op);
1977 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1978 ret = __btrfs_free_extent(trans, root, node->bytenr,
1979 node->num_bytes, parent,
1980 ref_root, ref->objectid,
1981 ref->offset, node->ref_mod,
1982 extent_op);
1983 } else {
1984 BUG();
1985 }
Chris Mason56bec292009-03-13 10:10:06 -04001986 return ret;
1987}
1988
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001989static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1990 struct extent_buffer *leaf,
1991 struct btrfs_extent_item *ei)
1992{
1993 u64 flags = btrfs_extent_flags(leaf, ei);
1994 if (extent_op->update_flags) {
1995 flags |= extent_op->flags_to_set;
1996 btrfs_set_extent_flags(leaf, ei, flags);
1997 }
1998
1999 if (extent_op->update_key) {
2000 struct btrfs_tree_block_info *bi;
2001 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2002 bi = (struct btrfs_tree_block_info *)(ei + 1);
2003 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2004 }
2005}
2006
2007static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2008 struct btrfs_root *root,
2009 struct btrfs_delayed_ref_node *node,
2010 struct btrfs_delayed_extent_op *extent_op)
2011{
2012 struct btrfs_key key;
2013 struct btrfs_path *path;
2014 struct btrfs_extent_item *ei;
2015 struct extent_buffer *leaf;
2016 u32 item_size;
2017 int ret;
2018 int err = 0;
2019
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002020 if (trans->aborted)
2021 return 0;
2022
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002023 path = btrfs_alloc_path();
2024 if (!path)
2025 return -ENOMEM;
2026
2027 key.objectid = node->bytenr;
2028 key.type = BTRFS_EXTENT_ITEM_KEY;
2029 key.offset = node->num_bytes;
2030
2031 path->reada = 1;
2032 path->leave_spinning = 1;
2033 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2034 path, 0, 1);
2035 if (ret < 0) {
2036 err = ret;
2037 goto out;
2038 }
2039 if (ret > 0) {
2040 err = -EIO;
2041 goto out;
2042 }
2043
2044 leaf = path->nodes[0];
2045 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2046#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2047 if (item_size < sizeof(*ei)) {
2048 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2049 path, (u64)-1, 0);
2050 if (ret < 0) {
2051 err = ret;
2052 goto out;
2053 }
2054 leaf = path->nodes[0];
2055 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2056 }
2057#endif
2058 BUG_ON(item_size < sizeof(*ei));
2059 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2060 __run_delayed_extent_op(extent_op, leaf, ei);
2061
2062 btrfs_mark_buffer_dirty(leaf);
2063out:
2064 btrfs_free_path(path);
2065 return err;
2066}
2067
2068static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2069 struct btrfs_root *root,
2070 struct btrfs_delayed_ref_node *node,
2071 struct btrfs_delayed_extent_op *extent_op,
2072 int insert_reserved)
2073{
2074 int ret = 0;
2075 struct btrfs_delayed_tree_ref *ref;
2076 struct btrfs_key ins;
2077 u64 parent = 0;
2078 u64 ref_root = 0;
2079
2080 ins.objectid = node->bytenr;
2081 ins.offset = node->num_bytes;
2082 ins.type = BTRFS_EXTENT_ITEM_KEY;
2083
2084 ref = btrfs_delayed_node_to_tree_ref(node);
2085 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2086 parent = ref->parent;
2087 else
2088 ref_root = ref->root;
2089
2090 BUG_ON(node->ref_mod != 1);
2091 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2092 BUG_ON(!extent_op || !extent_op->update_flags ||
2093 !extent_op->update_key);
2094 ret = alloc_reserved_tree_block(trans, root,
2095 parent, ref_root,
2096 extent_op->flags_to_set,
2097 &extent_op->key,
2098 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002099 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2100 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2101 node->num_bytes, parent, ref_root,
2102 ref->level, 0, 1, extent_op);
2103 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2104 ret = __btrfs_free_extent(trans, root, node->bytenr,
2105 node->num_bytes, parent, ref_root,
2106 ref->level, 0, 1, extent_op);
2107 } else {
2108 BUG();
2109 }
2110 return ret;
2111}
2112
Chris Mason56bec292009-03-13 10:10:06 -04002113/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002114static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2115 struct btrfs_root *root,
2116 struct btrfs_delayed_ref_node *node,
2117 struct btrfs_delayed_extent_op *extent_op,
2118 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002119{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002120 int ret = 0;
2121
2122 if (trans->aborted)
2123 return 0;
2124
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002125 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002126 struct btrfs_delayed_ref_head *head;
2127 /*
2128 * we've hit the end of the chain and we were supposed
2129 * to insert this extent into the tree. But, it got
2130 * deleted before we ever needed to insert it, so all
2131 * we have to do is clean up the accounting
2132 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002133 BUG_ON(extent_op);
2134 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002135 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002136 btrfs_pin_extent(root, node->bytenr,
2137 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002138 if (head->is_data) {
2139 ret = btrfs_del_csums(trans, root,
2140 node->bytenr,
2141 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002142 }
Chris Mason56bec292009-03-13 10:10:06 -04002143 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002144 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002145 }
Josef Bacikeb099672009-02-12 09:27:38 -05002146
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2148 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2149 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2150 insert_reserved);
2151 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2152 node->type == BTRFS_SHARED_DATA_REF_KEY)
2153 ret = run_delayed_data_ref(trans, root, node, extent_op,
2154 insert_reserved);
2155 else
2156 BUG();
2157 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002158}
2159
Chris Mason56bec292009-03-13 10:10:06 -04002160static noinline struct btrfs_delayed_ref_node *
2161select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002162{
Chris Mason56bec292009-03-13 10:10:06 -04002163 struct rb_node *node;
2164 struct btrfs_delayed_ref_node *ref;
2165 int action = BTRFS_ADD_DELAYED_REF;
2166again:
2167 /*
2168 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2169 * this prevents ref count from going down to zero when
2170 * there still are pending delayed ref.
2171 */
2172 node = rb_prev(&head->node.rb_node);
2173 while (1) {
2174 if (!node)
2175 break;
2176 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2177 rb_node);
2178 if (ref->bytenr != head->node.bytenr)
2179 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002180 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002181 return ref;
2182 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002183 }
Chris Mason56bec292009-03-13 10:10:06 -04002184 if (action == BTRFS_ADD_DELAYED_REF) {
2185 action = BTRFS_DROP_DELAYED_REF;
2186 goto again;
2187 }
2188 return NULL;
2189}
2190
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002191/*
2192 * Returns 0 on success or if called with an already aborted transaction.
2193 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2194 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002195static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2196 struct btrfs_root *root,
2197 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002198{
Chris Mason56bec292009-03-13 10:10:06 -04002199 struct btrfs_delayed_ref_root *delayed_refs;
2200 struct btrfs_delayed_ref_node *ref;
2201 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002203 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002204 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002205 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002206 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002207
2208 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002209 while (1) {
2210 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002211 /* pick a new head ref from the cluster list */
2212 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002213 break;
Chris Mason56bec292009-03-13 10:10:06 -04002214
Chris Masonc3e69d52009-03-13 10:17:05 -04002215 locked_ref = list_entry(cluster->next,
2216 struct btrfs_delayed_ref_head, cluster);
2217
2218 /* grab the lock that says we are going to process
2219 * all the refs for this head */
2220 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2221
2222 /*
2223 * we may have dropped the spin lock to get the head
2224 * mutex lock, and that might have given someone else
2225 * time to free the head. If that's true, it has been
2226 * removed from our list and we can move on.
2227 */
2228 if (ret == -EAGAIN) {
2229 locked_ref = NULL;
2230 count++;
2231 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002232 }
2233 }
2234
2235 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002236 * We need to try and merge add/drops of the same ref since we
2237 * can run into issues with relocate dropping the implicit ref
2238 * and then it being added back again before the drop can
2239 * finish. If we merged anything we need to re-loop so we can
2240 * get a good ref.
2241 */
2242 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2243 locked_ref);
2244
2245 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002246 * locked_ref is the head node, so we have to go one
2247 * node back for any delayed ref updates
2248 */
2249 ref = select_delayed_ref(locked_ref);
2250
2251 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002252 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002253 /*
2254 * there are still refs with lower seq numbers in the
2255 * process of being added. Don't run this ref yet.
2256 */
2257 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002258 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002259 locked_ref = NULL;
2260 delayed_refs->num_heads_ready++;
2261 spin_unlock(&delayed_refs->lock);
2262 cond_resched();
2263 spin_lock(&delayed_refs->lock);
2264 continue;
2265 }
2266
2267 /*
Chris Mason56bec292009-03-13 10:10:06 -04002268 * record the must insert reserved flag before we
2269 * drop the spin lock.
2270 */
2271 must_insert_reserved = locked_ref->must_insert_reserved;
2272 locked_ref->must_insert_reserved = 0;
2273
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002274 extent_op = locked_ref->extent_op;
2275 locked_ref->extent_op = NULL;
2276
Chris Mason56bec292009-03-13 10:10:06 -04002277 if (!ref) {
2278 /* All delayed refs have been processed, Go ahead
2279 * and send the head node to run_one_delayed_ref,
2280 * so that any accounting fixes can happen
2281 */
2282 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002283
2284 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002285 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286 extent_op = NULL;
2287 }
2288
2289 if (extent_op) {
2290 spin_unlock(&delayed_refs->lock);
2291
2292 ret = run_delayed_extent_op(trans, root,
2293 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002294 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002295
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002296 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002297 printk(KERN_DEBUG
2298 "btrfs: run_delayed_extent_op "
2299 "returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002300 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002301 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002302 return ret;
2303 }
2304
Chris Mason203bf282012-01-06 15:23:57 -05002305 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002306 }
Chris Mason56bec292009-03-13 10:10:06 -04002307 }
2308
2309 ref->in_tree = 0;
2310 rb_erase(&ref->rb_node, &delayed_refs->root);
2311 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002312 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002313 /*
2314 * when we play the delayed ref, also correct the
2315 * ref_mod on head
2316 */
2317 switch (ref->action) {
2318 case BTRFS_ADD_DELAYED_REF:
2319 case BTRFS_ADD_DELAYED_EXTENT:
2320 locked_ref->node.ref_mod -= ref->ref_mod;
2321 break;
2322 case BTRFS_DROP_DELAYED_REF:
2323 locked_ref->node.ref_mod += ref->ref_mod;
2324 break;
2325 default:
2326 WARN_ON(1);
2327 }
2328 }
Chris Mason56bec292009-03-13 10:10:06 -04002329 spin_unlock(&delayed_refs->lock);
2330
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002331 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002332 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002333
Miao Xie78a61842012-11-21 02:21:28 +00002334 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002335 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002336 btrfs_delayed_ref_unlock(locked_ref);
2337 btrfs_put_delayed_ref(ref);
2338 printk(KERN_DEBUG
2339 "btrfs: run_one_delayed_ref returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002340 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002341 return ret;
2342 }
2343
Miao Xie093486c2012-12-19 08:10:10 +00002344 /*
2345 * If this node is a head, that means all the refs in this head
2346 * have been dealt with, and we will pick the next head to deal
2347 * with, so we must unlock the head and drop it from the cluster
2348 * list before we release it.
2349 */
2350 if (btrfs_delayed_ref_is_head(ref)) {
2351 list_del_init(&locked_ref->cluster);
2352 btrfs_delayed_ref_unlock(locked_ref);
2353 locked_ref = NULL;
2354 }
2355 btrfs_put_delayed_ref(ref);
2356 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002357next:
Chris Mason1887be62009-03-13 10:11:24 -04002358 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002359 spin_lock(&delayed_refs->lock);
2360 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002361 return count;
2362}
2363
Arne Jansen709c0482011-09-12 12:22:57 +02002364#ifdef SCRAMBLE_DELAYED_REFS
2365/*
2366 * Normally delayed refs get processed in ascending bytenr order. This
2367 * correlates in most cases to the order added. To expose dependencies on this
2368 * order, we start to process the tree in the middle instead of the beginning
2369 */
2370static u64 find_middle(struct rb_root *root)
2371{
2372 struct rb_node *n = root->rb_node;
2373 struct btrfs_delayed_ref_node *entry;
2374 int alt = 1;
2375 u64 middle;
2376 u64 first = 0, last = 0;
2377
2378 n = rb_first(root);
2379 if (n) {
2380 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2381 first = entry->bytenr;
2382 }
2383 n = rb_last(root);
2384 if (n) {
2385 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2386 last = entry->bytenr;
2387 }
2388 n = root->rb_node;
2389
2390 while (n) {
2391 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2392 WARN_ON(!entry->in_tree);
2393
2394 middle = entry->bytenr;
2395
2396 if (alt)
2397 n = n->rb_left;
2398 else
2399 n = n->rb_right;
2400
2401 alt = 1 - alt;
2402 }
2403 return middle;
2404}
2405#endif
2406
Arne Jansenbed92ea2012-06-28 18:03:02 +02002407int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2408 struct btrfs_fs_info *fs_info)
2409{
2410 struct qgroup_update *qgroup_update;
2411 int ret = 0;
2412
2413 if (list_empty(&trans->qgroup_ref_list) !=
2414 !trans->delayed_ref_elem.seq) {
2415 /* list without seq or seq without list */
2416 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2417 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2418 trans->delayed_ref_elem.seq);
2419 BUG();
2420 }
2421
2422 if (!trans->delayed_ref_elem.seq)
2423 return 0;
2424
2425 while (!list_empty(&trans->qgroup_ref_list)) {
2426 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2427 struct qgroup_update, list);
2428 list_del(&qgroup_update->list);
2429 if (!ret)
2430 ret = btrfs_qgroup_account_ref(
2431 trans, fs_info, qgroup_update->node,
2432 qgroup_update->extent_op);
2433 kfree(qgroup_update);
2434 }
2435
2436 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2437
2438 return ret;
2439}
2440
Chris Masonc3e69d52009-03-13 10:17:05 -04002441/*
2442 * this starts processing the delayed reference count updates and
2443 * extent insertions we have queued up so far. count can be
2444 * 0, which means to process everything in the tree at the start
2445 * of the run (but not newly added entries), or it can be some target
2446 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002447 *
2448 * Returns 0 on success or if called with an aborted transaction
2449 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002450 */
2451int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2452 struct btrfs_root *root, unsigned long count)
2453{
2454 struct rb_node *node;
2455 struct btrfs_delayed_ref_root *delayed_refs;
2456 struct btrfs_delayed_ref_node *ref;
2457 struct list_head cluster;
2458 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002459 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002460 int run_all = count == (unsigned long)-1;
2461 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002462 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002463
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002464 /* We'll clean this up in btrfs_cleanup_transaction */
2465 if (trans->aborted)
2466 return 0;
2467
Chris Masonc3e69d52009-03-13 10:17:05 -04002468 if (root == root->fs_info->extent_root)
2469 root = root->fs_info->tree_root;
2470
Jan Schmidtedf39272012-06-28 18:04:55 +02002471 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2472
Chris Masonc3e69d52009-03-13 10:17:05 -04002473 delayed_refs = &trans->transaction->delayed_refs;
2474 INIT_LIST_HEAD(&cluster);
2475again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002476 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002477 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002478
Arne Jansen709c0482011-09-12 12:22:57 +02002479#ifdef SCRAMBLE_DELAYED_REFS
2480 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2481#endif
2482
Chris Masonc3e69d52009-03-13 10:17:05 -04002483 if (count == 0) {
2484 count = delayed_refs->num_entries * 2;
2485 run_most = 1;
2486 }
2487 while (1) {
2488 if (!(run_all || run_most) &&
2489 delayed_refs->num_heads_ready < 64)
2490 break;
2491
2492 /*
2493 * go find something we can process in the rbtree. We start at
2494 * the beginning of the tree, and then build a cluster
2495 * of refs to process starting at the first one we are able to
2496 * lock
2497 */
Jan Schmidta1686502011-12-12 16:10:07 +01002498 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002499 ret = btrfs_find_ref_cluster(trans, &cluster,
2500 delayed_refs->run_delayed_start);
2501 if (ret)
2502 break;
2503
2504 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002505 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002506 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002507 spin_unlock(&delayed_refs->lock);
2508 btrfs_abort_transaction(trans, root, ret);
2509 return ret;
2510 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002511
2512 count -= min_t(unsigned long, ret, count);
2513
2514 if (count == 0)
2515 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002516
Arne Jansen1fa11e22012-08-06 14:18:51 -06002517 if (delayed_start >= delayed_refs->run_delayed_start) {
2518 if (loops == 0) {
2519 /*
2520 * btrfs_find_ref_cluster looped. let's do one
2521 * more cycle. if we don't run any delayed ref
2522 * during that cycle (because we can't because
2523 * all of them are blocked), bail out.
2524 */
2525 loops = 1;
2526 } else {
2527 /*
2528 * no runnable refs left, stop trying
2529 */
2530 BUG_ON(run_all);
2531 break;
2532 }
2533 }
2534 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002535 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002536 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002537 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002538 }
2539
Chris Mason56bec292009-03-13 10:10:06 -04002540 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002541 if (!list_empty(&trans->new_bgs)) {
2542 spin_unlock(&delayed_refs->lock);
2543 btrfs_create_pending_block_groups(trans, root);
2544 spin_lock(&delayed_refs->lock);
2545 }
2546
Chris Mason56bec292009-03-13 10:10:06 -04002547 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002548 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002549 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002550 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002551
2552 while (node) {
2553 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2554 rb_node);
2555 if (btrfs_delayed_ref_is_head(ref)) {
2556 struct btrfs_delayed_ref_head *head;
2557
2558 head = btrfs_delayed_node_to_head(ref);
2559 atomic_inc(&ref->refs);
2560
2561 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002562 /*
2563 * Mutex was contended, block until it's
2564 * released and try again
2565 */
Chris Mason56bec292009-03-13 10:10:06 -04002566 mutex_lock(&head->mutex);
2567 mutex_unlock(&head->mutex);
2568
2569 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002570 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002571 goto again;
2572 }
2573 node = rb_next(node);
2574 }
2575 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002576 schedule_timeout(1);
2577 goto again;
2578 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002579out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002580 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002581 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002582 return 0;
2583}
2584
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002585int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2586 struct btrfs_root *root,
2587 u64 bytenr, u64 num_bytes, u64 flags,
2588 int is_data)
2589{
2590 struct btrfs_delayed_extent_op *extent_op;
2591 int ret;
2592
Miao Xie78a61842012-11-21 02:21:28 +00002593 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002594 if (!extent_op)
2595 return -ENOMEM;
2596
2597 extent_op->flags_to_set = flags;
2598 extent_op->update_flags = 1;
2599 extent_op->update_key = 0;
2600 extent_op->is_data = is_data ? 1 : 0;
2601
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002602 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2603 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002604 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002605 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002606 return ret;
2607}
2608
2609static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2610 struct btrfs_root *root,
2611 struct btrfs_path *path,
2612 u64 objectid, u64 offset, u64 bytenr)
2613{
2614 struct btrfs_delayed_ref_head *head;
2615 struct btrfs_delayed_ref_node *ref;
2616 struct btrfs_delayed_data_ref *data_ref;
2617 struct btrfs_delayed_ref_root *delayed_refs;
2618 struct rb_node *node;
2619 int ret = 0;
2620
2621 ret = -ENOENT;
2622 delayed_refs = &trans->transaction->delayed_refs;
2623 spin_lock(&delayed_refs->lock);
2624 head = btrfs_find_delayed_ref_head(trans, bytenr);
2625 if (!head)
2626 goto out;
2627
2628 if (!mutex_trylock(&head->mutex)) {
2629 atomic_inc(&head->node.refs);
2630 spin_unlock(&delayed_refs->lock);
2631
David Sterbab3b4aa72011-04-21 01:20:15 +02002632 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002633
David Sterba8cc33e52011-05-02 15:29:25 +02002634 /*
2635 * Mutex was contended, block until it's released and let
2636 * caller try again
2637 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002638 mutex_lock(&head->mutex);
2639 mutex_unlock(&head->mutex);
2640 btrfs_put_delayed_ref(&head->node);
2641 return -EAGAIN;
2642 }
2643
2644 node = rb_prev(&head->node.rb_node);
2645 if (!node)
2646 goto out_unlock;
2647
2648 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2649
2650 if (ref->bytenr != bytenr)
2651 goto out_unlock;
2652
2653 ret = 1;
2654 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2655 goto out_unlock;
2656
2657 data_ref = btrfs_delayed_node_to_data_ref(ref);
2658
2659 node = rb_prev(node);
2660 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002661 int seq = ref->seq;
2662
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002663 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002664 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002665 goto out_unlock;
2666 }
2667
2668 if (data_ref->root != root->root_key.objectid ||
2669 data_ref->objectid != objectid || data_ref->offset != offset)
2670 goto out_unlock;
2671
2672 ret = 0;
2673out_unlock:
2674 mutex_unlock(&head->mutex);
2675out:
2676 spin_unlock(&delayed_refs->lock);
2677 return ret;
2678}
2679
2680static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2681 struct btrfs_root *root,
2682 struct btrfs_path *path,
2683 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002684{
2685 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002686 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002687 struct btrfs_extent_data_ref *ref;
2688 struct btrfs_extent_inline_ref *iref;
2689 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002690 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002691 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002692 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002693
Chris Masonbe20aa92007-12-17 20:14:01 -05002694 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002695 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002696 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002697
Chris Masonbe20aa92007-12-17 20:14:01 -05002698 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2699 if (ret < 0)
2700 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002701 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002702
2703 ret = -ENOENT;
2704 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002705 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002706
Zheng Yan31840ae2008-09-23 13:14:14 -04002707 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002708 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002709 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002710
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002711 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002712 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002713
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002714 ret = 1;
2715 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2716#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2717 if (item_size < sizeof(*ei)) {
2718 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2719 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002720 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002721#endif
2722 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2723
2724 if (item_size != sizeof(*ei) +
2725 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2726 goto out;
2727
2728 if (btrfs_extent_generation(leaf, ei) <=
2729 btrfs_root_last_snapshot(&root->root_item))
2730 goto out;
2731
2732 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2733 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2734 BTRFS_EXTENT_DATA_REF_KEY)
2735 goto out;
2736
2737 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2738 if (btrfs_extent_refs(leaf, ei) !=
2739 btrfs_extent_data_ref_count(leaf, ref) ||
2740 btrfs_extent_data_ref_root(leaf, ref) !=
2741 root->root_key.objectid ||
2742 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2743 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2744 goto out;
2745
Yan Zhengf321e492008-07-30 09:26:11 -04002746 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002747out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002748 return ret;
2749}
2750
2751int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2752 struct btrfs_root *root,
2753 u64 objectid, u64 offset, u64 bytenr)
2754{
2755 struct btrfs_path *path;
2756 int ret;
2757 int ret2;
2758
2759 path = btrfs_alloc_path();
2760 if (!path)
2761 return -ENOENT;
2762
2763 do {
2764 ret = check_committed_ref(trans, root, path, objectid,
2765 offset, bytenr);
2766 if (ret && ret != -ENOENT)
2767 goto out;
2768
2769 ret2 = check_delayed_ref(trans, root, path, objectid,
2770 offset, bytenr);
2771 } while (ret2 == -EAGAIN);
2772
2773 if (ret2 && ret2 != -ENOENT) {
2774 ret = ret2;
2775 goto out;
2776 }
2777
2778 if (ret != -ENOENT || ret2 != -ENOENT)
2779 ret = 0;
2780out:
Yan Zhengf321e492008-07-30 09:26:11 -04002781 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002782 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2783 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002784 return ret;
2785}
2786
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002787static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002788 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002789 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002790 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002791{
2792 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002793 u64 num_bytes;
2794 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002795 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002796 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002797 struct btrfs_key key;
2798 struct btrfs_file_extent_item *fi;
2799 int i;
2800 int level;
2801 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002802 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002803 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002804
2805 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002806 nritems = btrfs_header_nritems(buf);
2807 level = btrfs_header_level(buf);
2808
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002809 if (!root->ref_cows && level == 0)
2810 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002811
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002812 if (inc)
2813 process_func = btrfs_inc_extent_ref;
2814 else
2815 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002816
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002817 if (full_backref)
2818 parent = buf->start;
2819 else
2820 parent = 0;
2821
Zheng Yan31840ae2008-09-23 13:14:14 -04002822 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002823 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002824 btrfs_item_key_to_cpu(buf, &key, i);
2825 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002826 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002827 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002828 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002829 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002830 BTRFS_FILE_EXTENT_INLINE)
2831 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002832 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2833 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002834 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002835
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002836 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2837 key.offset -= btrfs_file_extent_offset(buf, fi);
2838 ret = process_func(trans, root, bytenr, num_bytes,
2839 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002840 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002841 if (ret)
2842 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002843 } else {
2844 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002845 num_bytes = btrfs_level_size(root, level - 1);
2846 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002847 parent, ref_root, level - 1, 0,
2848 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002849 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002850 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002851 }
2852 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002853 return 0;
2854fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002855 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002856}
2857
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002858int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002859 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002860{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002861 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002862}
Zheng Yan31840ae2008-09-23 13:14:14 -04002863
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002864int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002865 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002866{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002867 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002868}
2869
Chris Mason9078a3e2007-04-26 16:46:15 -04002870static int write_one_cache_group(struct btrfs_trans_handle *trans,
2871 struct btrfs_root *root,
2872 struct btrfs_path *path,
2873 struct btrfs_block_group_cache *cache)
2874{
2875 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002876 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002877 unsigned long bi;
2878 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002879
Chris Mason9078a3e2007-04-26 16:46:15 -04002880 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002881 if (ret < 0)
2882 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002883 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04002884
2885 leaf = path->nodes[0];
2886 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2887 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2888 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002889 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002890fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002891 if (ret) {
2892 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04002893 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002894 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002895 return 0;
2896
2897}
2898
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002899static struct btrfs_block_group_cache *
2900next_block_group(struct btrfs_root *root,
2901 struct btrfs_block_group_cache *cache)
2902{
2903 struct rb_node *node;
2904 spin_lock(&root->fs_info->block_group_cache_lock);
2905 node = rb_next(&cache->cache_node);
2906 btrfs_put_block_group(cache);
2907 if (node) {
2908 cache = rb_entry(node, struct btrfs_block_group_cache,
2909 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002910 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002911 } else
2912 cache = NULL;
2913 spin_unlock(&root->fs_info->block_group_cache_lock);
2914 return cache;
2915}
2916
Josef Bacik0af3d002010-06-21 14:48:16 -04002917static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2918 struct btrfs_trans_handle *trans,
2919 struct btrfs_path *path)
2920{
2921 struct btrfs_root *root = block_group->fs_info->tree_root;
2922 struct inode *inode = NULL;
2923 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002924 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002925 int num_pages = 0;
2926 int retries = 0;
2927 int ret = 0;
2928
2929 /*
2930 * If this block group is smaller than 100 megs don't bother caching the
2931 * block group.
2932 */
2933 if (block_group->key.offset < (100 * 1024 * 1024)) {
2934 spin_lock(&block_group->lock);
2935 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2936 spin_unlock(&block_group->lock);
2937 return 0;
2938 }
2939
2940again:
2941 inode = lookup_free_space_inode(root, block_group, path);
2942 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2943 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002944 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002945 goto out;
2946 }
2947
2948 if (IS_ERR(inode)) {
2949 BUG_ON(retries);
2950 retries++;
2951
2952 if (block_group->ro)
2953 goto out_free;
2954
2955 ret = create_free_space_inode(root, trans, block_group, path);
2956 if (ret)
2957 goto out_free;
2958 goto again;
2959 }
2960
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002961 /* We've already setup this transaction, go ahead and exit */
2962 if (block_group->cache_generation == trans->transid &&
2963 i_size_read(inode)) {
2964 dcs = BTRFS_DC_SETUP;
2965 goto out_put;
2966 }
2967
Josef Bacik0af3d002010-06-21 14:48:16 -04002968 /*
2969 * We want to set the generation to 0, that way if anything goes wrong
2970 * from here on out we know not to trust this cache when we load up next
2971 * time.
2972 */
2973 BTRFS_I(inode)->generation = 0;
2974 ret = btrfs_update_inode(trans, root, inode);
2975 WARN_ON(ret);
2976
2977 if (i_size_read(inode) > 0) {
2978 ret = btrfs_truncate_free_space_cache(root, trans, path,
2979 inode);
2980 if (ret)
2981 goto out_put;
2982 }
2983
2984 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06002985 if (block_group->cached != BTRFS_CACHE_FINISHED ||
2986 !btrfs_test_opt(root, SPACE_CACHE)) {
2987 /*
2988 * don't bother trying to write stuff out _if_
2989 * a) we're not cached,
2990 * b) we're with nospace_cache mount option.
2991 */
Josef Bacik2b209822010-12-03 13:17:53 -05002992 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002993 spin_unlock(&block_group->lock);
2994 goto out_put;
2995 }
2996 spin_unlock(&block_group->lock);
2997
Josef Bacik6fc823b2012-08-06 13:46:38 -06002998 /*
2999 * Try to preallocate enough space based on how big the block group is.
3000 * Keep in mind this has to include any pinned space which could end up
3001 * taking up quite a bit since it's not folded into the other space
3002 * cache.
3003 */
3004 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003005 if (!num_pages)
3006 num_pages = 1;
3007
Josef Bacik0af3d002010-06-21 14:48:16 -04003008 num_pages *= 16;
3009 num_pages *= PAGE_CACHE_SIZE;
3010
3011 ret = btrfs_check_data_free_space(inode, num_pages);
3012 if (ret)
3013 goto out_put;
3014
3015 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3016 num_pages, num_pages,
3017 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003018 if (!ret)
3019 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003020 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003021
Josef Bacik0af3d002010-06-21 14:48:16 -04003022out_put:
3023 iput(inode);
3024out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003025 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003026out:
3027 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003028 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003029 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003030 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003031 spin_unlock(&block_group->lock);
3032
3033 return ret;
3034}
3035
Chris Mason96b51792007-10-15 16:15:19 -04003036int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3037 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003038{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003039 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003040 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003041 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003042 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003043
3044 path = btrfs_alloc_path();
3045 if (!path)
3046 return -ENOMEM;
3047
Josef Bacik0af3d002010-06-21 14:48:16 -04003048again:
3049 while (1) {
3050 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3051 while (cache) {
3052 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3053 break;
3054 cache = next_block_group(root, cache);
3055 }
3056 if (!cache) {
3057 if (last == 0)
3058 break;
3059 last = 0;
3060 continue;
3061 }
3062 err = cache_save_setup(cache, trans, path);
3063 last = cache->key.objectid + cache->key.offset;
3064 btrfs_put_block_group(cache);
3065 }
3066
Chris Masond3977122009-01-05 21:25:51 -05003067 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003068 if (last == 0) {
3069 err = btrfs_run_delayed_refs(trans, root,
3070 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003071 if (err) /* File system offline */
3072 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003073 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003074
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003075 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3076 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003077 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3078 btrfs_put_block_group(cache);
3079 goto again;
3080 }
3081
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003082 if (cache->dirty)
3083 break;
3084 cache = next_block_group(root, cache);
3085 }
3086 if (!cache) {
3087 if (last == 0)
3088 break;
3089 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003090 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003091 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003092
Josef Bacik0cb59c92010-07-02 12:14:14 -04003093 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3094 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003095 cache->dirty = 0;
3096 last = cache->key.objectid + cache->key.offset;
3097
3098 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003099 if (err) /* File system offline */
3100 goto out;
3101
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003102 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003103 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003104
Josef Bacik0cb59c92010-07-02 12:14:14 -04003105 while (1) {
3106 /*
3107 * I don't think this is needed since we're just marking our
3108 * preallocated extent as written, but just in case it can't
3109 * hurt.
3110 */
3111 if (last == 0) {
3112 err = btrfs_run_delayed_refs(trans, root,
3113 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003114 if (err) /* File system offline */
3115 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003116 }
3117
3118 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3119 while (cache) {
3120 /*
3121 * Really this shouldn't happen, but it could if we
3122 * couldn't write the entire preallocated extent and
3123 * splitting the extent resulted in a new block.
3124 */
3125 if (cache->dirty) {
3126 btrfs_put_block_group(cache);
3127 goto again;
3128 }
3129 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3130 break;
3131 cache = next_block_group(root, cache);
3132 }
3133 if (!cache) {
3134 if (last == 0)
3135 break;
3136 last = 0;
3137 continue;
3138 }
3139
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003140 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003141
3142 /*
3143 * If we didn't have an error then the cache state is still
3144 * NEED_WRITE, so we can set it to WRITTEN.
3145 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003146 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003147 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3148 last = cache->key.objectid + cache->key.offset;
3149 btrfs_put_block_group(cache);
3150 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003151out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003152
Chris Mason9078a3e2007-04-26 16:46:15 -04003153 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003154 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003155}
3156
Yan Zhengd2fb3432008-12-11 16:30:39 -05003157int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3158{
3159 struct btrfs_block_group_cache *block_group;
3160 int readonly = 0;
3161
3162 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3163 if (!block_group || block_group->ro)
3164 readonly = 1;
3165 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003166 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003167 return readonly;
3168}
3169
Chris Mason593060d2008-03-25 16:50:33 -04003170static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3171 u64 total_bytes, u64 bytes_used,
3172 struct btrfs_space_info **space_info)
3173{
3174 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003175 int i;
3176 int factor;
3177
3178 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3179 BTRFS_BLOCK_GROUP_RAID10))
3180 factor = 2;
3181 else
3182 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003183
3184 found = __find_space_info(info, flags);
3185 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003186 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003187 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003188 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003189 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003190 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003191 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003192 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003193 *space_info = found;
3194 return 0;
3195 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003196 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003197 if (!found)
3198 return -ENOMEM;
3199
Yan, Zhengb742bb82010-05-16 10:46:24 -04003200 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3201 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003202 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003203 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003204 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003205 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003206 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003207 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003208 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003209 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003210 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003211 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003212 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003213 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003214 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003215 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003216 found->flush = 0;
3217 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003218 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003219 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003220 if (flags & BTRFS_BLOCK_GROUP_DATA)
3221 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003222 return 0;
3223}
3224
Chris Mason8790d502008-04-03 16:29:03 -04003225static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3226{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003227 u64 extra_flags = chunk_to_extended(flags) &
3228 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003229
3230 if (flags & BTRFS_BLOCK_GROUP_DATA)
3231 fs_info->avail_data_alloc_bits |= extra_flags;
3232 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3233 fs_info->avail_metadata_alloc_bits |= extra_flags;
3234 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3235 fs_info->avail_system_alloc_bits |= extra_flags;
Chris Mason8790d502008-04-03 16:29:03 -04003236}
Chris Mason593060d2008-03-25 16:50:33 -04003237
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003238/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003239 * returns target flags in extended format or 0 if restripe for this
3240 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003241 *
3242 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003243 */
3244static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3245{
3246 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3247 u64 target = 0;
3248
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003249 if (!bctl)
3250 return 0;
3251
3252 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3253 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3254 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3255 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3256 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3257 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3258 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3259 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3260 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3261 }
3262
3263 return target;
3264}
3265
3266/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003267 * @flags: available profiles in extended format (see ctree.h)
3268 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003269 * Returns reduced profile in chunk format. If profile changing is in
3270 * progress (either running or paused) picks the target profile (if it's
3271 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003272 */
Yan Zheng2b820322008-11-17 21:11:30 -05003273u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003274{
Chris Masoncd02dca2010-12-13 14:56:23 -05003275 /*
3276 * we add in the count of missing devices because we want
3277 * to make sure that any RAID levels on a degraded FS
3278 * continue to be honored.
3279 */
3280 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3281 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003282 u64 target;
Chris Masona061fc82008-05-07 11:43:44 -04003283
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003284 /*
3285 * see if restripe for this chunk_type is in progress, if so
3286 * try to reduce to the target profile
3287 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003288 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003289 target = get_restripe_target(root->fs_info, flags);
3290 if (target) {
3291 /* pick target profile only if it's already available */
3292 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003293 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003294 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003295 }
3296 }
3297 spin_unlock(&root->fs_info->balance_lock);
3298
Chris Masona061fc82008-05-07 11:43:44 -04003299 if (num_devices == 1)
3300 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3301 if (num_devices < 4)
3302 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3303
Chris Masonec44a352008-04-28 15:29:52 -04003304 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3305 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003306 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003307 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003308 }
Chris Masonec44a352008-04-28 15:29:52 -04003309
3310 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003311 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003312 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003313 }
Chris Masonec44a352008-04-28 15:29:52 -04003314
3315 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3316 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3317 (flags & BTRFS_BLOCK_GROUP_RAID10) |
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003318 (flags & BTRFS_BLOCK_GROUP_DUP))) {
Chris Masonec44a352008-04-28 15:29:52 -04003319 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003320 }
3321
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003322 return extended_to_chunk(flags);
Chris Masonec44a352008-04-28 15:29:52 -04003323}
3324
Yan, Zhengb742bb82010-05-16 10:46:24 -04003325static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003326{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003327 if (flags & BTRFS_BLOCK_GROUP_DATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003328 flags |= root->fs_info->avail_data_alloc_bits;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003329 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003330 flags |= root->fs_info->avail_system_alloc_bits;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003331 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003332 flags |= root->fs_info->avail_metadata_alloc_bits;
3333
Yan, Zhengb742bb82010-05-16 10:46:24 -04003334 return btrfs_reduce_alloc_profile(root, flags);
3335}
Josef Bacik6a632092009-02-20 11:00:09 -05003336
Miao Xie6d07bce2011-01-05 10:07:31 +00003337u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003338{
3339 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003340
Yan, Zhengb742bb82010-05-16 10:46:24 -04003341 if (data)
3342 flags = BTRFS_BLOCK_GROUP_DATA;
3343 else if (root == root->fs_info->chunk_root)
3344 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3345 else
3346 flags = BTRFS_BLOCK_GROUP_METADATA;
3347
3348 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003349}
3350
Josef Bacik6a632092009-02-20 11:00:09 -05003351/*
3352 * This will check the space that the inode allocates from to make sure we have
3353 * enough space for bytes.
3354 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003355int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003356{
3357 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003358 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003359 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003360 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003361 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003362
3363 /* make sure bytes are sectorsize aligned */
3364 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3365
Li Zefan82d59022011-04-20 10:33:24 +08003366 if (root == root->fs_info->tree_root ||
3367 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003368 alloc_chunk = 0;
3369 committed = 1;
3370 }
3371
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003372 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003373 if (!data_sinfo)
3374 goto alloc;
3375
Josef Bacik6a632092009-02-20 11:00:09 -05003376again:
3377 /* make sure we have enough space to handle the data first */
3378 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003379 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3380 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3381 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003382
3383 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003384 struct btrfs_trans_handle *trans;
3385
Josef Bacik6a632092009-02-20 11:00:09 -05003386 /*
3387 * if we don't have enough free bytes in this space then we need
3388 * to alloc a new chunk.
3389 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003390 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003391 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003392
Chris Mason0e4f8f82011-04-15 16:05:44 -04003393 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003394 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003395alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003396 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003397 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003398 if (IS_ERR(trans))
3399 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003400
3401 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003402 alloc_target,
3403 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003404 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003405 if (ret < 0) {
3406 if (ret != -ENOSPC)
3407 return ret;
3408 else
3409 goto commit_trans;
3410 }
Chris Mason33b4d472009-09-22 14:45:50 -04003411
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003412 if (!data_sinfo)
3413 data_sinfo = fs_info->data_sinfo;
3414
Josef Bacik6a632092009-02-20 11:00:09 -05003415 goto again;
3416 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003417
3418 /*
3419 * If we have less pinned bytes than we want to allocate then
3420 * don't bother committing the transaction, it won't help us.
3421 */
3422 if (data_sinfo->bytes_pinned < bytes)
3423 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003424 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003425
3426 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003427commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003428 if (!committed &&
3429 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003430 committed = 1;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003431 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003432 if (IS_ERR(trans))
3433 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003434 ret = btrfs_commit_transaction(trans, root);
3435 if (ret)
3436 return ret;
3437 goto again;
3438 }
3439
Josef Bacik6a632092009-02-20 11:00:09 -05003440 return -ENOSPC;
3441 }
3442 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003443 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003444 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003445 spin_unlock(&data_sinfo->lock);
3446
Josef Bacik9ed74f22009-09-11 16:12:44 -04003447 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003448}
3449
3450/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003451 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003452 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003453void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003454{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003455 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003456 struct btrfs_space_info *data_sinfo;
3457
3458 /* make sure bytes are sectorsize aligned */
3459 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3460
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003461 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003462 spin_lock(&data_sinfo->lock);
3463 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003464 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003465 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003466 spin_unlock(&data_sinfo->lock);
3467}
3468
Josef Bacik97e728d2009-04-21 17:40:57 -04003469static void force_metadata_allocation(struct btrfs_fs_info *info)
3470{
3471 struct list_head *head = &info->space_info;
3472 struct btrfs_space_info *found;
3473
3474 rcu_read_lock();
3475 list_for_each_entry_rcu(found, head, list) {
3476 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003477 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003478 }
3479 rcu_read_unlock();
3480}
3481
Chris Masone5bc2452010-10-26 13:37:56 -04003482static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003483 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003484{
Josef Bacikfb25e912011-07-26 17:00:46 -04003485 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003486 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003487 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003488 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003489
Chris Mason0e4f8f82011-04-15 16:05:44 -04003490 if (force == CHUNK_ALLOC_FORCE)
3491 return 1;
3492
3493 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003494 * We need to take into account the global rsv because for all intents
3495 * and purposes it's used space. Don't worry about locking the
3496 * global_rsv, it doesn't change except when the transaction commits.
3497 */
Josef Bacik54338b52012-08-14 16:20:52 -04003498 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3499 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003500
3501 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003502 * in limited mode, we want to have some free space up to
3503 * about 1% of the FS size.
3504 */
3505 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003506 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003507 thresh = max_t(u64, 64 * 1024 * 1024,
3508 div_factor_fine(thresh, 1));
3509
3510 if (num_bytes - num_allocated < thresh)
3511 return 1;
3512 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003513
Josef Bacik698d0082012-09-12 14:08:47 -04003514 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003515 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003516 return 1;
3517}
3518
Liu Bo15d1ff82012-03-29 09:57:44 -04003519static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3520{
3521 u64 num_dev;
3522
3523 if (type & BTRFS_BLOCK_GROUP_RAID10 ||
3524 type & BTRFS_BLOCK_GROUP_RAID0)
3525 num_dev = root->fs_info->fs_devices->rw_devices;
3526 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3527 num_dev = 2;
3528 else
3529 num_dev = 1; /* DUP or single */
3530
3531 /* metadata for updaing devices and chunk tree */
3532 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3533}
3534
3535static void check_system_chunk(struct btrfs_trans_handle *trans,
3536 struct btrfs_root *root, u64 type)
3537{
3538 struct btrfs_space_info *info;
3539 u64 left;
3540 u64 thresh;
3541
3542 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3543 spin_lock(&info->lock);
3544 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3545 info->bytes_reserved - info->bytes_readonly;
3546 spin_unlock(&info->lock);
3547
3548 thresh = get_system_chunk_thresh(root, type);
3549 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3550 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3551 left, thresh, type);
3552 dump_space_info(info, 0, 0);
3553 }
3554
3555 if (left < thresh) {
3556 u64 flags;
3557
3558 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3559 btrfs_alloc_chunk(trans, root, flags);
3560 }
3561}
3562
Chris Mason6324fbf2008-03-24 15:01:59 -04003563static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003564 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003565{
3566 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003567 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003568 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003569 int ret = 0;
3570
Josef Bacikc6b305a2012-12-18 09:16:16 -05003571 /* Don't re-enter if we're already allocating a chunk */
3572 if (trans->allocating_chunk)
3573 return -ENOSPC;
3574
Chris Mason6324fbf2008-03-24 15:01:59 -04003575 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003576 if (!space_info) {
3577 ret = update_space_info(extent_root->fs_info, flags,
3578 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003579 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003580 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003581 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003582
Josef Bacik6d741192011-04-11 20:20:11 -04003583again:
Josef Bacik25179202008-10-29 14:49:05 -04003584 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003585 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003586 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003587 if (space_info->full) {
3588 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003589 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003590 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003591
Josef Bacik698d0082012-09-12 14:08:47 -04003592 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003593 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003594 return 0;
3595 } else if (space_info->chunk_alloc) {
3596 wait_for_alloc = 1;
3597 } else {
3598 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003599 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003600
Josef Bacik25179202008-10-29 14:49:05 -04003601 spin_unlock(&space_info->lock);
3602
Josef Bacik6d741192011-04-11 20:20:11 -04003603 mutex_lock(&fs_info->chunk_mutex);
3604
3605 /*
3606 * The chunk_mutex is held throughout the entirety of a chunk
3607 * allocation, so once we've acquired the chunk_mutex we know that the
3608 * other guy is done and we need to recheck and see if we should
3609 * allocate.
3610 */
3611 if (wait_for_alloc) {
3612 mutex_unlock(&fs_info->chunk_mutex);
3613 wait_for_alloc = 0;
3614 goto again;
3615 }
3616
Josef Bacikc6b305a2012-12-18 09:16:16 -05003617 trans->allocating_chunk = true;
3618
Josef Bacik97e728d2009-04-21 17:40:57 -04003619 /*
Josef Bacik67377732010-09-16 16:19:09 -04003620 * If we have mixed data/metadata chunks we want to make sure we keep
3621 * allocating mixed chunks instead of individual chunks.
3622 */
3623 if (btrfs_mixed_space_info(space_info))
3624 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3625
3626 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003627 * if we're doing a data chunk, go ahead and make sure that
3628 * we keep a reasonable number of metadata chunks allocated in the
3629 * FS as well.
3630 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003631 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003632 fs_info->data_chunk_allocations++;
3633 if (!(fs_info->data_chunk_allocations %
3634 fs_info->metadata_ratio))
3635 force_metadata_allocation(fs_info);
3636 }
3637
Liu Bo15d1ff82012-03-29 09:57:44 -04003638 /*
3639 * Check if we have enough space in SYSTEM chunk because we may need
3640 * to update devices.
3641 */
3642 check_system_chunk(trans, extent_root, flags);
3643
Yan Zheng2b820322008-11-17 21:11:30 -05003644 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003645 trans->allocating_chunk = false;
Mark Fasheh92b8e892011-07-12 10:57:59 -07003646 if (ret < 0 && ret != -ENOSPC)
3647 goto out;
3648
Josef Bacik9ed74f22009-09-11 16:12:44 -04003649 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003650 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003651 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003652 else
3653 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003654
Chris Mason0e4f8f82011-04-15 16:05:44 -04003655 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003656 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003657 spin_unlock(&space_info->lock);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003658out:
Dan Carpentera25c75d2012-04-18 09:59:29 +03003659 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003660 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003661}
3662
Josef Bacika80c8dc2012-09-06 16:59:33 -04003663static int can_overcommit(struct btrfs_root *root,
3664 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003665 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003666{
3667 u64 profile = btrfs_get_alloc_profile(root, 0);
3668 u64 avail;
3669 u64 used;
3670
3671 used = space_info->bytes_used + space_info->bytes_reserved +
3672 space_info->bytes_pinned + space_info->bytes_readonly +
3673 space_info->bytes_may_use;
3674
3675 spin_lock(&root->fs_info->free_chunk_lock);
3676 avail = root->fs_info->free_chunk_space;
3677 spin_unlock(&root->fs_info->free_chunk_lock);
3678
3679 /*
3680 * If we have dup, raid1 or raid10 then only half of the free
3681 * space is actually useable.
3682 */
3683 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3684 BTRFS_BLOCK_GROUP_RAID1 |
3685 BTRFS_BLOCK_GROUP_RAID10))
3686 avail >>= 1;
3687
3688 /*
Miao Xie561c2942012-10-16 11:32:18 +00003689 * If we aren't flushing all things, let us overcommit up to
3690 * 1/2th of the space. If we can flush, don't let us overcommit
3691 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003692 */
Miao Xie08e007d2012-10-16 11:33:38 +00003693 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003694 avail >>= 3;
3695 else
3696 avail >>= 1;
3697
3698 if (used + bytes < space_info->total_bytes + avail)
3699 return 1;
3700 return 0;
3701}
3702
Miao Xieda633a42012-12-20 11:19:09 +00003703static inline int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
3704 unsigned long nr_pages,
3705 enum wb_reason reason)
Josef Bacik1135d6d2012-12-14 13:46:43 -05003706{
Miao Xieda633a42012-12-20 11:19:09 +00003707 /* the flusher is dealing with the dirty inodes now. */
3708 if (writeback_in_progress(sb->s_bdi))
3709 return 1;
3710
3711 if (down_read_trylock(&sb->s_umount)) {
Josef Bacik1135d6d2012-12-14 13:46:43 -05003712 writeback_inodes_sb_nr(sb, nr_pages, reason);
3713 up_read(&sb->s_umount);
3714 return 1;
3715 }
3716
3717 return 0;
3718}
3719
Miao Xieda633a42012-12-20 11:19:09 +00003720void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3721 unsigned long nr_pages)
3722{
3723 struct super_block *sb = root->fs_info->sb;
3724 int started;
3725
3726 /* If we can not start writeback, just sync all the delalloc file. */
3727 started = writeback_inodes_sb_nr_if_idle_safe(sb, nr_pages,
3728 WB_REASON_FS_FREE_SPACE);
3729 if (!started) {
3730 /*
3731 * We needn't worry the filesystem going from r/w to r/o though
3732 * we don't acquire ->s_umount mutex, because the filesystem
3733 * should guarantee the delalloc inodes list be empty after
3734 * the filesystem is readonly(all dirty pages are written to
3735 * the disk).
3736 */
3737 btrfs_start_delalloc_inodes(root, 0);
3738 btrfs_wait_ordered_extents(root, 0);
3739 }
3740}
3741
Yan, Zheng5da9d012010-05-16 10:46:25 -04003742/*
3743 * shrink metadata reservation for delalloc
3744 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003745static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3746 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003747{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003748 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003749 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003750 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003751 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003752 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003753 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003754 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003755 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003756 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003757
Josef Bacik663350a2011-11-03 22:54:25 -04003758 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003759 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003760 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003761
3762 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003763 delalloc_bytes = percpu_counter_sum_positive(
3764 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003765 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003766 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003767 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003768 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003769 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003770 }
3771
Josef Bacikf4c738c2012-07-02 17:10:51 -04003772 while (delalloc_bytes && loops < 3) {
3773 max_reclaim = min(delalloc_bytes, to_reclaim);
3774 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003775 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003776 /*
3777 * We need to wait for the async pages to actually start before
3778 * we do anything.
3779 */
3780 wait_event(root->fs_info->async_submit_wait,
3781 !atomic_read(&root->fs_info->async_delalloc_pages));
3782
Miao Xie08e007d2012-10-16 11:33:38 +00003783 if (!trans)
3784 flush = BTRFS_RESERVE_FLUSH_ALL;
3785 else
3786 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003787 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003788 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003789 spin_unlock(&space_info->lock);
3790 break;
3791 }
Josef Bacik0019f102010-10-15 15:18:40 -04003792 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003793
Chris Mason36e39c42011-03-12 07:08:42 -05003794 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003795 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003796 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003797 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003798 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003799 if (time_left)
3800 break;
3801 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003802 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003803 delalloc_bytes = percpu_counter_sum_positive(
3804 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003805 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003806}
3807
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003808/**
Josef Bacik663350a2011-11-03 22:54:25 -04003809 * maybe_commit_transaction - possibly commit the transaction if its ok to
3810 * @root - the root we're allocating for
3811 * @bytes - the number of bytes we want to reserve
3812 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003813 *
Josef Bacik663350a2011-11-03 22:54:25 -04003814 * This will check to make sure that committing the transaction will actually
3815 * get us somewhere and then commit the transaction if it does. Otherwise it
3816 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003817 */
Josef Bacik663350a2011-11-03 22:54:25 -04003818static int may_commit_transaction(struct btrfs_root *root,
3819 struct btrfs_space_info *space_info,
3820 u64 bytes, int force)
3821{
3822 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3823 struct btrfs_trans_handle *trans;
3824
3825 trans = (struct btrfs_trans_handle *)current->journal_info;
3826 if (trans)
3827 return -EAGAIN;
3828
3829 if (force)
3830 goto commit;
3831
3832 /* See if there is enough pinned space to make this reservation */
3833 spin_lock(&space_info->lock);
3834 if (space_info->bytes_pinned >= bytes) {
3835 spin_unlock(&space_info->lock);
3836 goto commit;
3837 }
3838 spin_unlock(&space_info->lock);
3839
3840 /*
3841 * See if there is some space in the delayed insertion reservation for
3842 * this reservation.
3843 */
3844 if (space_info != delayed_rsv->space_info)
3845 return -ENOSPC;
3846
Liu Bod9b02182012-02-16 18:34:39 +08003847 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003848 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003849 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04003850 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003851 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003852 return -ENOSPC;
3853 }
3854 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003855 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003856
3857commit:
3858 trans = btrfs_join_transaction(root);
3859 if (IS_ERR(trans))
3860 return -ENOSPC;
3861
3862 return btrfs_commit_transaction(trans, root);
3863}
3864
Josef Bacik96c3f432012-06-21 14:05:49 -04003865enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04003866 FLUSH_DELAYED_ITEMS_NR = 1,
3867 FLUSH_DELAYED_ITEMS = 2,
3868 FLUSH_DELALLOC = 3,
3869 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04003870 ALLOC_CHUNK = 5,
3871 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04003872};
3873
3874static int flush_space(struct btrfs_root *root,
3875 struct btrfs_space_info *space_info, u64 num_bytes,
3876 u64 orig_bytes, int state)
3877{
3878 struct btrfs_trans_handle *trans;
3879 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003880 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04003881
3882 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04003883 case FLUSH_DELAYED_ITEMS_NR:
3884 case FLUSH_DELAYED_ITEMS:
3885 if (state == FLUSH_DELAYED_ITEMS_NR) {
3886 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3887
3888 nr = (int)div64_u64(num_bytes, bytes);
3889 if (!nr)
3890 nr = 1;
3891 nr *= 2;
3892 } else {
3893 nr = -1;
3894 }
3895 trans = btrfs_join_transaction(root);
3896 if (IS_ERR(trans)) {
3897 ret = PTR_ERR(trans);
3898 break;
3899 }
3900 ret = btrfs_run_delayed_items_nr(trans, root, nr);
3901 btrfs_end_transaction(trans, root);
3902 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003903 case FLUSH_DELALLOC:
3904 case FLUSH_DELALLOC_WAIT:
3905 shrink_delalloc(root, num_bytes, orig_bytes,
3906 state == FLUSH_DELALLOC_WAIT);
3907 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04003908 case ALLOC_CHUNK:
3909 trans = btrfs_join_transaction(root);
3910 if (IS_ERR(trans)) {
3911 ret = PTR_ERR(trans);
3912 break;
3913 }
3914 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04003915 btrfs_get_alloc_profile(root, 0),
3916 CHUNK_ALLOC_NO_FORCE);
3917 btrfs_end_transaction(trans, root);
3918 if (ret == -ENOSPC)
3919 ret = 0;
3920 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04003921 case COMMIT_TRANS:
3922 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3923 break;
3924 default:
3925 ret = -ENOSPC;
3926 break;
3927 }
3928
3929 return ret;
3930}
Josef Bacik663350a2011-11-03 22:54:25 -04003931/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003932 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3933 * @root - the root we're allocating for
3934 * @block_rsv - the block_rsv we're allocating for
3935 * @orig_bytes - the number of bytes we want
3936 * @flush - wether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003937 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003938 * This will reserve orgi_bytes number of bytes from the space info associated
3939 * with the block_rsv. If there is not enough space it will make an attempt to
3940 * flush out space to make room. It will do this by flushing delalloc if
3941 * possible or committing the transaction. If flush is 0 then no attempts to
3942 * regain reservations will be made and this will fail if there is not enough
3943 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003944 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003945static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003946 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00003947 u64 orig_bytes,
3948 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003949{
3950 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04003951 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003952 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003953 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003954 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003955 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003956
3957again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003958 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003959 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003960 /*
Miao Xie08e007d2012-10-16 11:33:38 +00003961 * We only want to wait if somebody other than us is flushing and we
3962 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003963 */
Miao Xie08e007d2012-10-16 11:33:38 +00003964 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
3965 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003966 spin_unlock(&space_info->lock);
3967 /*
3968 * If we have a trans handle we can't wait because the flusher
3969 * may have to commit the transaction, which would mean we would
3970 * deadlock since we are waiting for the flusher to finish, but
3971 * hold the current transaction open.
3972 */
Josef Bacik663350a2011-11-03 22:54:25 -04003973 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003974 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02003975 ret = wait_event_killable(space_info->wait, !space_info->flush);
3976 /* Must have been killed, return */
3977 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003978 return -EINTR;
3979
3980 spin_lock(&space_info->lock);
3981 }
3982
3983 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04003984 used = space_info->bytes_used + space_info->bytes_reserved +
3985 space_info->bytes_pinned + space_info->bytes_readonly +
3986 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003987
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003988 /*
3989 * The idea here is that we've not already over-reserved the block group
3990 * then we can go ahead and save our reservation first and then start
3991 * flushing if we need to. Otherwise if we've already overcommitted
3992 * lets start flushing stuff first and then come back and try to make
3993 * our reservation.
3994 */
Josef Bacik2bf64752011-09-26 17:12:22 -04003995 if (used <= space_info->total_bytes) {
3996 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04003997 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003998 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04003999 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004000 ret = 0;
4001 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004002 /*
4003 * Ok set num_bytes to orig_bytes since we aren't
4004 * overocmmitted, this way we only try and reclaim what
4005 * we need.
4006 */
4007 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004008 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004009 } else {
4010 /*
4011 * Ok we're over committed, set num_bytes to the overcommitted
4012 * amount plus the amount of bytes that we need for this
4013 * reservation.
4014 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004015 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004016 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004017 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004018
Josef Bacik44734ed2012-09-28 16:04:19 -04004019 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4020 space_info->bytes_may_use += orig_bytes;
4021 trace_btrfs_space_reservation(root->fs_info, "space_info",
4022 space_info->flags, orig_bytes,
4023 1);
4024 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004025 }
4026
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004027 /*
4028 * Couldn't make our reservation, save our place so while we're trying
4029 * to reclaim space we can actually use it instead of somebody else
4030 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004031 *
4032 * We make the other tasks wait for the flush only when we can flush
4033 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004034 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004035 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004036 flushing = true;
4037 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004038 }
4039
Yan, Zhengf0486c62010-05-16 10:46:25 -04004040 spin_unlock(&space_info->lock);
4041
Miao Xie08e007d2012-10-16 11:33:38 +00004042 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004043 goto out;
4044
Josef Bacik96c3f432012-06-21 14:05:49 -04004045 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4046 flush_state);
4047 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004048
4049 /*
4050 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4051 * would happen. So skip delalloc flush.
4052 */
4053 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4054 (flush_state == FLUSH_DELALLOC ||
4055 flush_state == FLUSH_DELALLOC_WAIT))
4056 flush_state = ALLOC_CHUNK;
4057
Josef Bacik96c3f432012-06-21 14:05:49 -04004058 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004059 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004060 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4061 flush_state < COMMIT_TRANS)
4062 goto again;
4063 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4064 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004065 goto again;
4066
4067out:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004068 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004069 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004070 space_info->flush = 0;
4071 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004072 spin_unlock(&space_info->lock);
4073 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004074 return ret;
4075}
4076
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004077static struct btrfs_block_rsv *get_block_rsv(
4078 const struct btrfs_trans_handle *trans,
4079 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004080{
Josef Bacik4c13d752011-08-30 11:31:29 -04004081 struct btrfs_block_rsv *block_rsv = NULL;
4082
Josef Bacik0e721102012-06-26 16:13:18 -04004083 if (root->ref_cows)
4084 block_rsv = trans->block_rsv;
4085
4086 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004087 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004088
4089 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004090 block_rsv = root->block_rsv;
4091
4092 if (!block_rsv)
4093 block_rsv = &root->fs_info->empty_block_rsv;
4094
4095 return block_rsv;
4096}
4097
4098static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4099 u64 num_bytes)
4100{
4101 int ret = -ENOSPC;
4102 spin_lock(&block_rsv->lock);
4103 if (block_rsv->reserved >= num_bytes) {
4104 block_rsv->reserved -= num_bytes;
4105 if (block_rsv->reserved < block_rsv->size)
4106 block_rsv->full = 0;
4107 ret = 0;
4108 }
4109 spin_unlock(&block_rsv->lock);
4110 return ret;
4111}
4112
4113static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4114 u64 num_bytes, int update_size)
4115{
4116 spin_lock(&block_rsv->lock);
4117 block_rsv->reserved += num_bytes;
4118 if (update_size)
4119 block_rsv->size += num_bytes;
4120 else if (block_rsv->reserved >= block_rsv->size)
4121 block_rsv->full = 1;
4122 spin_unlock(&block_rsv->lock);
4123}
4124
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004125static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4126 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004127 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004128{
4129 struct btrfs_space_info *space_info = block_rsv->space_info;
4130
4131 spin_lock(&block_rsv->lock);
4132 if (num_bytes == (u64)-1)
4133 num_bytes = block_rsv->size;
4134 block_rsv->size -= num_bytes;
4135 if (block_rsv->reserved >= block_rsv->size) {
4136 num_bytes = block_rsv->reserved - block_rsv->size;
4137 block_rsv->reserved = block_rsv->size;
4138 block_rsv->full = 1;
4139 } else {
4140 num_bytes = 0;
4141 }
4142 spin_unlock(&block_rsv->lock);
4143
4144 if (num_bytes > 0) {
4145 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004146 spin_lock(&dest->lock);
4147 if (!dest->full) {
4148 u64 bytes_to_add;
4149
4150 bytes_to_add = dest->size - dest->reserved;
4151 bytes_to_add = min(num_bytes, bytes_to_add);
4152 dest->reserved += bytes_to_add;
4153 if (dest->reserved >= dest->size)
4154 dest->full = 1;
4155 num_bytes -= bytes_to_add;
4156 }
4157 spin_unlock(&dest->lock);
4158 }
4159 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004160 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004161 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004162 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004163 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004164 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004165 spin_unlock(&space_info->lock);
4166 }
4167 }
4168}
4169
4170static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4171 struct btrfs_block_rsv *dst, u64 num_bytes)
4172{
4173 int ret;
4174
4175 ret = block_rsv_use_bytes(src, num_bytes);
4176 if (ret)
4177 return ret;
4178
4179 block_rsv_add_bytes(dst, num_bytes, 1);
4180 return 0;
4181}
4182
Miao Xie66d8f3d2012-09-06 04:02:28 -06004183void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004184{
4185 memset(rsv, 0, sizeof(*rsv));
4186 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004187 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004188}
4189
Miao Xie66d8f3d2012-09-06 04:02:28 -06004190struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4191 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004192{
4193 struct btrfs_block_rsv *block_rsv;
4194 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004195
4196 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4197 if (!block_rsv)
4198 return NULL;
4199
Miao Xie66d8f3d2012-09-06 04:02:28 -06004200 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004201 block_rsv->space_info = __find_space_info(fs_info,
4202 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004203 return block_rsv;
4204}
4205
4206void btrfs_free_block_rsv(struct btrfs_root *root,
4207 struct btrfs_block_rsv *rsv)
4208{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004209 if (!rsv)
4210 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004211 btrfs_block_rsv_release(root, rsv, (u64)-1);
4212 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004213}
4214
Miao Xie08e007d2012-10-16 11:33:38 +00004215int btrfs_block_rsv_add(struct btrfs_root *root,
4216 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4217 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004218{
4219 int ret;
4220
4221 if (num_bytes == 0)
4222 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004223
Miao Xie61b520a2011-11-10 20:45:05 -05004224 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004225 if (!ret) {
4226 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4227 return 0;
4228 }
4229
Yan, Zhengf0486c62010-05-16 10:46:25 -04004230 return ret;
4231}
4232
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004233int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004234 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004235{
4236 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004237 int ret = -ENOSPC;
4238
4239 if (!block_rsv)
4240 return 0;
4241
4242 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004243 num_bytes = div_factor(block_rsv->size, min_factor);
4244 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004245 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004246 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004247
Josef Bacik36ba0222011-10-18 12:15:48 -04004248 return ret;
4249}
4250
Miao Xie08e007d2012-10-16 11:33:38 +00004251int btrfs_block_rsv_refill(struct btrfs_root *root,
4252 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4253 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004254{
4255 u64 num_bytes = 0;
4256 int ret = -ENOSPC;
4257
4258 if (!block_rsv)
4259 return 0;
4260
4261 spin_lock(&block_rsv->lock);
4262 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004263 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004264 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004265 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004266 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004267 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004268
Yan, Zhengf0486c62010-05-16 10:46:25 -04004269 if (!ret)
4270 return 0;
4271
Miao Xieaa38a712011-11-18 17:43:00 +08004272 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004273 if (!ret) {
4274 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004275 return 0;
4276 }
4277
Josef Bacik13553e52011-08-08 13:33:21 -04004278 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004279}
4280
4281int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4282 struct btrfs_block_rsv *dst_rsv,
4283 u64 num_bytes)
4284{
4285 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4286}
4287
4288void btrfs_block_rsv_release(struct btrfs_root *root,
4289 struct btrfs_block_rsv *block_rsv,
4290 u64 num_bytes)
4291{
4292 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4293 if (global_rsv->full || global_rsv == block_rsv ||
4294 block_rsv->space_info != global_rsv->space_info)
4295 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004296 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4297 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004298}
4299
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004300/*
4301 * helper to calculate size of global block reservation.
4302 * the desired value is sum of space used by extent tree,
4303 * checksum tree and root tree
4304 */
4305static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4306{
4307 struct btrfs_space_info *sinfo;
4308 u64 num_bytes;
4309 u64 meta_used;
4310 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004311 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004312
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004313 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4314 spin_lock(&sinfo->lock);
4315 data_used = sinfo->bytes_used;
4316 spin_unlock(&sinfo->lock);
4317
4318 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4319 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004320 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4321 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004322 meta_used = sinfo->bytes_used;
4323 spin_unlock(&sinfo->lock);
4324
4325 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4326 csum_size * 2;
4327 num_bytes += div64_u64(data_used + meta_used, 50);
4328
4329 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004330 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004331
4332 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4333}
4334
4335static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4336{
4337 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4338 struct btrfs_space_info *sinfo = block_rsv->space_info;
4339 u64 num_bytes;
4340
4341 num_bytes = calc_global_metadata_size(fs_info);
4342
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004343 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004344 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004345
4346 block_rsv->size = num_bytes;
4347
4348 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004349 sinfo->bytes_reserved + sinfo->bytes_readonly +
4350 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004351
4352 if (sinfo->total_bytes > num_bytes) {
4353 num_bytes = sinfo->total_bytes - num_bytes;
4354 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004355 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004356 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004357 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004358 }
4359
4360 if (block_rsv->reserved >= block_rsv->size) {
4361 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004362 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004363 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004364 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004365 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004366 block_rsv->reserved = block_rsv->size;
4367 block_rsv->full = 1;
4368 }
David Sterba182608c2011-05-05 13:13:16 +02004369
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004370 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004371 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004372}
4373
Yan, Zhengf0486c62010-05-16 10:46:25 -04004374static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4375{
4376 struct btrfs_space_info *space_info;
4377
4378 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4379 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004380
4381 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004382 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004383 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004384 fs_info->trans_block_rsv.space_info = space_info;
4385 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004386 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004387
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004388 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4389 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4390 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4391 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004392 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004393
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004394 update_global_block_rsv(fs_info);
4395}
4396
4397static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4398{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004399 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4400 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004401 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4402 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4403 WARN_ON(fs_info->trans_block_rsv.size > 0);
4404 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4405 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4406 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004407 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4408 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004409}
4410
Yan, Zhenga22285a2010-05-16 10:48:46 -04004411void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4412 struct btrfs_root *root)
4413{
Josef Bacik0e721102012-06-26 16:13:18 -04004414 if (!trans->block_rsv)
4415 return;
4416
Yan, Zhenga22285a2010-05-16 10:48:46 -04004417 if (!trans->bytes_reserved)
4418 return;
4419
Chris Masone77266e2012-02-24 10:39:05 -05004420 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004421 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004422 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004423 trans->bytes_reserved = 0;
4424}
4425
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004426/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004427int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4428 struct inode *inode)
4429{
4430 struct btrfs_root *root = BTRFS_I(inode)->root;
4431 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4432 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4433
4434 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004435 * We need to hold space in order to delete our orphan item once we've
4436 * added it, so this takes the reservation so we can release it later
4437 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004438 */
Chris Masonff5714c2011-05-28 07:00:39 -04004439 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004440 trace_btrfs_space_reservation(root->fs_info, "orphan",
4441 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004442 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4443}
4444
4445void btrfs_orphan_release_metadata(struct inode *inode)
4446{
4447 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004448 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004449 trace_btrfs_space_reservation(root->fs_info, "orphan",
4450 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004451 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4452}
4453
Yan, Zhenga22285a2010-05-16 10:48:46 -04004454int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4455 struct btrfs_pending_snapshot *pending)
4456{
4457 struct btrfs_root *root = pending->root;
4458 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4459 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4460 /*
Miao Xie48c03c42012-09-06 04:03:56 -06004461 * two for root back/forward refs, two for directory entries,
4462 * one for root of the snapshot and one for parent inode.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004463 */
Miao Xie48c03c42012-09-06 04:03:56 -06004464 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004465 dst_rsv->space_info = src_rsv->space_info;
4466 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4467}
4468
Josef Bacik7709cde2011-08-04 10:25:02 -04004469/**
4470 * drop_outstanding_extent - drop an outstanding extent
4471 * @inode: the inode we're dropping the extent for
4472 *
4473 * This is called when we are freeing up an outstanding extent, either called
4474 * after an error or after an extent is written. This will return the number of
4475 * reserved extents that need to be freed. This must be called with
4476 * BTRFS_I(inode)->lock held.
4477 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004478static unsigned drop_outstanding_extent(struct inode *inode)
4479{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004480 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004481 unsigned dropped_extents = 0;
4482
Josef Bacik9e0baf62011-07-15 15:16:44 +00004483 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4484 BTRFS_I(inode)->outstanding_extents--;
4485
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004486 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004487 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4488 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004489 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004490
Josef Bacik9e0baf62011-07-15 15:16:44 +00004491 /*
4492 * If we have more or the same amount of outsanding extents than we have
4493 * reserved then we need to leave the reserved extents count alone.
4494 */
4495 if (BTRFS_I(inode)->outstanding_extents >=
4496 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004497 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004498
4499 dropped_extents = BTRFS_I(inode)->reserved_extents -
4500 BTRFS_I(inode)->outstanding_extents;
4501 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004502 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004503}
4504
Josef Bacik7709cde2011-08-04 10:25:02 -04004505/**
4506 * calc_csum_metadata_size - return the amount of metada space that must be
4507 * reserved/free'd for the given bytes.
4508 * @inode: the inode we're manipulating
4509 * @num_bytes: the number of bytes in question
4510 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4511 *
4512 * This adjusts the number of csum_bytes in the inode and then returns the
4513 * correct amount of metadata that must either be reserved or freed. We
4514 * calculate how many checksums we can fit into one leaf and then divide the
4515 * number of bytes that will need to be checksumed by this value to figure out
4516 * how many checksums will be required. If we are adding bytes then the number
4517 * may go up and we will return the number of additional bytes that must be
4518 * reserved. If it is going down we will return the number of bytes that must
4519 * be freed.
4520 *
4521 * This must be called with BTRFS_I(inode)->lock held.
4522 */
4523static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4524 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004525{
Josef Bacik7709cde2011-08-04 10:25:02 -04004526 struct btrfs_root *root = BTRFS_I(inode)->root;
4527 u64 csum_size;
4528 int num_csums_per_leaf;
4529 int num_csums;
4530 int old_csums;
4531
4532 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4533 BTRFS_I(inode)->csum_bytes == 0)
4534 return 0;
4535
4536 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4537 if (reserve)
4538 BTRFS_I(inode)->csum_bytes += num_bytes;
4539 else
4540 BTRFS_I(inode)->csum_bytes -= num_bytes;
4541 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4542 num_csums_per_leaf = (int)div64_u64(csum_size,
4543 sizeof(struct btrfs_csum_item) +
4544 sizeof(struct btrfs_disk_key));
4545 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4546 num_csums = num_csums + num_csums_per_leaf - 1;
4547 num_csums = num_csums / num_csums_per_leaf;
4548
4549 old_csums = old_csums + num_csums_per_leaf - 1;
4550 old_csums = old_csums / num_csums_per_leaf;
4551
4552 /* No change, no need to reserve more */
4553 if (old_csums == num_csums)
4554 return 0;
4555
4556 if (reserve)
4557 return btrfs_calc_trans_metadata_size(root,
4558 num_csums - old_csums);
4559
4560 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004561}
4562
4563int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4564{
4565 struct btrfs_root *root = BTRFS_I(inode)->root;
4566 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004567 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004568 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004569 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004570 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004571 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004572 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004573 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004574
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004575 /* If we are a free space inode we need to not flush since we will be in
4576 * the middle of a transaction commit. We also don't need the delalloc
4577 * mutex since we won't race with anybody. We need this mostly to make
4578 * lockdep shut its filthy mouth.
4579 */
4580 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004581 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004582 delalloc_lock = false;
4583 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004584
Miao Xie08e007d2012-10-16 11:33:38 +00004585 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4586 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004587 schedule_timeout(1);
4588
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004589 if (delalloc_lock)
4590 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4591
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004592 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004593
Josef Bacik9e0baf62011-07-15 15:16:44 +00004594 spin_lock(&BTRFS_I(inode)->lock);
4595 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004596
Josef Bacik9e0baf62011-07-15 15:16:44 +00004597 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004598 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004599 nr_extents = BTRFS_I(inode)->outstanding_extents -
4600 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004601
4602 /*
4603 * Add an item to reserve for updating the inode when we complete the
4604 * delalloc io.
4605 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004606 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4607 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004608 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004609 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004610 }
4611
4612 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004613 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004614 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004615 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004616
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004617 if (root->fs_info->quota_enabled)
Arne Jansenc5567232011-09-14 15:44:05 +02004618 ret = btrfs_qgroup_reserve(root, num_bytes +
4619 nr_extents * root->leafsize);
Arne Jansenc5567232011-09-14 15:44:05 +02004620
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004621 /*
4622 * ret != 0 here means the qgroup reservation failed, we go straight to
4623 * the shared error handling then.
4624 */
4625 if (ret == 0)
4626 ret = reserve_metadata_bytes(root, block_rsv,
4627 to_reserve, flush);
4628
Josef Bacik9e0baf62011-07-15 15:16:44 +00004629 if (ret) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004630 u64 to_free = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004631 unsigned dropped;
Josef Bacik7ed49f12011-08-19 15:45:52 -04004632
Josef Bacik7709cde2011-08-04 10:25:02 -04004633 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004634 dropped = drop_outstanding_extent(inode);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004635 /*
Josef Bacik660d3f62011-12-09 11:18:51 -05004636 * If the inodes csum_bytes is the same as the original
4637 * csum_bytes then we know we haven't raced with any free()ers
4638 * so we can just reduce our inodes csum bytes and carry on.
4639 * Otherwise we have to do the normal free thing to account for
4640 * the case that the free side didn't free up its reserve
4641 * because of this outstanding reservation.
Josef Bacik7ed49f12011-08-19 15:45:52 -04004642 */
Josef Bacik660d3f62011-12-09 11:18:51 -05004643 if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4644 calc_csum_metadata_size(inode, num_bytes, 0);
4645 else
4646 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4647 spin_unlock(&BTRFS_I(inode)->lock);
4648 if (dropped)
4649 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4650
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004651 if (to_free) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004652 btrfs_block_rsv_release(root, block_rsv, to_free);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004653 trace_btrfs_space_reservation(root->fs_info,
4654 "delalloc",
4655 btrfs_ino(inode),
4656 to_free, 0);
4657 }
Miao Xie4b5829a2012-12-05 10:53:25 +00004658 if (root->fs_info->quota_enabled) {
4659 btrfs_qgroup_free(root, num_bytes +
4660 nr_extents * root->leafsize);
4661 }
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004662 if (delalloc_lock)
4663 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004664 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004665 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004666
Josef Bacik660d3f62011-12-09 11:18:51 -05004667 spin_lock(&BTRFS_I(inode)->lock);
4668 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004669 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4670 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004671 nr_extents--;
4672 }
4673 BTRFS_I(inode)->reserved_extents += nr_extents;
4674 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004675
4676 if (delalloc_lock)
4677 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004678
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004679 if (to_reserve)
4680 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4681 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004682 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4683
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004684 return 0;
4685}
4686
Josef Bacik7709cde2011-08-04 10:25:02 -04004687/**
4688 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4689 * @inode: the inode to release the reservation for
4690 * @num_bytes: the number of bytes we're releasing
4691 *
4692 * This will release the metadata reservation for an inode. This can be called
4693 * once we complete IO for a given set of bytes to release their metadata
4694 * reservations.
4695 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004696void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4697{
4698 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004699 u64 to_free = 0;
4700 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004701
4702 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004703 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004704 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004705
Josef Bacik7709cde2011-08-04 10:25:02 -04004706 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4707 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004708 if (dropped > 0)
4709 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004710
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004711 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4712 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004713 if (root->fs_info->quota_enabled) {
4714 btrfs_qgroup_free(root, num_bytes +
4715 dropped * root->leafsize);
4716 }
4717
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004718 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4719 to_free);
4720}
4721
Josef Bacik7709cde2011-08-04 10:25:02 -04004722/**
4723 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4724 * @inode: inode we're writing to
4725 * @num_bytes: the number of bytes we want to allocate
4726 *
4727 * This will do the following things
4728 *
4729 * o reserve space in the data space info for num_bytes
4730 * o reserve space in the metadata space info based on number of outstanding
4731 * extents and how much csums will be needed
4732 * o add to the inodes ->delalloc_bytes
4733 * o add it to the fs_info's delalloc inodes list.
4734 *
4735 * This will return 0 for success and -ENOSPC if there is no space left.
4736 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004737int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4738{
4739 int ret;
4740
4741 ret = btrfs_check_data_free_space(inode, num_bytes);
4742 if (ret)
4743 return ret;
4744
4745 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4746 if (ret) {
4747 btrfs_free_reserved_data_space(inode, num_bytes);
4748 return ret;
4749 }
4750
4751 return 0;
4752}
4753
Josef Bacik7709cde2011-08-04 10:25:02 -04004754/**
4755 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4756 * @inode: inode we're releasing space for
4757 * @num_bytes: the number of bytes we want to free up
4758 *
4759 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4760 * called in the case that we don't need the metadata AND data reservations
4761 * anymore. So if there is an error or we insert an inline extent.
4762 *
4763 * This function will release the metadata space that was not used and will
4764 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4765 * list if there are no delalloc bytes left.
4766 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004767void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4768{
4769 btrfs_delalloc_release_metadata(inode, num_bytes);
4770 btrfs_free_reserved_data_space(inode, num_bytes);
4771}
4772
Liu Boc53d6132012-12-27 09:01:19 +00004773static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004774 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004775{
Josef Bacik0af3d002010-06-21 14:48:16 -04004776 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004777 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004778 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004779 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004780 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004781 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004782
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004783 /* block accounting for super block */
4784 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02004785 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004786 if (alloc)
4787 old_val += num_bytes;
4788 else
4789 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02004790 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004791 spin_unlock(&info->delalloc_lock);
4792
Chris Masond3977122009-01-05 21:25:51 -05004793 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004794 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004795 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004796 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004797 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4798 BTRFS_BLOCK_GROUP_RAID1 |
4799 BTRFS_BLOCK_GROUP_RAID10))
4800 factor = 2;
4801 else
4802 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004803 /*
4804 * If this block group has free space cache written out, we
4805 * need to make sure to load it if we are removing space. This
4806 * is because we need the unpinning stage to actually add the
4807 * space back to the block group, otherwise we will leak space.
4808 */
4809 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00004810 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004811
Chris Masondb945352007-10-15 16:15:53 -04004812 byte_in_group = bytenr - cache->key.objectid;
4813 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004814
Josef Bacik25179202008-10-29 14:49:05 -04004815 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004816 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004817
Josef Bacik73bc1872011-10-03 14:07:49 -04004818 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04004819 cache->disk_cache_state < BTRFS_DC_CLEAR)
4820 cache->disk_cache_state = BTRFS_DC_CLEAR;
4821
Josef Bacik0f9dd462008-09-23 13:14:11 -04004822 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004823 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004824 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004825 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004826 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004827 btrfs_set_block_group_used(&cache->item, old_val);
4828 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004829 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004830 cache->space_info->bytes_used += num_bytes;
4831 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004832 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004833 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004834 } else {
Chris Masondb945352007-10-15 16:15:53 -04004835 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004836 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004837 cache->pinned += num_bytes;
4838 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004839 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004840 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004841 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004842 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004843
Yan, Zhengf0486c62010-05-16 10:46:25 -04004844 set_extent_dirty(info->pinned_extents,
4845 bytenr, bytenr + num_bytes - 1,
4846 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004847 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04004848 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004849 total -= num_bytes;
4850 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004851 }
4852 return 0;
4853}
Chris Mason6324fbf2008-03-24 15:01:59 -04004854
Chris Masona061fc82008-05-07 11:43:44 -04004855static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4856{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004857 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004858 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004859
Liu Boa1897fd2012-12-27 09:01:23 +00004860 spin_lock(&root->fs_info->block_group_cache_lock);
4861 bytenr = root->fs_info->first_logical_byte;
4862 spin_unlock(&root->fs_info->block_group_cache_lock);
4863
4864 if (bytenr < (u64)-1)
4865 return bytenr;
4866
Josef Bacik0f9dd462008-09-23 13:14:11 -04004867 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4868 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004869 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004870
Yan Zhengd2fb3432008-12-11 16:30:39 -05004871 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04004872 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004873
4874 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004875}
4876
Yan, Zhengf0486c62010-05-16 10:46:25 -04004877static int pin_down_extent(struct btrfs_root *root,
4878 struct btrfs_block_group_cache *cache,
4879 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004880{
Yan Zheng11833d62009-09-11 16:11:19 -04004881 spin_lock(&cache->space_info->lock);
4882 spin_lock(&cache->lock);
4883 cache->pinned += num_bytes;
4884 cache->space_info->bytes_pinned += num_bytes;
4885 if (reserved) {
4886 cache->reserved -= num_bytes;
4887 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004888 }
Yan Zheng11833d62009-09-11 16:11:19 -04004889 spin_unlock(&cache->lock);
4890 spin_unlock(&cache->space_info->lock);
4891
Yan, Zhengf0486c62010-05-16 10:46:25 -04004892 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4893 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004894 return 0;
4895}
Chris Mason9078a3e2007-04-26 16:46:15 -04004896
Yan, Zhengf0486c62010-05-16 10:46:25 -04004897/*
4898 * this function must be called within transaction
4899 */
4900int btrfs_pin_extent(struct btrfs_root *root,
4901 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004902{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004903 struct btrfs_block_group_cache *cache;
4904
4905 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004906 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04004907
4908 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4909
4910 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004911 return 0;
4912}
Zheng Yane8569812008-09-26 10:05:48 -04004913
Yan, Zhengf0486c62010-05-16 10:46:25 -04004914/*
Chris Masone688b722011-10-31 20:52:39 -04004915 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04004916 */
Liu Bodcfac412012-12-27 09:01:20 +00004917int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b722011-10-31 20:52:39 -04004918 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004919{
Chris Masone688b722011-10-31 20:52:39 -04004920 struct btrfs_block_group_cache *cache;
4921
4922 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004923 BUG_ON(!cache); /* Logic error */
Chris Masone688b722011-10-31 20:52:39 -04004924
4925 /*
4926 * pull in the free space cache (if any) so that our pin
4927 * removes the free space from the cache. We have load_only set
4928 * to one because the slow code to read in the free extents does check
4929 * the pinned extents.
4930 */
Liu Bof6373bf2012-12-27 09:01:18 +00004931 cache_block_group(cache, 1);
Chris Masone688b722011-10-31 20:52:39 -04004932
4933 pin_down_extent(root, cache, bytenr, num_bytes, 0);
4934
4935 /* remove us from the free space cache (if we're there at all) */
4936 btrfs_remove_free_space(cache, bytenr, num_bytes);
4937 btrfs_put_block_group(cache);
4938 return 0;
4939}
4940
Josef Bacikfb25e912011-07-26 17:00:46 -04004941/**
4942 * btrfs_update_reserved_bytes - update the block_group and space info counters
4943 * @cache: The cache we are manipulating
4944 * @num_bytes: The number of bytes in question
4945 * @reserve: One of the reservation enums
4946 *
4947 * This is called by the allocator when it reserves space, or by somebody who is
4948 * freeing space that was never actually used on disk. For example if you
4949 * reserve some space for a new leaf in transaction A and before transaction A
4950 * commits you free that leaf, you call this with reserve set to 0 in order to
4951 * clear the reservation.
4952 *
4953 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4954 * ENOSPC accounting. For data we handle the reservation through clearing the
4955 * delalloc bits in the io_tree. We have to do this since we could end up
4956 * allocating less disk space for the amount of data we have reserved in the
4957 * case of compression.
4958 *
4959 * If this is a reservation and the block group has become read only we cannot
4960 * make the reservation and return -EAGAIN, otherwise this function always
4961 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04004962 */
Josef Bacikfb25e912011-07-26 17:00:46 -04004963static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4964 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004965{
Josef Bacikfb25e912011-07-26 17:00:46 -04004966 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004967 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004968
Josef Bacikfb25e912011-07-26 17:00:46 -04004969 spin_lock(&space_info->lock);
4970 spin_lock(&cache->lock);
4971 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004972 if (cache->ro) {
4973 ret = -EAGAIN;
4974 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04004975 cache->reserved += num_bytes;
4976 space_info->bytes_reserved += num_bytes;
4977 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004978 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004979 "space_info", space_info->flags,
4980 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04004981 space_info->bytes_may_use -= num_bytes;
4982 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004983 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004984 } else {
4985 if (cache->ro)
4986 space_info->bytes_readonly += num_bytes;
4987 cache->reserved -= num_bytes;
4988 space_info->bytes_reserved -= num_bytes;
4989 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004990 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004991 spin_unlock(&cache->lock);
4992 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004993 return ret;
4994}
4995
Jeff Mahoney143bede2012-03-01 14:56:26 +01004996void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004997 struct btrfs_root *root)
4998{
4999 struct btrfs_fs_info *fs_info = root->fs_info;
5000 struct btrfs_caching_control *next;
5001 struct btrfs_caching_control *caching_ctl;
5002 struct btrfs_block_group_cache *cache;
5003
5004 down_write(&fs_info->extent_commit_sem);
5005
5006 list_for_each_entry_safe(caching_ctl, next,
5007 &fs_info->caching_block_groups, list) {
5008 cache = caching_ctl->block_group;
5009 if (block_group_cache_done(cache)) {
5010 cache->last_byte_to_unpin = (u64)-1;
5011 list_del_init(&caching_ctl->list);
5012 put_caching_control(caching_ctl);
5013 } else {
5014 cache->last_byte_to_unpin = caching_ctl->progress;
5015 }
5016 }
5017
5018 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5019 fs_info->pinned_extents = &fs_info->freed_extents[1];
5020 else
5021 fs_info->pinned_extents = &fs_info->freed_extents[0];
5022
5023 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005024
5025 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005026}
5027
5028static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5029{
5030 struct btrfs_fs_info *fs_info = root->fs_info;
5031 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005032 struct btrfs_space_info *space_info;
5033 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005034 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005035 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005036
5037 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005038 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005039 if (!cache ||
5040 start >= cache->key.objectid + cache->key.offset) {
5041 if (cache)
5042 btrfs_put_block_group(cache);
5043 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005044 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005045 }
5046
5047 len = cache->key.objectid + cache->key.offset - start;
5048 len = min(len, end + 1 - start);
5049
5050 if (start < cache->last_byte_to_unpin) {
5051 len = min(len, cache->last_byte_to_unpin - start);
5052 btrfs_add_free_space(cache, start, len);
5053 }
Josef Bacik25179202008-10-29 14:49:05 -04005054
Yan, Zhengf0486c62010-05-16 10:46:25 -04005055 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005056 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005057
Josef Bacik7b398f82012-10-22 15:52:28 -04005058 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005059 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005060 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005061 space_info->bytes_pinned -= len;
5062 if (cache->ro) {
5063 space_info->bytes_readonly += len;
5064 readonly = true;
5065 }
Josef Bacik25179202008-10-29 14:49:05 -04005066 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005067 if (!readonly && global_rsv->space_info == space_info) {
5068 spin_lock(&global_rsv->lock);
5069 if (!global_rsv->full) {
5070 len = min(len, global_rsv->size -
5071 global_rsv->reserved);
5072 global_rsv->reserved += len;
5073 space_info->bytes_may_use += len;
5074 if (global_rsv->reserved >= global_rsv->size)
5075 global_rsv->full = 1;
5076 }
5077 spin_unlock(&global_rsv->lock);
5078 }
5079 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005080 }
5081
5082 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005083 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005084 return 0;
5085}
5086
5087int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005088 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005089{
Yan Zheng11833d62009-09-11 16:11:19 -04005090 struct btrfs_fs_info *fs_info = root->fs_info;
5091 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005092 u64 start;
5093 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005094 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005095
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005096 if (trans->aborted)
5097 return 0;
5098
Yan Zheng11833d62009-09-11 16:11:19 -04005099 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5100 unpin = &fs_info->freed_extents[1];
5101 else
5102 unpin = &fs_info->freed_extents[0];
5103
Chris Masond3977122009-01-05 21:25:51 -05005104 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005105 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005106 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005107 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005108 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005109
Li Dongyang5378e602011-03-24 10:24:27 +00005110 if (btrfs_test_opt(root, DISCARD))
5111 ret = btrfs_discard_extent(root, start,
5112 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005113
Chris Mason1a5bc162007-10-15 16:15:26 -04005114 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005115 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005116 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005117 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005118
Chris Masone20d96d2007-03-22 12:13:20 -04005119 return 0;
5120}
5121
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005122static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5123 struct btrfs_root *root,
5124 u64 bytenr, u64 num_bytes, u64 parent,
5125 u64 root_objectid, u64 owner_objectid,
5126 u64 owner_offset, int refs_to_drop,
5127 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005128{
Chris Masone2fa7222007-03-12 16:22:34 -04005129 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005130 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005131 struct btrfs_fs_info *info = root->fs_info;
5132 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005133 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005134 struct btrfs_extent_item *ei;
5135 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005136 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005137 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005138 int extent_slot = 0;
5139 int found_extent = 0;
5140 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005141 u32 item_size;
5142 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05005143
Chris Mason5caf2a02007-04-02 11:20:42 -04005144 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005145 if (!path)
5146 return -ENOMEM;
5147
Chris Mason3c12ac72008-04-21 12:01:38 -04005148 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005149 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005150
5151 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5152 BUG_ON(!is_data && refs_to_drop != 1);
5153
5154 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5155 bytenr, num_bytes, parent,
5156 root_objectid, owner_objectid,
5157 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005158 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005159 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005160 while (extent_slot >= 0) {
5161 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005162 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005163 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005164 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005165 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5166 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005167 found_extent = 1;
5168 break;
5169 }
5170 if (path->slots[0] - extent_slot > 5)
5171 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005172 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005173 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005174#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5175 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5176 if (found_extent && item_size < sizeof(*ei))
5177 found_extent = 0;
5178#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005179 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005180 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005181 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005182 NULL, refs_to_drop,
5183 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005184 if (ret) {
5185 btrfs_abort_transaction(trans, extent_root, ret);
5186 goto out;
5187 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005188 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005189 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005190
5191 key.objectid = bytenr;
5192 key.type = BTRFS_EXTENT_ITEM_KEY;
5193 key.offset = num_bytes;
5194
Zheng Yan31840ae2008-09-23 13:14:14 -04005195 ret = btrfs_search_slot(trans, extent_root,
5196 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005197 if (ret) {
5198 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05005199 ", was looking for %llu\n", ret,
5200 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005201 if (ret > 0)
5202 btrfs_print_leaf(extent_root,
5203 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005204 }
David Sterba005d6422012-09-18 07:52:32 -06005205 if (ret < 0) {
5206 btrfs_abort_transaction(trans, extent_root, ret);
5207 goto out;
5208 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005209 extent_slot = path->slots[0];
5210 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005211 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005212 btrfs_print_leaf(extent_root, path->nodes[0]);
5213 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05005214 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005215 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05005216 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04005217 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05005218 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005219 (unsigned long long)owner_objectid,
5220 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005221 } else {
David Sterba005d6422012-09-18 07:52:32 -06005222 btrfs_abort_transaction(trans, extent_root, ret);
5223 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005224 }
Chris Mason5f39d392007-10-15 16:14:19 -04005225
5226 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005227 item_size = btrfs_item_size_nr(leaf, extent_slot);
5228#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5229 if (item_size < sizeof(*ei)) {
5230 BUG_ON(found_extent || extent_slot != path->slots[0]);
5231 ret = convert_extent_item_v0(trans, extent_root, path,
5232 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005233 if (ret < 0) {
5234 btrfs_abort_transaction(trans, extent_root, ret);
5235 goto out;
5236 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005237
David Sterbab3b4aa72011-04-21 01:20:15 +02005238 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005239 path->leave_spinning = 1;
5240
5241 key.objectid = bytenr;
5242 key.type = BTRFS_EXTENT_ITEM_KEY;
5243 key.offset = num_bytes;
5244
5245 ret = btrfs_search_slot(trans, extent_root, &key, path,
5246 -1, 1);
5247 if (ret) {
5248 printk(KERN_ERR "umm, got %d back from search"
5249 ", was looking for %llu\n", ret,
5250 (unsigned long long)bytenr);
5251 btrfs_print_leaf(extent_root, path->nodes[0]);
5252 }
David Sterba005d6422012-09-18 07:52:32 -06005253 if (ret < 0) {
5254 btrfs_abort_transaction(trans, extent_root, ret);
5255 goto out;
5256 }
5257
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005258 extent_slot = path->slots[0];
5259 leaf = path->nodes[0];
5260 item_size = btrfs_item_size_nr(leaf, extent_slot);
5261 }
5262#endif
5263 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005264 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005265 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005266 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5267 struct btrfs_tree_block_info *bi;
5268 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5269 bi = (struct btrfs_tree_block_info *)(ei + 1);
5270 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005271 }
5272
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005273 refs = btrfs_extent_refs(leaf, ei);
5274 BUG_ON(refs < refs_to_drop);
5275 refs -= refs_to_drop;
5276
5277 if (refs > 0) {
5278 if (extent_op)
5279 __run_delayed_extent_op(extent_op, leaf, ei);
5280 /*
5281 * In the case of inline back ref, reference count will
5282 * be updated by remove_extent_backref
5283 */
5284 if (iref) {
5285 BUG_ON(!found_extent);
5286 } else {
5287 btrfs_set_extent_refs(leaf, ei, refs);
5288 btrfs_mark_buffer_dirty(leaf);
5289 }
5290 if (found_extent) {
5291 ret = remove_extent_backref(trans, extent_root, path,
5292 iref, refs_to_drop,
5293 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005294 if (ret) {
5295 btrfs_abort_transaction(trans, extent_root, ret);
5296 goto out;
5297 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005298 }
5299 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005300 if (found_extent) {
5301 BUG_ON(is_data && refs_to_drop !=
5302 extent_data_ref_count(root, path, iref));
5303 if (iref) {
5304 BUG_ON(path->slots[0] != extent_slot);
5305 } else {
5306 BUG_ON(path->slots[0] != extent_slot + 1);
5307 path->slots[0] = extent_slot;
5308 num_to_del = 2;
5309 }
Chris Mason78fae272007-03-25 11:35:08 -04005310 }
Chris Masonb9473432009-03-13 11:00:37 -04005311
Chris Mason952fcca2008-02-18 16:33:44 -05005312 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5313 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005314 if (ret) {
5315 btrfs_abort_transaction(trans, extent_root, ret);
5316 goto out;
5317 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005318 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005319
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005320 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005321 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005322 if (ret) {
5323 btrfs_abort_transaction(trans, extent_root, ret);
5324 goto out;
5325 }
Chris Mason459931e2008-12-10 09:10:46 -05005326 }
5327
Liu Boc53d6132012-12-27 09:01:19 +00005328 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005329 if (ret) {
5330 btrfs_abort_transaction(trans, extent_root, ret);
5331 goto out;
5332 }
Chris Masona28ec192007-03-06 20:08:01 -05005333 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005334out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005335 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005336 return ret;
5337}
5338
5339/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005340 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005341 * delayed ref for that extent as well. This searches the delayed ref tree for
5342 * a given extent, and if there are no other delayed refs to be processed, it
5343 * removes it from the tree.
5344 */
5345static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5346 struct btrfs_root *root, u64 bytenr)
5347{
5348 struct btrfs_delayed_ref_head *head;
5349 struct btrfs_delayed_ref_root *delayed_refs;
5350 struct btrfs_delayed_ref_node *ref;
5351 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005352 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005353
5354 delayed_refs = &trans->transaction->delayed_refs;
5355 spin_lock(&delayed_refs->lock);
5356 head = btrfs_find_delayed_ref_head(trans, bytenr);
5357 if (!head)
5358 goto out;
5359
5360 node = rb_prev(&head->node.rb_node);
5361 if (!node)
5362 goto out;
5363
5364 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5365
5366 /* there are still entries for this ref, we can't drop it */
5367 if (ref->bytenr == bytenr)
5368 goto out;
5369
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005370 if (head->extent_op) {
5371 if (!head->must_insert_reserved)
5372 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005373 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005374 head->extent_op = NULL;
5375 }
5376
Chris Mason1887be62009-03-13 10:11:24 -04005377 /*
5378 * waiting for the lock here would deadlock. If someone else has it
5379 * locked they are already in the process of dropping it anyway
5380 */
5381 if (!mutex_trylock(&head->mutex))
5382 goto out;
5383
5384 /*
5385 * at this point we have a head with no other entries. Go
5386 * ahead and process it.
5387 */
5388 head->node.in_tree = 0;
5389 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005390
Chris Mason1887be62009-03-13 10:11:24 -04005391 delayed_refs->num_entries--;
5392
5393 /*
5394 * we don't take a ref on the node because we're removing it from the
5395 * tree, so we just steal the ref the tree was holding.
5396 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005397 delayed_refs->num_heads--;
5398 if (list_empty(&head->cluster))
5399 delayed_refs->num_heads_ready--;
5400
5401 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005402 spin_unlock(&delayed_refs->lock);
5403
Yan, Zhengf0486c62010-05-16 10:46:25 -04005404 BUG_ON(head->extent_op);
5405 if (head->must_insert_reserved)
5406 ret = 1;
5407
5408 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005409 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005410 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005411out:
5412 spin_unlock(&delayed_refs->lock);
5413 return 0;
5414}
5415
Yan, Zhengf0486c62010-05-16 10:46:25 -04005416void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5417 struct btrfs_root *root,
5418 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005419 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005420{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005421 struct btrfs_block_group_cache *cache = NULL;
5422 int ret;
5423
5424 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005425 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5426 buf->start, buf->len,
5427 parent, root->root_key.objectid,
5428 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005429 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005430 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005431 }
5432
5433 if (!last_ref)
5434 return;
5435
Yan, Zhengf0486c62010-05-16 10:46:25 -04005436 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005437
5438 if (btrfs_header_generation(buf) == trans->transid) {
5439 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5440 ret = check_ref_cleanup(trans, root, buf->start);
5441 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005442 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005443 }
5444
5445 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5446 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005447 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005448 }
5449
5450 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5451
5452 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005453 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005454 }
5455out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005456 /*
5457 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5458 * anymore.
5459 */
5460 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005461 btrfs_put_block_group(cache);
5462}
5463
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005464/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005465int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5466 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5467 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005468{
5469 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005470 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005471
Chris Mason56bec292009-03-13 10:10:06 -04005472 /*
5473 * tree log blocks never actually go into the extent allocation
5474 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005475 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005476 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5477 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005478 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005479 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005480 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005481 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005482 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5483 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005484 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005485 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005487 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5488 num_bytes,
5489 parent, root_objectid, owner,
5490 offset, BTRFS_DROP_DELAYED_REF,
5491 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005492 }
Chris Mason925baed2008-06-25 16:01:30 -04005493 return ret;
5494}
5495
Chris Mason87ee04e2007-11-30 11:30:34 -05005496static u64 stripe_align(struct btrfs_root *root, u64 val)
5497{
5498 u64 mask = ((u64)root->stripesize - 1);
5499 u64 ret = (val + mask) & ~mask;
5500 return ret;
5501}
5502
Chris Masonfec577f2007-02-26 10:40:21 -05005503/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005504 * when we wait for progress in the block group caching, its because
5505 * our allocation attempt failed at least once. So, we must sleep
5506 * and let some progress happen before we try again.
5507 *
5508 * This function will sleep at least once waiting for new free space to
5509 * show up, and then it will check the block group free space numbers
5510 * for our min num_bytes. Another option is to have it go ahead
5511 * and look in the rbtree for a free extent of a given size, but this
5512 * is a good start.
5513 */
5514static noinline int
5515wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5516 u64 num_bytes)
5517{
Yan Zheng11833d62009-09-11 16:11:19 -04005518 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005519 DEFINE_WAIT(wait);
5520
Yan Zheng11833d62009-09-11 16:11:19 -04005521 caching_ctl = get_caching_control(cache);
5522 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005523 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005524
Yan Zheng11833d62009-09-11 16:11:19 -04005525 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb6c2011-03-29 13:46:06 +08005526 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005527
5528 put_caching_control(caching_ctl);
5529 return 0;
5530}
5531
5532static noinline int
5533wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5534{
5535 struct btrfs_caching_control *caching_ctl;
5536 DEFINE_WAIT(wait);
5537
5538 caching_ctl = get_caching_control(cache);
5539 if (!caching_ctl)
5540 return 0;
5541
5542 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5543
5544 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005545 return 0;
5546}
5547
Liu Bo31e50222012-11-21 14:18:10 +00005548int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005549{
Ilya Dryomov7738a532012-03-27 17:09:17 +03005550 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00005551 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005552 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00005553 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005554 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00005555 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005556 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00005557 return BTRFS_RAID_RAID0;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005558 else
Miao Xiee6ec7162013-01-17 05:38:51 +00005559 return BTRFS_RAID_SINGLE;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005560}
5561
Ilya Dryomov7738a532012-03-27 17:09:17 +03005562static int get_block_group_index(struct btrfs_block_group_cache *cache)
5563{
Liu Bo31e50222012-11-21 14:18:10 +00005564 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005565}
5566
Josef Bacik817d52f2009-07-13 21:29:25 -04005567enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005568 LOOP_CACHING_NOWAIT = 0,
5569 LOOP_CACHING_WAIT = 1,
5570 LOOP_ALLOC_CHUNK = 2,
5571 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005572};
5573
5574/*
Chris Masonfec577f2007-02-26 10:40:21 -05005575 * walks the btree of allocated extents and find a hole of a given size.
5576 * The key ins is changed to record the hole:
5577 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005578 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005579 * ins->offset == number of blocks
5580 * Any available blocks before search_start are skipped.
5581 */
Chris Masond3977122009-01-05 21:25:51 -05005582static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005583 struct btrfs_root *orig_root,
5584 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005585 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005586 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005587{
Josef Bacik80eb2342008-10-29 14:49:05 -04005588 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005589 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005590 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005591 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005592 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005593 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005594 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005595 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005596 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005597 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005598 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5599 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005600 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005601 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005602 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005603 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005604 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005605
Chris Masondb945352007-10-15 16:15:53 -04005606 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005607 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005608 ins->objectid = 0;
5609 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005610
Josef Bacik3f7de032011-11-10 08:29:20 -05005611 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5612
Josef Bacik2552d172009-04-03 10:14:19 -04005613 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005614 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00005615 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005616 return -ENOSPC;
5617 }
Josef Bacik2552d172009-04-03 10:14:19 -04005618
Josef Bacik67377732010-09-16 16:19:09 -04005619 /*
5620 * If the space info is for both data and metadata it means we have a
5621 * small filesystem and we can't use the clustering stuff.
5622 */
5623 if (btrfs_mixed_space_info(space_info))
5624 use_cluster = false;
5625
Josef Bacik67377732010-09-16 16:19:09 -04005626 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005627 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005628 if (!btrfs_test_opt(root, SSD))
5629 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005630 }
5631
Josef Bacik67377732010-09-16 16:19:09 -04005632 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5633 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005634 last_ptr = &root->fs_info->data_alloc_cluster;
5635 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005636
Chris Mason239b14b2008-03-24 15:02:07 -04005637 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005638 spin_lock(&last_ptr->lock);
5639 if (last_ptr->block_group)
5640 hint_byte = last_ptr->window_start;
5641 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005642 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005643
Chris Masona061fc82008-05-07 11:43:44 -04005644 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005645 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005646
Josef Bacik817d52f2009-07-13 21:29:25 -04005647 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005648 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005649
Josef Bacik2552d172009-04-03 10:14:19 -04005650 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005651 block_group = btrfs_lookup_block_group(root->fs_info,
5652 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005653 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005654 /*
5655 * we don't want to use the block group if it doesn't match our
5656 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005657 *
5658 * However if we are re-searching with an ideal block group
5659 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005660 */
5661 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005662 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005663 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005664 if (list_empty(&block_group->list) ||
5665 block_group->ro) {
5666 /*
5667 * someone is removing this block group,
5668 * we can't jump into the have_block_group
5669 * target because our list pointers are not
5670 * valid
5671 */
5672 btrfs_put_block_group(block_group);
5673 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005674 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005675 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005676 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005677 }
Josef Bacik2552d172009-04-03 10:14:19 -04005678 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005679 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005680 }
Chris Mason42e70e72008-11-07 18:17:11 -05005681 }
Josef Bacik2552d172009-04-03 10:14:19 -04005682search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005683 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005684 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005685 list_for_each_entry(block_group, &space_info->block_groups[index],
5686 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005687 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005688 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005689
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005690 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005691 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005692 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005693
Chris Mason83a50de2010-12-13 15:06:46 -05005694 /*
5695 * this can happen if we end up cycling through all the
5696 * raid types, but we want to make sure we only allocate
5697 * for the proper type.
5698 */
5699 if (!block_group_bits(block_group, data)) {
5700 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5701 BTRFS_BLOCK_GROUP_RAID1 |
5702 BTRFS_BLOCK_GROUP_RAID10;
5703
5704 /*
5705 * if they asked for extra copies and this block group
5706 * doesn't provide them, bail. This does allow us to
5707 * fill raid0 from raid1.
5708 */
5709 if ((data & extra) && !(block_group->flags & extra))
5710 goto loop;
5711 }
5712
Josef Bacik2552d172009-04-03 10:14:19 -04005713have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05005714 cached = block_group_cache_done(block_group);
5715 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05005716 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00005717 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005718 BUG_ON(ret < 0);
5719 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005720 }
5721
Josef Bacikea6a4782008-11-20 12:16:16 -05005722 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005723 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005724
Josef Bacik0a243252009-09-11 16:11:20 -04005725 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005726 * Ok we want to try and use the cluster allocator, so
5727 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005728 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005729 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005730 /*
5731 * the refill lock keeps out other
5732 * people trying to start a new cluster
5733 */
5734 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005735 used_block_group = last_ptr->block_group;
5736 if (used_block_group != block_group &&
5737 (!used_block_group ||
5738 used_block_group->ro ||
5739 !block_group_bits(used_block_group, data))) {
5740 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04005741 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005742 }
Chris Mason44fb5512009-06-04 15:34:51 -04005743
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005744 if (used_block_group != block_group)
5745 btrfs_get_block_group(used_block_group);
5746
5747 offset = btrfs_alloc_from_cluster(used_block_group,
5748 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005749 if (offset) {
5750 /* we have a block, we're done */
5751 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005752 trace_btrfs_reserve_extent_cluster(root,
5753 block_group, search_start, num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005754 goto checks;
5755 }
5756
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005757 WARN_ON(last_ptr->block_group != used_block_group);
5758 if (used_block_group != block_group) {
5759 btrfs_put_block_group(used_block_group);
5760 used_block_group = block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005761 }
Chris Mason44fb5512009-06-04 15:34:51 -04005762refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005763 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005764 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5765 * set up a new clusters, so lets just skip it
5766 * and let the allocator find whatever block
5767 * it can find. If we reach this point, we
5768 * will have tried the cluster allocator
5769 * plenty of times and not have found
5770 * anything, so we are likely way too
5771 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005772 * anything.
5773 *
5774 * However, if the cluster is taken from the
5775 * current block group, release the cluster
5776 * first, so that we stand a better chance of
5777 * succeeding in the unclustered
5778 * allocation. */
5779 if (loop >= LOOP_NO_EMPTY_SIZE &&
5780 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005781 spin_unlock(&last_ptr->refill_lock);
5782 goto unclustered_alloc;
5783 }
5784
Chris Masonfa9c0d72009-04-03 09:47:43 -04005785 /*
5786 * this cluster didn't work out, free it and
5787 * start over
5788 */
5789 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5790
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005791 if (loop >= LOOP_NO_EMPTY_SIZE) {
5792 spin_unlock(&last_ptr->refill_lock);
5793 goto unclustered_alloc;
5794 }
5795
Chris Masonfa9c0d72009-04-03 09:47:43 -04005796 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005797 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005798 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05005799 search_start, num_bytes,
Chris Masonfa9c0d72009-04-03 09:47:43 -04005800 empty_cluster + empty_size);
5801 if (ret == 0) {
5802 /*
5803 * now pull our allocation out of this
5804 * cluster
5805 */
5806 offset = btrfs_alloc_from_cluster(block_group,
5807 last_ptr, num_bytes,
5808 search_start);
5809 if (offset) {
5810 /* we found one, proceed */
5811 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005812 trace_btrfs_reserve_extent_cluster(root,
5813 block_group, search_start,
5814 num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005815 goto checks;
5816 }
Josef Bacik0a243252009-09-11 16:11:20 -04005817 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5818 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005819 spin_unlock(&last_ptr->refill_lock);
5820
Josef Bacik0a243252009-09-11 16:11:20 -04005821 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005822 wait_block_group_cache_progress(block_group,
5823 num_bytes + empty_cluster + empty_size);
5824 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005825 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005826
Chris Masonfa9c0d72009-04-03 09:47:43 -04005827 /*
5828 * at this point we either didn't find a cluster
5829 * or we weren't able to allocate a block from our
5830 * cluster. Free the cluster we've been trying
5831 * to use, and go to the next block group
5832 */
Josef Bacik0a243252009-09-11 16:11:20 -04005833 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005834 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005835 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005836 }
5837
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005838unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005839 spin_lock(&block_group->free_space_ctl->tree_lock);
5840 if (cached &&
5841 block_group->free_space_ctl->free_space <
5842 num_bytes + empty_cluster + empty_size) {
5843 spin_unlock(&block_group->free_space_ctl->tree_lock);
5844 goto loop;
5845 }
5846 spin_unlock(&block_group->free_space_ctl->tree_lock);
5847
Josef Bacik6226cb02009-04-03 10:14:18 -04005848 offset = btrfs_find_space_for_alloc(block_group, search_start,
5849 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005850 /*
5851 * If we didn't find a chunk, and we haven't failed on this
5852 * block group before, and this block group is in the middle of
5853 * caching and we are ok with waiting, then go ahead and wait
5854 * for progress to be made, and set failed_alloc to true.
5855 *
5856 * If failed_alloc is true then we've already waited on this
5857 * block group once and should move on to the next block group.
5858 */
5859 if (!offset && !failed_alloc && !cached &&
5860 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005861 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005862 num_bytes + empty_size);
5863 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005864 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005865 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08005866 if (!cached)
5867 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005868 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005869 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005870checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005871 search_start = stripe_align(root, offset);
Chris Masone37c9e62007-05-09 20:13:14 -04005872
Josef Bacik2552d172009-04-03 10:14:19 -04005873 /* move on to the next group */
5874 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005875 used_block_group->key.objectid + used_block_group->key.offset) {
5876 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005877 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005878 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005879
Josef Bacik6226cb02009-04-03 10:14:18 -04005880 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005881 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04005882 search_start - offset);
5883 BUG_ON(offset > search_start);
5884
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005885 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04005886 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005887 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005888 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005889 goto loop;
5890 }
Yan Zheng11833d62009-09-11 16:11:19 -04005891
Josef Bacik2552d172009-04-03 10:14:19 -04005892 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005893 ins->objectid = search_start;
5894 ins->offset = num_bytes;
5895
Josef Bacik3f7de032011-11-10 08:29:20 -05005896 trace_btrfs_reserve_extent(orig_root, block_group,
5897 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005898 if (used_block_group != block_group)
5899 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005900 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005901 break;
5902loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005903 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005904 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005905 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005906 if (used_block_group != block_group)
5907 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04005908 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005909 }
5910 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005911
Miao Xie60d2adb2011-09-09 17:34:35 +08005912 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
5913 goto search;
5914
Yan, Zhengb742bb82010-05-16 10:46:24 -04005915 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5916 goto search;
5917
Josef Bacik285ff5a2012-01-13 15:27:45 -05005918 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005919 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5920 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005921 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5922 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5923 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5924 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04005925 */
Josef Bacik723bda22011-05-27 16:11:38 -04005926 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005927 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04005928 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005929 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04005930 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04005931 CHUNK_ALLOC_FORCE);
5932 /*
5933 * Do not bail out on ENOSPC since we
5934 * can do more things.
5935 */
5936 if (ret < 0 && ret != -ENOSPC) {
5937 btrfs_abort_transaction(trans,
5938 root, ret);
5939 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04005940 }
Josef Bacik723bda22011-05-27 16:11:38 -04005941 }
5942
5943 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04005944 empty_size = 0;
5945 empty_cluster = 0;
5946 }
Chris Mason42e70e72008-11-07 18:17:11 -05005947
Josef Bacik723bda22011-05-27 16:11:38 -04005948 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005949 } else if (!ins->objectid) {
5950 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005951 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005952 ret = 0;
5953 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005954out:
Chris Mason0b86a832008-03-24 15:01:56 -04005955
Chris Mason0f70abe2007-02-28 16:46:22 -05005956 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005957}
Chris Masonec44a352008-04-28 15:29:52 -04005958
Josef Bacik9ed74f22009-09-11 16:12:44 -04005959static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5960 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005961{
5962 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005963 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005964
Josef Bacik9ed74f22009-09-11 16:12:44 -04005965 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04005966 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
5967 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05005968 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005969 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005970 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005971 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005972 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5973 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005974 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005975 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005976 (unsigned long long)info->bytes_pinned,
5977 (unsigned long long)info->bytes_reserved,
5978 (unsigned long long)info->bytes_may_use,
5979 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005980 spin_unlock(&info->lock);
5981
5982 if (!dump_block_groups)
5983 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005984
Josef Bacik80eb2342008-10-29 14:49:05 -04005985 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005986again:
5987 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005988 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06005989 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05005990 (unsigned long long)cache->key.objectid,
5991 (unsigned long long)cache->key.offset,
5992 (unsigned long long)btrfs_block_group_used(&cache->item),
5993 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06005994 (unsigned long long)cache->reserved,
5995 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04005996 btrfs_dump_free_space(cache, bytes);
5997 spin_unlock(&cache->lock);
5998 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005999 if (++index < BTRFS_NR_RAID_TYPES)
6000 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006001 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006002}
Zheng Yane8569812008-09-26 10:05:48 -04006003
Yan Zheng11833d62009-09-11 16:11:19 -04006004int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6005 struct btrfs_root *root,
6006 u64 num_bytes, u64 min_alloc_size,
6007 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006008 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006009{
Miao Xie9e622d62012-01-26 15:01:12 -05006010 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006011 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006012
Josef Bacik6a632092009-02-20 11:00:09 -05006013 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006014again:
Chris Masondb945352007-10-15 16:15:53 -04006015 WARN_ON(num_bytes < root->sectorsize);
6016 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006017 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006018
Miao Xie9e622d62012-01-26 15:01:12 -05006019 if (ret == -ENOSPC) {
6020 if (!final_tried) {
6021 num_bytes = num_bytes >> 1;
6022 num_bytes = num_bytes & ~(root->sectorsize - 1);
6023 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006024 if (num_bytes == min_alloc_size)
6025 final_tried = true;
6026 goto again;
6027 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6028 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006029
Miao Xie9e622d62012-01-26 15:01:12 -05006030 sinfo = __find_space_info(root->fs_info, data);
6031 printk(KERN_ERR "btrfs allocation failed flags %llu, "
6032 "wanted %llu\n", (unsigned long long)data,
6033 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006034 if (sinfo)
6035 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006036 }
Chris Mason925baed2008-06-25 16:01:30 -04006037 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006038
liubo1abe9b82011-03-24 11:18:59 +00006039 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6040
Josef Bacik0f9dd462008-09-23 13:14:11 -04006041 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006042}
6043
Chris Masone688b722011-10-31 20:52:39 -04006044static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6045 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006046{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006047 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006048 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006049
Josef Bacik0f9dd462008-09-23 13:14:11 -04006050 cache = btrfs_lookup_block_group(root->fs_info, start);
6051 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05006052 printk(KERN_ERR "Unable to find block group for %llu\n",
6053 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006054 return -ENOSPC;
6055 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006056
Li Dongyang5378e602011-03-24 10:24:27 +00006057 if (btrfs_test_opt(root, DISCARD))
6058 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006059
Chris Masone688b722011-10-31 20:52:39 -04006060 if (pin)
6061 pin_down_extent(root, cache, start, len, 1);
6062 else {
6063 btrfs_add_free_space(cache, start, len);
6064 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6065 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006066 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006067
liubo1abe9b82011-03-24 11:18:59 +00006068 trace_btrfs_reserved_extent_free(root, start, len);
6069
Chris Masone6dcd2d2008-07-17 12:53:50 -04006070 return ret;
6071}
6072
Chris Masone688b722011-10-31 20:52:39 -04006073int btrfs_free_reserved_extent(struct btrfs_root *root,
6074 u64 start, u64 len)
6075{
6076 return __btrfs_free_reserved_extent(root, start, len, 0);
6077}
6078
6079int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6080 u64 start, u64 len)
6081{
6082 return __btrfs_free_reserved_extent(root, start, len, 1);
6083}
6084
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006085static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6086 struct btrfs_root *root,
6087 u64 parent, u64 root_objectid,
6088 u64 flags, u64 owner, u64 offset,
6089 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006090{
6091 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006092 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006093 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006094 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006095 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006096 struct extent_buffer *leaf;
6097 int type;
6098 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006099
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006100 if (parent > 0)
6101 type = BTRFS_SHARED_DATA_REF_KEY;
6102 else
6103 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006104
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006105 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006106
6107 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006108 if (!path)
6109 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006110
Chris Masonb9473432009-03-13 11:00:37 -04006111 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006112 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6113 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006114 if (ret) {
6115 btrfs_free_path(path);
6116 return ret;
6117 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006119 leaf = path->nodes[0];
6120 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006121 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006122 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6123 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6124 btrfs_set_extent_flags(leaf, extent_item,
6125 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006126
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006127 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6128 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6129 if (parent > 0) {
6130 struct btrfs_shared_data_ref *ref;
6131 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6132 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6133 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6134 } else {
6135 struct btrfs_extent_data_ref *ref;
6136 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6137 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6138 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6139 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6140 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6141 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006142
6143 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006144 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006145
Liu Boc53d6132012-12-27 09:01:19 +00006146 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006147 if (ret) { /* -ENOENT, logic error */
Chris Masond3977122009-01-05 21:25:51 -05006148 printk(KERN_ERR "btrfs update block group failed for %llu "
6149 "%llu\n", (unsigned long long)ins->objectid,
6150 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006151 BUG();
6152 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006153 return ret;
6154}
6155
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006156static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6157 struct btrfs_root *root,
6158 u64 parent, u64 root_objectid,
6159 u64 flags, struct btrfs_disk_key *key,
6160 int level, struct btrfs_key *ins)
6161{
6162 int ret;
6163 struct btrfs_fs_info *fs_info = root->fs_info;
6164 struct btrfs_extent_item *extent_item;
6165 struct btrfs_tree_block_info *block_info;
6166 struct btrfs_extent_inline_ref *iref;
6167 struct btrfs_path *path;
6168 struct extent_buffer *leaf;
6169 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
6170
6171 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006172 if (!path)
6173 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006174
6175 path->leave_spinning = 1;
6176 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6177 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006178 if (ret) {
6179 btrfs_free_path(path);
6180 return ret;
6181 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006182
6183 leaf = path->nodes[0];
6184 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6185 struct btrfs_extent_item);
6186 btrfs_set_extent_refs(leaf, extent_item, 1);
6187 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6188 btrfs_set_extent_flags(leaf, extent_item,
6189 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6190 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6191
6192 btrfs_set_tree_block_key(leaf, block_info, key);
6193 btrfs_set_tree_block_level(leaf, block_info, level);
6194
6195 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6196 if (parent > 0) {
6197 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6198 btrfs_set_extent_inline_ref_type(leaf, iref,
6199 BTRFS_SHARED_BLOCK_REF_KEY);
6200 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6201 } else {
6202 btrfs_set_extent_inline_ref_type(leaf, iref,
6203 BTRFS_TREE_BLOCK_REF_KEY);
6204 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6205 }
6206
6207 btrfs_mark_buffer_dirty(leaf);
6208 btrfs_free_path(path);
6209
Liu Boc53d6132012-12-27 09:01:19 +00006210 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006211 if (ret) { /* -ENOENT, logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006212 printk(KERN_ERR "btrfs update block group failed for %llu "
6213 "%llu\n", (unsigned long long)ins->objectid,
6214 (unsigned long long)ins->offset);
6215 BUG();
6216 }
6217 return ret;
6218}
6219
6220int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6221 struct btrfs_root *root,
6222 u64 root_objectid, u64 owner,
6223 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006224{
6225 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006226
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006227 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006228
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006229 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6230 ins->offset, 0,
6231 root_objectid, owner, offset,
6232 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006233 return ret;
6234}
Chris Masone02119d2008-09-05 16:13:11 -04006235
6236/*
6237 * this is used by the tree logging recovery code. It records that
6238 * an extent has been allocated and makes sure to clear the free
6239 * space cache bits as well
6240 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006241int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6242 struct btrfs_root *root,
6243 u64 root_objectid, u64 owner, u64 offset,
6244 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006245{
6246 int ret;
6247 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006248 struct btrfs_caching_control *caching_ctl;
6249 u64 start = ins->objectid;
6250 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006251
Chris Masone02119d2008-09-05 16:13:11 -04006252 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006253 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006254 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006255
Yan Zheng11833d62009-09-11 16:11:19 -04006256 if (!caching_ctl) {
6257 BUG_ON(!block_group_cache_done(block_group));
6258 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006259 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006260 } else {
6261 mutex_lock(&caching_ctl->mutex);
6262
6263 if (start >= caching_ctl->progress) {
6264 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006265 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006266 } else if (start + num_bytes <= caching_ctl->progress) {
6267 ret = btrfs_remove_free_space(block_group,
6268 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006269 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006270 } else {
6271 num_bytes = caching_ctl->progress - start;
6272 ret = btrfs_remove_free_space(block_group,
6273 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006274 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006275
6276 start = caching_ctl->progress;
6277 num_bytes = ins->objectid + ins->offset -
6278 caching_ctl->progress;
6279 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006281 }
6282
6283 mutex_unlock(&caching_ctl->mutex);
6284 put_caching_control(caching_ctl);
6285 }
6286
Josef Bacikfb25e912011-07-26 17:00:46 -04006287 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6288 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006289 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d72009-04-03 09:47:43 -04006290 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006291 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6292 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006293 return ret;
6294}
6295
Chris Mason65b51a02008-08-01 15:11:20 -04006296struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6297 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006298 u64 bytenr, u32 blocksize,
6299 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006300{
6301 struct extent_buffer *buf;
6302
6303 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6304 if (!buf)
6305 return ERR_PTR(-ENOMEM);
6306 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006307 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006308 btrfs_tree_lock(buf);
6309 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006310 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006311
6312 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006313 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006314
Chris Masond0c803c2008-09-11 16:17:57 -04006315 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006316 /*
6317 * we allow two log transactions at a time, use different
6318 * EXENT bit to differentiate dirty pages.
6319 */
6320 if (root->log_transid % 2 == 0)
6321 set_extent_dirty(&root->dirty_log_pages, buf->start,
6322 buf->start + buf->len - 1, GFP_NOFS);
6323 else
6324 set_extent_new(&root->dirty_log_pages, buf->start,
6325 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006326 } else {
6327 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6328 buf->start + buf->len - 1, GFP_NOFS);
6329 }
Chris Mason65b51a02008-08-01 15:11:20 -04006330 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006331 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006332 return buf;
6333}
6334
Yan, Zhengf0486c62010-05-16 10:46:25 -04006335static struct btrfs_block_rsv *
6336use_block_rsv(struct btrfs_trans_handle *trans,
6337 struct btrfs_root *root, u32 blocksize)
6338{
6339 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006340 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006341 int ret;
6342
6343 block_rsv = get_block_rsv(trans, root);
6344
6345 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006346 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6347 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006348 /*
6349 * If we couldn't reserve metadata bytes try and use some from
6350 * the global reserve.
6351 */
6352 if (ret && block_rsv != global_rsv) {
6353 ret = block_rsv_use_bytes(global_rsv, blocksize);
6354 if (!ret)
6355 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006356 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006357 } else if (ret) {
6358 return ERR_PTR(ret);
6359 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006360 return block_rsv;
6361 }
6362
6363 ret = block_rsv_use_bytes(block_rsv, blocksize);
6364 if (!ret)
6365 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006366 if (ret && !block_rsv->failfast) {
David Sterbadff51cd2011-06-14 12:52:17 +02006367 static DEFINE_RATELIMIT_STATE(_rs,
6368 DEFAULT_RATELIMIT_INTERVAL,
6369 /*DEFAULT_RATELIMIT_BURST*/ 2);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006370 if (__ratelimit(&_rs))
6371 WARN(1, KERN_DEBUG "btrfs: block rsv returned %d\n",
6372 ret);
Miao Xie08e007d2012-10-16 11:33:38 +00006373 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6374 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006375 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006376 return block_rsv;
6377 } else if (ret && block_rsv != global_rsv) {
6378 ret = block_rsv_use_bytes(global_rsv, blocksize);
6379 if (!ret)
6380 return global_rsv;
6381 }
6382 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006383
Yan, Zhengf0486c62010-05-16 10:46:25 -04006384 return ERR_PTR(-ENOSPC);
6385}
6386
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006387static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6388 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006389{
6390 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006391 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006392}
6393
Chris Masonfec577f2007-02-26 10:40:21 -05006394/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006395 * finds a free extent and does all the dirty work required for allocation
6396 * returns the key for the extent through ins, and a tree buffer for
6397 * the first block of the extent through buf.
6398 *
Chris Masonfec577f2007-02-26 10:40:21 -05006399 * returns the tree buffer or NULL.
6400 */
Chris Mason5f39d392007-10-15 16:14:19 -04006401struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006402 struct btrfs_root *root, u32 blocksize,
6403 u64 parent, u64 root_objectid,
6404 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006405 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006406{
Chris Masone2fa7222007-03-12 16:22:34 -04006407 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006408 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006409 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006410 u64 flags = 0;
6411 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006412
Yan, Zhengf0486c62010-05-16 10:46:25 -04006413
6414 block_rsv = use_block_rsv(trans, root, blocksize);
6415 if (IS_ERR(block_rsv))
6416 return ERR_CAST(block_rsv);
6417
6418 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006419 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006420 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006421 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006422 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006423 }
Chris Mason55c69072008-01-09 15:55:33 -05006424
Chris Mason4008c042009-02-12 14:09:45 -05006425 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6426 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006427 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006428
6429 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6430 if (parent == 0)
6431 parent = ins.objectid;
6432 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6433 } else
6434 BUG_ON(parent > 0);
6435
6436 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6437 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006438 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006439 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006440 if (key)
6441 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6442 else
6443 memset(&extent_op->key, 0, sizeof(extent_op->key));
6444 extent_op->flags_to_set = flags;
6445 extent_op->update_key = 1;
6446 extent_op->update_flags = 1;
6447 extent_op->is_data = 0;
6448
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006449 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6450 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006451 ins.offset, parent, root_objectid,
6452 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006453 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006454 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006455 }
Chris Masonfec577f2007-02-26 10:40:21 -05006456 return buf;
6457}
Chris Masona28ec192007-03-06 20:08:01 -05006458
Yan Zheng2c47e6052009-06-27 21:07:35 -04006459struct walk_control {
6460 u64 refs[BTRFS_MAX_LEVEL];
6461 u64 flags[BTRFS_MAX_LEVEL];
6462 struct btrfs_key update_progress;
6463 int stage;
6464 int level;
6465 int shared_level;
6466 int update_ref;
6467 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006468 int reada_slot;
6469 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006470 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006471};
6472
6473#define DROP_REFERENCE 1
6474#define UPDATE_BACKREF 2
6475
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006476static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6477 struct btrfs_root *root,
6478 struct walk_control *wc,
6479 struct btrfs_path *path)
6480{
6481 u64 bytenr;
6482 u64 generation;
6483 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006484 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006485 u32 nritems;
6486 u32 blocksize;
6487 struct btrfs_key key;
6488 struct extent_buffer *eb;
6489 int ret;
6490 int slot;
6491 int nread = 0;
6492
6493 if (path->slots[wc->level] < wc->reada_slot) {
6494 wc->reada_count = wc->reada_count * 2 / 3;
6495 wc->reada_count = max(wc->reada_count, 2);
6496 } else {
6497 wc->reada_count = wc->reada_count * 3 / 2;
6498 wc->reada_count = min_t(int, wc->reada_count,
6499 BTRFS_NODEPTRS_PER_BLOCK(root));
6500 }
6501
6502 eb = path->nodes[wc->level];
6503 nritems = btrfs_header_nritems(eb);
6504 blocksize = btrfs_level_size(root, wc->level - 1);
6505
6506 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6507 if (nread >= wc->reada_count)
6508 break;
6509
6510 cond_resched();
6511 bytenr = btrfs_node_blockptr(eb, slot);
6512 generation = btrfs_node_ptr_generation(eb, slot);
6513
6514 if (slot == path->slots[wc->level])
6515 goto reada;
6516
6517 if (wc->stage == UPDATE_BACKREF &&
6518 generation <= root->root_key.offset)
6519 continue;
6520
Yan, Zheng94fcca92009-10-09 09:25:16 -04006521 /* We don't lock the tree block, it's OK to be racy here */
6522 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6523 &refs, &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006524 /* We don't care about errors in readahead. */
6525 if (ret < 0)
6526 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006527 BUG_ON(refs == 0);
6528
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006529 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006530 if (refs == 1)
6531 goto reada;
6532
Yan, Zheng94fcca92009-10-09 09:25:16 -04006533 if (wc->level == 1 &&
6534 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6535 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006536 if (!wc->update_ref ||
6537 generation <= root->root_key.offset)
6538 continue;
6539 btrfs_node_key_to_cpu(eb, &key, slot);
6540 ret = btrfs_comp_cpu_keys(&key,
6541 &wc->update_progress);
6542 if (ret < 0)
6543 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006544 } else {
6545 if (wc->level == 1 &&
6546 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6547 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006548 }
6549reada:
6550 ret = readahead_tree_block(root, bytenr, blocksize,
6551 generation);
6552 if (ret)
6553 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006554 nread++;
6555 }
6556 wc->reada_slot = slot;
6557}
6558
Chris Mason9aca1d52007-03-13 11:09:37 -04006559/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006560 * hepler to process tree block while walking down the tree.
6561 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006562 * when wc->stage == UPDATE_BACKREF, this function updates
6563 * back refs for pointers in the block.
6564 *
6565 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006566 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006567static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6568 struct btrfs_root *root,
6569 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006570 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006571{
6572 int level = wc->level;
6573 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006574 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6575 int ret;
6576
6577 if (wc->stage == UPDATE_BACKREF &&
6578 btrfs_header_owner(eb) != root->root_key.objectid)
6579 return 1;
6580
6581 /*
6582 * when reference count of tree block is 1, it won't increase
6583 * again. once full backref flag is set, we never clear it.
6584 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006585 if (lookup_info &&
6586 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6587 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006588 BUG_ON(!path->locks[level]);
6589 ret = btrfs_lookup_extent_info(trans, root,
6590 eb->start, eb->len,
6591 &wc->refs[level],
6592 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006593 BUG_ON(ret == -ENOMEM);
6594 if (ret)
6595 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006596 BUG_ON(wc->refs[level] == 0);
6597 }
6598
Yan Zheng2c47e6052009-06-27 21:07:35 -04006599 if (wc->stage == DROP_REFERENCE) {
6600 if (wc->refs[level] > 1)
6601 return 1;
6602
6603 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006604 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006605 path->locks[level] = 0;
6606 }
6607 return 0;
6608 }
6609
6610 /* wc->stage == UPDATE_BACKREF */
6611 if (!(wc->flags[level] & flag)) {
6612 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006613 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006614 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006615 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006616 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006617 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6618 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006619 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006620 wc->flags[level] |= flag;
6621 }
6622
6623 /*
6624 * the block is shared by multiple trees, so it's not good to
6625 * keep the tree lock
6626 */
6627 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006628 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006629 path->locks[level] = 0;
6630 }
6631 return 0;
6632}
6633
6634/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006635 * hepler to process tree block pointer.
6636 *
6637 * when wc->stage == DROP_REFERENCE, this function checks
6638 * reference count of the block pointed to. if the block
6639 * is shared and we need update back refs for the subtree
6640 * rooted at the block, this function changes wc->stage to
6641 * UPDATE_BACKREF. if the block is shared and there is no
6642 * need to update back, this function drops the reference
6643 * to the block.
6644 *
6645 * NOTE: return value 1 means we should stop walking down.
6646 */
6647static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6648 struct btrfs_root *root,
6649 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006650 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006651{
6652 u64 bytenr;
6653 u64 generation;
6654 u64 parent;
6655 u32 blocksize;
6656 struct btrfs_key key;
6657 struct extent_buffer *next;
6658 int level = wc->level;
6659 int reada = 0;
6660 int ret = 0;
6661
6662 generation = btrfs_node_ptr_generation(path->nodes[level],
6663 path->slots[level]);
6664 /*
6665 * if the lower level block was created before the snapshot
6666 * was created, we know there is no need to update back refs
6667 * for the subtree
6668 */
6669 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006670 generation <= root->root_key.offset) {
6671 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006672 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006673 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006674
6675 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6676 blocksize = btrfs_level_size(root, level - 1);
6677
6678 next = btrfs_find_tree_block(root, bytenr, blocksize);
6679 if (!next) {
6680 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00006681 if (!next)
6682 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006683 reada = 1;
6684 }
6685 btrfs_tree_lock(next);
6686 btrfs_set_lock_blocking(next);
6687
Yan, Zheng94fcca92009-10-09 09:25:16 -04006688 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6689 &wc->refs[level - 1],
6690 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006691 if (ret < 0) {
6692 btrfs_tree_unlock(next);
6693 return ret;
6694 }
6695
Yan, Zheng94fcca92009-10-09 09:25:16 -04006696 BUG_ON(wc->refs[level - 1] == 0);
6697 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006698
Yan, Zheng94fcca92009-10-09 09:25:16 -04006699 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006700 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006701 if (level == 1 &&
6702 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6703 goto skip;
6704
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006705 if (!wc->update_ref ||
6706 generation <= root->root_key.offset)
6707 goto skip;
6708
6709 btrfs_node_key_to_cpu(path->nodes[level], &key,
6710 path->slots[level]);
6711 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6712 if (ret < 0)
6713 goto skip;
6714
6715 wc->stage = UPDATE_BACKREF;
6716 wc->shared_level = level - 1;
6717 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006718 } else {
6719 if (level == 1 &&
6720 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6721 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006722 }
6723
Chris Masonb9fab912012-05-06 07:23:47 -04006724 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006725 btrfs_tree_unlock(next);
6726 free_extent_buffer(next);
6727 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006728 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006729 }
6730
6731 if (!next) {
6732 if (reada && level == 1)
6733 reada_walk_down(trans, root, wc, path);
6734 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006735 if (!next)
6736 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006737 btrfs_tree_lock(next);
6738 btrfs_set_lock_blocking(next);
6739 }
6740
6741 level--;
6742 BUG_ON(level != btrfs_header_level(next));
6743 path->nodes[level] = next;
6744 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006745 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006746 wc->level = level;
6747 if (wc->level == 1)
6748 wc->reada_slot = 0;
6749 return 0;
6750skip:
6751 wc->refs[level - 1] = 0;
6752 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006753 if (wc->stage == DROP_REFERENCE) {
6754 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6755 parent = path->nodes[level]->start;
6756 } else {
6757 BUG_ON(root->root_key.objectid !=
6758 btrfs_header_owner(path->nodes[level]));
6759 parent = 0;
6760 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006761
Yan, Zheng94fcca92009-10-09 09:25:16 -04006762 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006763 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006764 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006765 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006766 btrfs_tree_unlock(next);
6767 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006768 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006769 return 1;
6770}
6771
6772/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006773 * hepler to process tree block while walking up the tree.
6774 *
6775 * when wc->stage == DROP_REFERENCE, this function drops
6776 * reference count on the block.
6777 *
6778 * when wc->stage == UPDATE_BACKREF, this function changes
6779 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6780 * to UPDATE_BACKREF previously while processing the block.
6781 *
6782 * NOTE: return value 1 means we should stop walking up.
6783 */
6784static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6785 struct btrfs_root *root,
6786 struct btrfs_path *path,
6787 struct walk_control *wc)
6788{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006789 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006790 int level = wc->level;
6791 struct extent_buffer *eb = path->nodes[level];
6792 u64 parent = 0;
6793
6794 if (wc->stage == UPDATE_BACKREF) {
6795 BUG_ON(wc->shared_level < level);
6796 if (level < wc->shared_level)
6797 goto out;
6798
Yan Zheng2c47e6052009-06-27 21:07:35 -04006799 ret = find_next_key(path, level + 1, &wc->update_progress);
6800 if (ret > 0)
6801 wc->update_ref = 0;
6802
6803 wc->stage = DROP_REFERENCE;
6804 wc->shared_level = -1;
6805 path->slots[level] = 0;
6806
6807 /*
6808 * check reference count again if the block isn't locked.
6809 * we should start walking down the tree again if reference
6810 * count is one.
6811 */
6812 if (!path->locks[level]) {
6813 BUG_ON(level == 0);
6814 btrfs_tree_lock(eb);
6815 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006816 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006817
6818 ret = btrfs_lookup_extent_info(trans, root,
6819 eb->start, eb->len,
6820 &wc->refs[level],
6821 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006822 if (ret < 0) {
6823 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006824 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006825 return ret;
6826 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006827 BUG_ON(wc->refs[level] == 0);
6828 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006829 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006830 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006831 return 1;
6832 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006833 }
6834 }
6835
6836 /* wc->stage == DROP_REFERENCE */
6837 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6838
6839 if (wc->refs[level] == 1) {
6840 if (level == 0) {
6841 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006842 ret = btrfs_dec_ref(trans, root, eb, 1,
6843 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006844 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006845 ret = btrfs_dec_ref(trans, root, eb, 0,
6846 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006847 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006848 }
6849 /* make block locked assertion in clean_tree_block happy */
6850 if (!path->locks[level] &&
6851 btrfs_header_generation(eb) == trans->transid) {
6852 btrfs_tree_lock(eb);
6853 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006854 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006855 }
6856 clean_tree_block(trans, root, eb);
6857 }
6858
6859 if (eb == root->node) {
6860 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6861 parent = eb->start;
6862 else
6863 BUG_ON(root->root_key.objectid !=
6864 btrfs_header_owner(eb));
6865 } else {
6866 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6867 parent = path->nodes[level + 1]->start;
6868 else
6869 BUG_ON(root->root_key.objectid !=
6870 btrfs_header_owner(path->nodes[level + 1]));
6871 }
6872
Jan Schmidt5581a512012-05-16 17:04:52 +02006873 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006874out:
6875 wc->refs[level] = 0;
6876 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006877 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006878}
6879
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006880static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6881 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006882 struct btrfs_path *path,
6883 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006884{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006885 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006886 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006887 int ret;
6888
Yan Zheng2c47e6052009-06-27 21:07:35 -04006889 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006890 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006891 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006892 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006893
Yan Zheng2c47e6052009-06-27 21:07:35 -04006894 if (level == 0)
6895 break;
6896
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006897 if (path->slots[level] >=
6898 btrfs_header_nritems(path->nodes[level]))
6899 break;
6900
Yan, Zheng94fcca92009-10-09 09:25:16 -04006901 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006902 if (ret > 0) {
6903 path->slots[level]++;
6904 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00006905 } else if (ret < 0)
6906 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006907 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006908 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006909 return 0;
6910}
6911
Chris Masond3977122009-01-05 21:25:51 -05006912static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006913 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006914 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006915 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006916{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006917 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006918 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006919
Yan Zheng2c47e6052009-06-27 21:07:35 -04006920 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6921 while (level < max_level && path->nodes[level]) {
6922 wc->level = level;
6923 if (path->slots[level] + 1 <
6924 btrfs_header_nritems(path->nodes[level])) {
6925 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006926 return 0;
6927 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006928 ret = walk_up_proc(trans, root, path, wc);
6929 if (ret > 0)
6930 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006931
Yan Zheng2c47e6052009-06-27 21:07:35 -04006932 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006933 btrfs_tree_unlock_rw(path->nodes[level],
6934 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006935 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006936 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006937 free_extent_buffer(path->nodes[level]);
6938 path->nodes[level] = NULL;
6939 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006940 }
6941 }
6942 return 1;
6943}
6944
Chris Mason9aca1d52007-03-13 11:09:37 -04006945/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006946 * drop a subvolume tree.
6947 *
6948 * this function traverses the tree freeing any blocks that only
6949 * referenced by the tree.
6950 *
6951 * when a shared tree block is found. this function decreases its
6952 * reference count by one. if update_ref is true, this function
6953 * also make sure backrefs for the shared block and all lower level
6954 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006955 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04006956int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006957 struct btrfs_block_rsv *block_rsv, int update_ref,
6958 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05006959{
Chris Mason5caf2a02007-04-02 11:20:42 -04006960 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006961 struct btrfs_trans_handle *trans;
6962 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006963 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006964 struct walk_control *wc;
6965 struct btrfs_key key;
6966 int err = 0;
6967 int ret;
6968 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006969
Chris Mason5caf2a02007-04-02 11:20:42 -04006970 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006971 if (!path) {
6972 err = -ENOMEM;
6973 goto out;
6974 }
Chris Mason20524f02007-03-10 06:35:47 -05006975
Yan Zheng2c47e6052009-06-27 21:07:35 -04006976 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07006977 if (!wc) {
6978 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006979 err = -ENOMEM;
6980 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07006981 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006982
Yan, Zhenga22285a2010-05-16 10:48:46 -04006983 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006984 if (IS_ERR(trans)) {
6985 err = PTR_ERR(trans);
6986 goto out_free;
6987 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006988
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006989 if (block_rsv)
6990 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006991
Chris Mason9f3a7422007-08-07 15:52:19 -04006992 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006993 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006994 path->nodes[level] = btrfs_lock_root_node(root);
6995 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006996 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006997 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006998 memset(&wc->update_progress, 0,
6999 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007000 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007001 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007002 memcpy(&wc->update_progress, &key,
7003 sizeof(wc->update_progress));
7004
Chris Mason6702ed42007-08-07 16:15:09 -04007005 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007006 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007007 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007008 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7009 path->lowest_level = 0;
7010 if (ret < 0) {
7011 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007012 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007013 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007014 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007015
Chris Mason7d9eb122008-07-08 14:19:17 -04007016 /*
7017 * unlock our path, this is safe because only this
7018 * function is allowed to delete this snapshot
7019 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007020 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007021
Yan Zheng2c47e6052009-06-27 21:07:35 -04007022 level = btrfs_header_level(root->node);
7023 while (1) {
7024 btrfs_tree_lock(path->nodes[level]);
7025 btrfs_set_lock_blocking(path->nodes[level]);
7026
7027 ret = btrfs_lookup_extent_info(trans, root,
7028 path->nodes[level]->start,
7029 path->nodes[level]->len,
7030 &wc->refs[level],
7031 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007032 if (ret < 0) {
7033 err = ret;
7034 goto out_end_trans;
7035 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007036 BUG_ON(wc->refs[level] == 0);
7037
7038 if (level == root_item->drop_level)
7039 break;
7040
7041 btrfs_tree_unlock(path->nodes[level]);
7042 WARN_ON(wc->refs[level] != 1);
7043 level--;
7044 }
7045 }
7046
7047 wc->level = level;
7048 wc->shared_level = -1;
7049 wc->stage = DROP_REFERENCE;
7050 wc->update_ref = update_ref;
7051 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007052 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007053 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007054
7055 while (1) {
7056 ret = walk_down_tree(trans, root, path, wc);
7057 if (ret < 0) {
7058 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007059 break;
7060 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007061
7062 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7063 if (ret < 0) {
7064 err = ret;
7065 break;
7066 }
7067
7068 if (ret > 0) {
7069 BUG_ON(wc->stage != DROP_REFERENCE);
7070 break;
7071 }
7072
7073 if (wc->stage == DROP_REFERENCE) {
7074 level = wc->level;
7075 btrfs_node_key(path->nodes[level],
7076 &root_item->drop_progress,
7077 path->slots[level]);
7078 root_item->drop_level = level;
7079 }
7080
7081 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007082 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007083 ret = btrfs_update_root(trans, tree_root,
7084 &root->root_key,
7085 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007086 if (ret) {
7087 btrfs_abort_transaction(trans, tree_root, ret);
7088 err = ret;
7089 goto out_end_trans;
7090 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007091
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007092 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007093 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007094 if (IS_ERR(trans)) {
7095 err = PTR_ERR(trans);
7096 goto out_free;
7097 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007098 if (block_rsv)
7099 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007100 }
Chris Mason20524f02007-03-10 06:35:47 -05007101 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007102 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007103 if (err)
7104 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007105
7106 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007107 if (ret) {
7108 btrfs_abort_transaction(trans, tree_root, ret);
7109 goto out_end_trans;
7110 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007111
Yan, Zheng76dda932009-09-21 16:00:26 -04007112 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7113 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7114 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007115 if (ret < 0) {
7116 btrfs_abort_transaction(trans, tree_root, ret);
7117 err = ret;
7118 goto out_end_trans;
7119 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007120 /* if we fail to delete the orphan item this time
7121 * around, it'll get picked up the next time.
7122 *
7123 * The most common failure here is just -ENOENT.
7124 */
7125 btrfs_del_orphan_item(trans, tree_root,
7126 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007127 }
7128 }
7129
7130 if (root->in_radix) {
7131 btrfs_free_fs_root(tree_root->fs_info, root);
7132 } else {
7133 free_extent_buffer(root->node);
7134 free_extent_buffer(root->commit_root);
7135 kfree(root);
7136 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007137out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007138 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007139out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007140 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007141 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007142out:
7143 if (err)
7144 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007145 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007146}
Chris Mason9078a3e2007-04-26 16:46:15 -04007147
Yan Zheng2c47e6052009-06-27 21:07:35 -04007148/*
7149 * drop subtree rooted at tree block 'node'.
7150 *
7151 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007152 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007153 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007154int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7155 struct btrfs_root *root,
7156 struct extent_buffer *node,
7157 struct extent_buffer *parent)
7158{
7159 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007160 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007161 int level;
7162 int parent_level;
7163 int ret = 0;
7164 int wret;
7165
Yan Zheng2c47e6052009-06-27 21:07:35 -04007166 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7167
Yan Zhengf82d02d2008-10-29 14:49:05 -04007168 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007169 if (!path)
7170 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007171
Yan Zheng2c47e6052009-06-27 21:07:35 -04007172 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007173 if (!wc) {
7174 btrfs_free_path(path);
7175 return -ENOMEM;
7176 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007177
Chris Masonb9447ef2009-03-09 11:45:38 -04007178 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007179 parent_level = btrfs_header_level(parent);
7180 extent_buffer_get(parent);
7181 path->nodes[parent_level] = parent;
7182 path->slots[parent_level] = btrfs_header_nritems(parent);
7183
Chris Masonb9447ef2009-03-09 11:45:38 -04007184 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007185 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007186 path->nodes[level] = node;
7187 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007188 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007189
7190 wc->refs[parent_level] = 1;
7191 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7192 wc->level = level;
7193 wc->shared_level = -1;
7194 wc->stage = DROP_REFERENCE;
7195 wc->update_ref = 0;
7196 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007197 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007198 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007199
7200 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007201 wret = walk_down_tree(trans, root, path, wc);
7202 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007203 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007204 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007205 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007206
Yan Zheng2c47e6052009-06-27 21:07:35 -04007207 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007208 if (wret < 0)
7209 ret = wret;
7210 if (wret != 0)
7211 break;
7212 }
7213
Yan Zheng2c47e6052009-06-27 21:07:35 -04007214 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007215 btrfs_free_path(path);
7216 return ret;
7217}
7218
Chris Masonec44a352008-04-28 15:29:52 -04007219static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7220{
7221 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007222 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007223
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007224 /*
7225 * if restripe for this chunk_type is on pick target profile and
7226 * return, otherwise do the usual balance
7227 */
7228 stripped = get_restripe_target(root->fs_info, flags);
7229 if (stripped)
7230 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007231
Chris Masoncd02dca2010-12-13 14:56:23 -05007232 /*
7233 * we add in the count of missing devices because we want
7234 * to make sure that any RAID levels on a degraded FS
7235 * continue to be honored.
7236 */
7237 num_devices = root->fs_info->fs_devices->rw_devices +
7238 root->fs_info->fs_devices->missing_devices;
7239
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007240 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7241 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7242
Chris Masonec44a352008-04-28 15:29:52 -04007243 if (num_devices == 1) {
7244 stripped |= BTRFS_BLOCK_GROUP_DUP;
7245 stripped = flags & ~stripped;
7246
7247 /* turn raid0 into single device chunks */
7248 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7249 return stripped;
7250
7251 /* turn mirroring into duplication */
7252 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7253 BTRFS_BLOCK_GROUP_RAID10))
7254 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007255 } else {
7256 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007257 if (flags & stripped)
7258 return flags;
7259
7260 stripped |= BTRFS_BLOCK_GROUP_DUP;
7261 stripped = flags & ~stripped;
7262
7263 /* switch duplicated blocks with raid1 */
7264 if (flags & BTRFS_BLOCK_GROUP_DUP)
7265 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7266
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007267 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007268 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007269
Chris Masonec44a352008-04-28 15:29:52 -04007270 return flags;
7271}
7272
Miao Xie199c36e2011-07-15 10:34:36 +00007273static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007274{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007275 struct btrfs_space_info *sinfo = cache->space_info;
7276 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007277 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007278 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007279
Chris Masonc286ac42008-07-22 23:06:41 -04007280
Miao Xie199c36e2011-07-15 10:34:36 +00007281 /*
7282 * We need some metadata space and system metadata space for
7283 * allocating chunks in some corner cases until we force to set
7284 * it to be readonly.
7285 */
7286 if ((sinfo->flags &
7287 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7288 !force)
7289 min_allocable_bytes = 1 * 1024 * 1024;
7290 else
7291 min_allocable_bytes = 0;
7292
Yan, Zhengf0486c62010-05-16 10:46:25 -04007293 spin_lock(&sinfo->lock);
7294 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007295
7296 if (cache->ro) {
7297 ret = 0;
7298 goto out;
7299 }
7300
Yan, Zhengf0486c62010-05-16 10:46:25 -04007301 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7302 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007303
Yan, Zhengf0486c62010-05-16 10:46:25 -04007304 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007305 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7306 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007307 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007308 cache->ro = 1;
7309 ret = 0;
7310 }
WuBo61cfea92011-07-26 03:30:11 +00007311out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007312 spin_unlock(&cache->lock);
7313 spin_unlock(&sinfo->lock);
7314 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007315}
7316
Yan, Zhengf0486c62010-05-16 10:46:25 -04007317int btrfs_set_block_group_ro(struct btrfs_root *root,
7318 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007319
7320{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007321 struct btrfs_trans_handle *trans;
7322 u64 alloc_flags;
7323 int ret;
7324
7325 BUG_ON(cache->ro);
7326
Josef Bacik7a7eaa402011-04-13 12:54:33 -04007327 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007328 if (IS_ERR(trans))
7329 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007330
7331 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007332 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007333 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007334 CHUNK_ALLOC_FORCE);
7335 if (ret < 0)
7336 goto out;
7337 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007338
Miao Xie199c36e2011-07-15 10:34:36 +00007339 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007340 if (!ret)
7341 goto out;
7342 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007343 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007344 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007345 if (ret < 0)
7346 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007347 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007348out:
7349 btrfs_end_transaction(trans, root);
7350 return ret;
7351}
7352
Chris Masonc87f08c2011-02-16 13:57:04 -05007353int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7354 struct btrfs_root *root, u64 type)
7355{
7356 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007357 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007358 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007359}
7360
Miao Xie6d07bce2011-01-05 10:07:31 +00007361/*
7362 * helper to account the unused space of all the readonly block group in the
7363 * list. takes mirrors into account.
7364 */
7365static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7366{
7367 struct btrfs_block_group_cache *block_group;
7368 u64 free_bytes = 0;
7369 int factor;
7370
7371 list_for_each_entry(block_group, groups_list, list) {
7372 spin_lock(&block_group->lock);
7373
7374 if (!block_group->ro) {
7375 spin_unlock(&block_group->lock);
7376 continue;
7377 }
7378
7379 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7380 BTRFS_BLOCK_GROUP_RAID10 |
7381 BTRFS_BLOCK_GROUP_DUP))
7382 factor = 2;
7383 else
7384 factor = 1;
7385
7386 free_bytes += (block_group->key.offset -
7387 btrfs_block_group_used(&block_group->item)) *
7388 factor;
7389
7390 spin_unlock(&block_group->lock);
7391 }
7392
7393 return free_bytes;
7394}
7395
7396/*
7397 * helper to account the unused space of all the readonly block group in the
7398 * space_info. takes mirrors into account.
7399 */
7400u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7401{
7402 int i;
7403 u64 free_bytes = 0;
7404
7405 spin_lock(&sinfo->lock);
7406
7407 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7408 if (!list_empty(&sinfo->block_groups[i]))
7409 free_bytes += __btrfs_get_ro_block_group_free_space(
7410 &sinfo->block_groups[i]);
7411
7412 spin_unlock(&sinfo->lock);
7413
7414 return free_bytes;
7415}
7416
Jeff Mahoney143bede2012-03-01 14:56:26 +01007417void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007418 struct btrfs_block_group_cache *cache)
7419{
7420 struct btrfs_space_info *sinfo = cache->space_info;
7421 u64 num_bytes;
7422
7423 BUG_ON(!cache->ro);
7424
7425 spin_lock(&sinfo->lock);
7426 spin_lock(&cache->lock);
7427 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7428 cache->bytes_super - btrfs_block_group_used(&cache->item);
7429 sinfo->bytes_readonly -= num_bytes;
7430 cache->ro = 0;
7431 spin_unlock(&cache->lock);
7432 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007433}
7434
Josef Bacikba1bf482009-09-11 16:11:19 -04007435/*
7436 * checks to see if its even possible to relocate this block group.
7437 *
7438 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7439 * ok to go ahead and try.
7440 */
7441int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007442{
Zheng Yan1a40e232008-09-26 10:09:34 -04007443 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007444 struct btrfs_space_info *space_info;
7445 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7446 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007447 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007448 u64 dev_min = 1;
7449 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007450 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007451 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007452 int full = 0;
7453 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007454
Josef Bacikba1bf482009-09-11 16:11:19 -04007455 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007456
Josef Bacikba1bf482009-09-11 16:11:19 -04007457 /* odd, couldn't find the block group, leave it alone */
7458 if (!block_group)
7459 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007460
liubocdcb7252011-08-03 10:15:25 +00007461 min_free = btrfs_block_group_used(&block_group->item);
7462
Josef Bacikba1bf482009-09-11 16:11:19 -04007463 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007464 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007465 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007466
Josef Bacikba1bf482009-09-11 16:11:19 -04007467 space_info = block_group->space_info;
7468 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007469
Josef Bacikba1bf482009-09-11 16:11:19 -04007470 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007471
Josef Bacikba1bf482009-09-11 16:11:19 -04007472 /*
7473 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007474 * relocate it unless we're able to allocate a new chunk below.
7475 *
7476 * Otherwise, we need to make sure we have room in the space to handle
7477 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007478 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007479 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007480 (space_info->bytes_used + space_info->bytes_reserved +
7481 space_info->bytes_pinned + space_info->bytes_readonly +
7482 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007483 spin_unlock(&space_info->lock);
7484 goto out;
7485 }
7486 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007487
Josef Bacikba1bf482009-09-11 16:11:19 -04007488 /*
7489 * ok we don't have enough space, but maybe we have free space on our
7490 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007491 * alloc devices and guess if we have enough space. if this block
7492 * group is going to be restriped, run checks against the target
7493 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007494 */
7495 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007496
liubocdcb7252011-08-03 10:15:25 +00007497 /*
7498 * index:
7499 * 0: raid10
7500 * 1: raid1
7501 * 2: dup
7502 * 3: raid0
7503 * 4: single
7504 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007505 target = get_restripe_target(root->fs_info, block_group->flags);
7506 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007507 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007508 } else {
7509 /*
7510 * this is just a balance, so if we were marked as full
7511 * we know there is no space for a new chunk
7512 */
7513 if (full)
7514 goto out;
7515
7516 index = get_block_group_index(block_group);
7517 }
7518
Miao Xiee6ec7162013-01-17 05:38:51 +00007519 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00007520 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007521 /* Divide by 2 */
7522 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007523 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00007524 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00007525 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04007526 /* Multiply by 2 */
7527 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007528 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00007529 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007530 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007531 }
7532
Josef Bacikba1bf482009-09-11 16:11:19 -04007533 mutex_lock(&root->fs_info->chunk_mutex);
7534 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007535 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007536
Josef Bacikba1bf482009-09-11 16:11:19 -04007537 /*
7538 * check to make sure we can actually find a chunk with enough
7539 * space to fit our block group in.
7540 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007541 if (device->total_bytes > device->bytes_used + min_free &&
7542 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007543 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007544 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007545 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007546 dev_nr++;
7547
7548 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007549 break;
liubocdcb7252011-08-03 10:15:25 +00007550
Josef Bacikba1bf482009-09-11 16:11:19 -04007551 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007552 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007553 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007554 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007555out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007556 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007557 return ret;
7558}
7559
Christoph Hellwigb2950862008-12-02 09:54:17 -05007560static int find_first_block_group(struct btrfs_root *root,
7561 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007562{
Chris Mason925baed2008-06-25 16:01:30 -04007563 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007564 struct btrfs_key found_key;
7565 struct extent_buffer *leaf;
7566 int slot;
7567
7568 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7569 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007570 goto out;
7571
Chris Masond3977122009-01-05 21:25:51 -05007572 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007573 slot = path->slots[0];
7574 leaf = path->nodes[0];
7575 if (slot >= btrfs_header_nritems(leaf)) {
7576 ret = btrfs_next_leaf(root, path);
7577 if (ret == 0)
7578 continue;
7579 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007580 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007581 break;
7582 }
7583 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7584
7585 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007586 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7587 ret = 0;
7588 goto out;
7589 }
Chris Mason0b86a832008-03-24 15:01:56 -04007590 path->slots[0]++;
7591 }
Chris Mason925baed2008-06-25 16:01:30 -04007592out:
Chris Mason0b86a832008-03-24 15:01:56 -04007593 return ret;
7594}
7595
Josef Bacik0af3d002010-06-21 14:48:16 -04007596void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7597{
7598 struct btrfs_block_group_cache *block_group;
7599 u64 last = 0;
7600
7601 while (1) {
7602 struct inode *inode;
7603
7604 block_group = btrfs_lookup_first_block_group(info, last);
7605 while (block_group) {
7606 spin_lock(&block_group->lock);
7607 if (block_group->iref)
7608 break;
7609 spin_unlock(&block_group->lock);
7610 block_group = next_block_group(info->tree_root,
7611 block_group);
7612 }
7613 if (!block_group) {
7614 if (last == 0)
7615 break;
7616 last = 0;
7617 continue;
7618 }
7619
7620 inode = block_group->inode;
7621 block_group->iref = 0;
7622 block_group->inode = NULL;
7623 spin_unlock(&block_group->lock);
7624 iput(inode);
7625 last = block_group->key.objectid + block_group->key.offset;
7626 btrfs_put_block_group(block_group);
7627 }
7628}
7629
Zheng Yan1a40e232008-09-26 10:09:34 -04007630int btrfs_free_block_groups(struct btrfs_fs_info *info)
7631{
7632 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007633 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007634 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007635 struct rb_node *n;
7636
Yan Zheng11833d62009-09-11 16:11:19 -04007637 down_write(&info->extent_commit_sem);
7638 while (!list_empty(&info->caching_block_groups)) {
7639 caching_ctl = list_entry(info->caching_block_groups.next,
7640 struct btrfs_caching_control, list);
7641 list_del(&caching_ctl->list);
7642 put_caching_control(caching_ctl);
7643 }
7644 up_write(&info->extent_commit_sem);
7645
Zheng Yan1a40e232008-09-26 10:09:34 -04007646 spin_lock(&info->block_group_cache_lock);
7647 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7648 block_group = rb_entry(n, struct btrfs_block_group_cache,
7649 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007650 rb_erase(&block_group->cache_node,
7651 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007652 spin_unlock(&info->block_group_cache_lock);
7653
Josef Bacik80eb2342008-10-29 14:49:05 -04007654 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007655 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007656 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007657
Josef Bacik817d52f2009-07-13 21:29:25 -04007658 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007659 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007660
Josef Bacik3c148742011-02-02 15:53:47 +00007661 /*
7662 * We haven't cached this block group, which means we could
7663 * possibly have excluded extents on this block group.
7664 */
7665 if (block_group->cached == BTRFS_CACHE_NO)
7666 free_excluded_extents(info->extent_root, block_group);
7667
Josef Bacik817d52f2009-07-13 21:29:25 -04007668 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007669 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007670
7671 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007672 }
7673 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007674
7675 /* now that all the block groups are freed, go through and
7676 * free all the space_info structs. This is only called during
7677 * the final stages of unmount, and so we know nobody is
7678 * using them. We call synchronize_rcu() once before we start,
7679 * just to be on the safe side.
7680 */
7681 synchronize_rcu();
7682
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007683 release_global_block_rsv(info);
7684
Chris Mason4184ea72009-03-10 12:39:20 -04007685 while(!list_empty(&info->space_info)) {
7686 space_info = list_entry(info->space_info.next,
7687 struct btrfs_space_info,
7688 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007689 if (space_info->bytes_pinned > 0 ||
Josef Bacikfb25e912011-07-26 17:00:46 -04007690 space_info->bytes_reserved > 0 ||
7691 space_info->bytes_may_use > 0) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007692 WARN_ON(1);
7693 dump_space_info(space_info, 0, 0);
7694 }
Chris Mason4184ea72009-03-10 12:39:20 -04007695 list_del(&space_info->list);
7696 kfree(space_info);
7697 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007698 return 0;
7699}
7700
Yan, Zhengb742bb82010-05-16 10:46:24 -04007701static void __link_block_group(struct btrfs_space_info *space_info,
7702 struct btrfs_block_group_cache *cache)
7703{
7704 int index = get_block_group_index(cache);
7705
7706 down_write(&space_info->groups_sem);
7707 list_add_tail(&cache->list, &space_info->block_groups[index]);
7708 up_write(&space_info->groups_sem);
7709}
7710
Chris Mason9078a3e2007-04-26 16:46:15 -04007711int btrfs_read_block_groups(struct btrfs_root *root)
7712{
7713 struct btrfs_path *path;
7714 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007715 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007716 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007717 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007718 struct btrfs_key key;
7719 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007720 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007721 int need_clear = 0;
7722 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007723
Chris Masonbe744172007-05-06 10:15:01 -04007724 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007725 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007726 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007727 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007728 path = btrfs_alloc_path();
7729 if (!path)
7730 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007731 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007732
David Sterba6c417612011-04-13 15:41:04 +02007733 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007734 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02007735 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04007736 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007737 if (btrfs_test_opt(root, CLEAR_CACHE))
7738 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007739
Chris Masond3977122009-01-05 21:25:51 -05007740 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007741 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007742 if (ret > 0)
7743 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007744 if (ret != 0)
7745 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007746 leaf = path->nodes[0];
7747 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007748 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007749 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007750 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007751 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007752 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007753 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7754 GFP_NOFS);
7755 if (!cache->free_space_ctl) {
7756 kfree(cache);
7757 ret = -ENOMEM;
7758 goto error;
7759 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007760
Yan Zhengd2fb3432008-12-11 16:30:39 -05007761 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007762 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007763 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007764 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007765 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007766
Liu Bocf7c1ef2012-07-06 03:31:34 -06007767 if (need_clear) {
7768 /*
7769 * When we mount with old space cache, we need to
7770 * set BTRFS_DC_CLEAR and set dirty flag.
7771 *
7772 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7773 * truncate the old free space cache inode and
7774 * setup a new one.
7775 * b) Setting 'dirty flag' makes sure that we flush
7776 * the new space cache info onto disk.
7777 */
Josef Bacik0af3d002010-06-21 14:48:16 -04007778 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007779 if (btrfs_test_opt(root, SPACE_CACHE))
7780 cache->dirty = 1;
7781 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007782
Chris Mason5f39d392007-10-15 16:14:19 -04007783 read_extent_buffer(leaf, &cache->item,
7784 btrfs_item_ptr_offset(leaf, path->slots[0]),
7785 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007786 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007787
Chris Mason9078a3e2007-04-26 16:46:15 -04007788 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007789 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007790 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007791 cache->sectorsize = root->sectorsize;
7792
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007793 btrfs_init_free_space_ctl(cache);
7794
Josef Bacik817d52f2009-07-13 21:29:25 -04007795 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007796 * We need to exclude the super stripes now so that the space
7797 * info has super bytes accounted for, otherwise we'll think
7798 * we have more space than we actually do.
7799 */
7800 exclude_super_stripes(root, cache);
7801
7802 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007803 * check for two cases, either we are full, and therefore
7804 * don't need to bother with the caching work since we won't
7805 * find any space, or we are empty, and we can just add all
7806 * the space in and be done with it. This saves us _alot_ of
7807 * time, particularly in the full case.
7808 */
7809 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007810 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007811 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007812 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007813 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007814 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007815 cache->cached = BTRFS_CACHE_FINISHED;
7816 add_new_free_space(cache, root->fs_info,
7817 found_key.objectid,
7818 found_key.objectid +
7819 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007820 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007821 }
Chris Mason96b51792007-10-15 16:15:19 -04007822
Chris Mason6324fbf2008-03-24 15:01:59 -04007823 ret = update_space_info(info, cache->flags, found_key.offset,
7824 btrfs_block_group_used(&cache->item),
7825 &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007826 BUG_ON(ret); /* -ENOMEM */
Chris Mason6324fbf2008-03-24 15:01:59 -04007827 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007828 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007829 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007830 spin_unlock(&cache->space_info->lock);
7831
Yan, Zhengb742bb82010-05-16 10:46:24 -04007832 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007833
Josef Bacik0f9dd462008-09-23 13:14:11 -04007834 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007835 BUG_ON(ret); /* Logic error */
Chris Mason75ccf472008-09-30 19:24:06 -04007836
7837 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007838 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007839 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007840 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007841
7842 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7843 if (!(get_alloc_profile(root, space_info->flags) &
7844 (BTRFS_BLOCK_GROUP_RAID10 |
7845 BTRFS_BLOCK_GROUP_RAID1 |
7846 BTRFS_BLOCK_GROUP_DUP)))
7847 continue;
7848 /*
7849 * avoid allocating from un-mirrored block group if there are
7850 * mirrored block groups.
7851 */
7852 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007853 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007854 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007855 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007856 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007857
7858 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007859 ret = 0;
7860error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007861 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007862 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007863}
Chris Mason6324fbf2008-03-24 15:01:59 -04007864
Josef Bacikea658ba2012-09-11 16:57:25 -04007865void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
7866 struct btrfs_root *root)
7867{
7868 struct btrfs_block_group_cache *block_group, *tmp;
7869 struct btrfs_root *extent_root = root->fs_info->extent_root;
7870 struct btrfs_block_group_item item;
7871 struct btrfs_key key;
7872 int ret = 0;
7873
7874 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
7875 new_bg_list) {
7876 list_del_init(&block_group->new_bg_list);
7877
7878 if (ret)
7879 continue;
7880
7881 spin_lock(&block_group->lock);
7882 memcpy(&item, &block_group->item, sizeof(item));
7883 memcpy(&key, &block_group->key, sizeof(key));
7884 spin_unlock(&block_group->lock);
7885
7886 ret = btrfs_insert_item(trans, extent_root, &key, &item,
7887 sizeof(item));
7888 if (ret)
7889 btrfs_abort_transaction(trans, extent_root, ret);
7890 }
7891}
7892
Chris Mason6324fbf2008-03-24 15:01:59 -04007893int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7894 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007895 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007896 u64 size)
7897{
7898 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007899 struct btrfs_root *extent_root;
7900 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007901
7902 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007903
Chris Mason12fcfd22009-03-24 10:24:20 -04007904 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007905
Chris Mason8f18cf12008-04-25 16:53:30 -04007906 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007907 if (!cache)
7908 return -ENOMEM;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007909 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7910 GFP_NOFS);
7911 if (!cache->free_space_ctl) {
7912 kfree(cache);
7913 return -ENOMEM;
7914 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007915
Chris Masone17cade2008-04-15 15:41:47 -04007916 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007917 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007918 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007919 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007920 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007921
Yan Zhengd2fb3432008-12-11 16:30:39 -05007922 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007923 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007924 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04007925 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04007926 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007927
Li Zefan34d52cb6c2011-03-29 13:46:06 +08007928 btrfs_init_free_space_ctl(cache);
7929
Chris Mason6324fbf2008-03-24 15:01:59 -04007930 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007931 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7932 cache->flags = type;
7933 btrfs_set_block_group_flags(&cache->item, type);
7934
Yan Zheng11833d62009-09-11 16:11:19 -04007935 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007936 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007937 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007938
Josef Bacik817d52f2009-07-13 21:29:25 -04007939 add_new_free_space(cache, root->fs_info, chunk_offset,
7940 chunk_offset + size);
7941
Yan Zheng11833d62009-09-11 16:11:19 -04007942 free_excluded_extents(root, cache);
7943
Chris Mason6324fbf2008-03-24 15:01:59 -04007944 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7945 &cache->space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007946 BUG_ON(ret); /* -ENOMEM */
Li Zefanc7c144d2011-12-07 10:39:22 +08007947 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04007948
7949 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007950 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007951 spin_unlock(&cache->space_info->lock);
7952
Yan, Zhengb742bb82010-05-16 10:46:24 -04007953 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007954
Josef Bacik0f9dd462008-09-23 13:14:11 -04007955 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007956 BUG_ON(ret); /* Logic error */
Chris Masonc286ac42008-07-22 23:06:41 -04007957
Josef Bacikea658ba2012-09-11 16:57:25 -04007958 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04007959
Chris Masond18a2c42008-04-04 15:40:00 -04007960 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007961
Chris Mason6324fbf2008-03-24 15:01:59 -04007962 return 0;
7963}
Zheng Yan1a40e232008-09-26 10:09:34 -04007964
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007965static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
7966{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03007967 u64 extra_flags = chunk_to_extended(flags) &
7968 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007969
7970 if (flags & BTRFS_BLOCK_GROUP_DATA)
7971 fs_info->avail_data_alloc_bits &= ~extra_flags;
7972 if (flags & BTRFS_BLOCK_GROUP_METADATA)
7973 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
7974 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
7975 fs_info->avail_system_alloc_bits &= ~extra_flags;
7976}
7977
Zheng Yan1a40e232008-09-26 10:09:34 -04007978int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7979 struct btrfs_root *root, u64 group_start)
7980{
7981 struct btrfs_path *path;
7982 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007983 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007984 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007985 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007986 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007987 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007988 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04007989 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007990
Zheng Yan1a40e232008-09-26 10:09:34 -04007991 root = root->fs_info->extent_root;
7992
7993 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7994 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007995 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007996
liubo9f7c43c2011-03-07 02:13:33 +00007997 /*
7998 * Free the reserved super bytes from this block group before
7999 * remove it.
8000 */
8001 free_excluded_extents(root, block_group);
8002
Zheng Yan1a40e232008-09-26 10:09:34 -04008003 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008004 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008005 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8006 BTRFS_BLOCK_GROUP_RAID1 |
8007 BTRFS_BLOCK_GROUP_RAID10))
8008 factor = 2;
8009 else
8010 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008011
Chris Mason44fb5512009-06-04 15:34:51 -04008012 /* make sure this block group isn't part of an allocation cluster */
8013 cluster = &root->fs_info->data_alloc_cluster;
8014 spin_lock(&cluster->refill_lock);
8015 btrfs_return_cluster_to_free_space(block_group, cluster);
8016 spin_unlock(&cluster->refill_lock);
8017
8018 /*
8019 * make sure this block group isn't part of a metadata
8020 * allocation cluster
8021 */
8022 cluster = &root->fs_info->meta_alloc_cluster;
8023 spin_lock(&cluster->refill_lock);
8024 btrfs_return_cluster_to_free_space(block_group, cluster);
8025 spin_unlock(&cluster->refill_lock);
8026
Zheng Yan1a40e232008-09-26 10:09:34 -04008027 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008028 if (!path) {
8029 ret = -ENOMEM;
8030 goto out;
8031 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008032
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008033 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008034 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008035 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008036 if (ret) {
8037 btrfs_add_delayed_iput(inode);
8038 goto out;
8039 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008040 clear_nlink(inode);
8041 /* One for the block groups ref */
8042 spin_lock(&block_group->lock);
8043 if (block_group->iref) {
8044 block_group->iref = 0;
8045 block_group->inode = NULL;
8046 spin_unlock(&block_group->lock);
8047 iput(inode);
8048 } else {
8049 spin_unlock(&block_group->lock);
8050 }
8051 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008052 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008053 }
8054
8055 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8056 key.offset = block_group->key.objectid;
8057 key.type = 0;
8058
8059 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8060 if (ret < 0)
8061 goto out;
8062 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008063 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008064 if (ret == 0) {
8065 ret = btrfs_del_item(trans, tree_root, path);
8066 if (ret)
8067 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008068 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008069 }
8070
Yan Zheng3dfdb932009-01-21 10:49:16 -05008071 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008072 rb_erase(&block_group->cache_node,
8073 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008074
8075 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8076 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008077 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008078
Josef Bacik80eb2342008-10-29 14:49:05 -04008079 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008080 /*
8081 * we must use list_del_init so people can check to see if they
8082 * are still on the list after taking the semaphore
8083 */
8084 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008085 if (list_empty(&block_group->space_info->block_groups[index]))
8086 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008087 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008088
Josef Bacik817d52f2009-07-13 21:29:25 -04008089 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008090 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008091
8092 btrfs_remove_free_space_cache(block_group);
8093
Yan Zhengc146afa2008-11-12 14:34:12 -05008094 spin_lock(&block_group->space_info->lock);
8095 block_group->space_info->total_bytes -= block_group->key.offset;
8096 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008097 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008098 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008099
Josef Bacik0af3d002010-06-21 14:48:16 -04008100 memcpy(&key, &block_group->key, sizeof(key));
8101
Chris Mason283bb192009-07-24 16:30:55 -04008102 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008103
Chris Masonfa9c0d72009-04-03 09:47:43 -04008104 btrfs_put_block_group(block_group);
8105 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008106
8107 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8108 if (ret > 0)
8109 ret = -EIO;
8110 if (ret < 0)
8111 goto out;
8112
8113 ret = btrfs_del_item(trans, root, path);
8114out:
8115 btrfs_free_path(path);
8116 return ret;
8117}
liuboacce9522011-01-06 19:30:25 +08008118
liuboc59021f2011-03-07 02:13:14 +00008119int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8120{
8121 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008122 struct btrfs_super_block *disk_super;
8123 u64 features;
8124 u64 flags;
8125 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008126 int ret;
8127
David Sterba6c417612011-04-13 15:41:04 +02008128 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008129 if (!btrfs_super_root(disk_super))
8130 return 1;
liuboc59021f2011-03-07 02:13:14 +00008131
liubo1aba86d2011-04-08 08:44:37 +00008132 features = btrfs_super_incompat_flags(disk_super);
8133 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8134 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008135
liubo1aba86d2011-04-08 08:44:37 +00008136 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8137 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008138 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008139 goto out;
liuboc59021f2011-03-07 02:13:14 +00008140
liubo1aba86d2011-04-08 08:44:37 +00008141 if (mixed) {
8142 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8143 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8144 } else {
8145 flags = BTRFS_BLOCK_GROUP_METADATA;
8146 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8147 if (ret)
8148 goto out;
8149
8150 flags = BTRFS_BLOCK_GROUP_DATA;
8151 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8152 }
8153out:
liuboc59021f2011-03-07 02:13:14 +00008154 return ret;
8155}
8156
liuboacce9522011-01-06 19:30:25 +08008157int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8158{
8159 return unpin_extent_range(root, start, end);
8160}
8161
8162int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008163 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008164{
Li Dongyang5378e602011-03-24 10:24:27 +00008165 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008166}
Li Dongyangf7039b12011-03-24 10:24:28 +00008167
8168int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8169{
8170 struct btrfs_fs_info *fs_info = root->fs_info;
8171 struct btrfs_block_group_cache *cache = NULL;
8172 u64 group_trimmed;
8173 u64 start;
8174 u64 end;
8175 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008176 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008177 int ret = 0;
8178
Liu Bo2cac13e2012-02-09 18:17:41 +08008179 /*
8180 * try to trim all FS space, our block group may start from non-zero.
8181 */
8182 if (range->len == total_bytes)
8183 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8184 else
8185 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008186
8187 while (cache) {
8188 if (cache->key.objectid >= (range->start + range->len)) {
8189 btrfs_put_block_group(cache);
8190 break;
8191 }
8192
8193 start = max(range->start, cache->key.objectid);
8194 end = min(range->start + range->len,
8195 cache->key.objectid + cache->key.offset);
8196
8197 if (end - start >= range->minlen) {
8198 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008199 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008200 if (!ret)
8201 wait_block_group_cache_done(cache);
8202 }
8203 ret = btrfs_trim_block_group(cache,
8204 &group_trimmed,
8205 start,
8206 end,
8207 range->minlen);
8208
8209 trimmed += group_trimmed;
8210 if (ret) {
8211 btrfs_put_block_group(cache);
8212 break;
8213 }
8214 }
8215
8216 cache = next_block_group(fs_info->tree_root, cache);
8217 }
8218
8219 range->len = trimmed;
8220 return ret;
8221}