blob: 825f23b13b584da158fa1bebd6a101a2e637758a [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 Masonfa9c0d792009-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 Zefan34d52cb2011-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);
164 spin_unlock(&info->block_group_cache_lock);
165
166 return 0;
167}
168
169/*
170 * This will return the block group at or after bytenr if contains is 0, else
171 * it will return the block group that contains the bytenr
172 */
173static struct btrfs_block_group_cache *
174block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
175 int contains)
176{
177 struct btrfs_block_group_cache *cache, *ret = NULL;
178 struct rb_node *n;
179 u64 end, start;
180
181 spin_lock(&info->block_group_cache_lock);
182 n = info->block_group_cache_tree.rb_node;
183
184 while (n) {
185 cache = rb_entry(n, struct btrfs_block_group_cache,
186 cache_node);
187 end = cache->key.objectid + cache->key.offset - 1;
188 start = cache->key.objectid;
189
190 if (bytenr < start) {
191 if (!contains && (!ret || start < ret->key.objectid))
192 ret = cache;
193 n = n->rb_left;
194 } else if (bytenr > start) {
195 if (contains && bytenr <= end) {
196 ret = cache;
197 break;
198 }
199 n = n->rb_right;
200 } else {
201 ret = cache;
202 break;
203 }
204 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500205 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000206 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400207 spin_unlock(&info->block_group_cache_lock);
208
209 return ret;
210}
211
Yan Zheng11833d62009-09-11 16:11:19 -0400212static int add_excluded_extent(struct btrfs_root *root,
213 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400214{
Yan Zheng11833d62009-09-11 16:11:19 -0400215 u64 end = start + num_bytes - 1;
216 set_extent_bits(&root->fs_info->freed_extents[0],
217 start, end, EXTENT_UPTODATE, GFP_NOFS);
218 set_extent_bits(&root->fs_info->freed_extents[1],
219 start, end, EXTENT_UPTODATE, GFP_NOFS);
220 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400221}
222
Yan Zheng11833d62009-09-11 16:11:19 -0400223static void free_excluded_extents(struct btrfs_root *root,
224 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400225{
Yan Zheng11833d62009-09-11 16:11:19 -0400226 u64 start, end;
227
228 start = cache->key.objectid;
229 end = start + cache->key.offset - 1;
230
231 clear_extent_bits(&root->fs_info->freed_extents[0],
232 start, end, EXTENT_UPTODATE, GFP_NOFS);
233 clear_extent_bits(&root->fs_info->freed_extents[1],
234 start, end, EXTENT_UPTODATE, GFP_NOFS);
235}
236
237static int exclude_super_stripes(struct btrfs_root *root,
238 struct btrfs_block_group_cache *cache)
239{
Josef Bacik817d52f2009-07-13 21:29:25 -0400240 u64 bytenr;
241 u64 *logical;
242 int stripe_len;
243 int i, nr, ret;
244
Yan, Zheng06b23312009-11-26 09:31:11 +0000245 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
246 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
247 cache->bytes_super += stripe_len;
248 ret = add_excluded_extent(root, cache->key.objectid,
249 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100250 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng06b23312009-11-26 09:31:11 +0000251 }
252
Josef Bacik817d52f2009-07-13 21:29:25 -0400253 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
254 bytenr = btrfs_sb_offset(i);
255 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
256 cache->key.objectid, bytenr,
257 0, &logical, &nr, &stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100258 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -0400259
Josef Bacik817d52f2009-07-13 21:29:25 -0400260 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400261 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400262 ret = add_excluded_extent(root, logical[nr],
263 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100264 BUG_ON(ret); /* -ENOMEM */
Josef Bacik817d52f2009-07-13 21:29:25 -0400265 }
Yan Zheng11833d62009-09-11 16:11:19 -0400266
Josef Bacik817d52f2009-07-13 21:29:25 -0400267 kfree(logical);
268 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400269 return 0;
270}
271
Yan Zheng11833d62009-09-11 16:11:19 -0400272static struct btrfs_caching_control *
273get_caching_control(struct btrfs_block_group_cache *cache)
274{
275 struct btrfs_caching_control *ctl;
276
277 spin_lock(&cache->lock);
278 if (cache->cached != BTRFS_CACHE_STARTED) {
279 spin_unlock(&cache->lock);
280 return NULL;
281 }
282
Josef Bacikdde5abe2010-09-16 16:17:03 -0400283 /* We're loading it the fast way, so we don't have a caching_ctl. */
284 if (!cache->caching_ctl) {
285 spin_unlock(&cache->lock);
286 return NULL;
287 }
288
Yan Zheng11833d62009-09-11 16:11:19 -0400289 ctl = cache->caching_ctl;
290 atomic_inc(&ctl->count);
291 spin_unlock(&cache->lock);
292 return ctl;
293}
294
295static void put_caching_control(struct btrfs_caching_control *ctl)
296{
297 if (atomic_dec_and_test(&ctl->count))
298 kfree(ctl);
299}
300
Josef Bacik0f9dd462008-09-23 13:14:11 -0400301/*
302 * this is only called by cache_block_group, since we could have freed extents
303 * we need to check the pinned_extents for any extents that can't be used yet
304 * since their free space will be released as soon as the transaction commits.
305 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400306static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400307 struct btrfs_fs_info *info, u64 start, u64 end)
308{
Josef Bacik817d52f2009-07-13 21:29:25 -0400309 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 int ret;
311
312 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400313 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400315 EXTENT_DIRTY | EXTENT_UPTODATE,
316 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400317 if (ret)
318 break;
319
Yan, Zheng06b23312009-11-26 09:31:11 +0000320 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400321 start = extent_end + 1;
322 } else if (extent_start > start && extent_start < end) {
323 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400324 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500325 ret = btrfs_add_free_space(block_group, start,
326 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100327 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400328 start = extent_end + 1;
329 } else {
330 break;
331 }
332 }
333
334 if (start < end) {
335 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400336 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500337 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100338 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400339 }
340
Josef Bacik817d52f2009-07-13 21:29:25 -0400341 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400342}
343
Josef Bacikbab39bf2011-06-30 14:42:28 -0400344static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400345{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400346 struct btrfs_block_group_cache *block_group;
347 struct btrfs_fs_info *fs_info;
348 struct btrfs_caching_control *caching_ctl;
349 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400350 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400351 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400352 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400353 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400354 u64 last = 0;
355 u32 nritems;
356 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400357
Josef Bacikbab39bf2011-06-30 14:42:28 -0400358 caching_ctl = container_of(work, struct btrfs_caching_control, work);
359 block_group = caching_ctl->block_group;
360 fs_info = block_group->fs_info;
361 extent_root = fs_info->extent_root;
362
Chris Masone37c9e62007-05-09 20:13:14 -0400363 path = btrfs_alloc_path();
364 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400365 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400366
Josef Bacik817d52f2009-07-13 21:29:25 -0400367 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400368
Chris Mason5cd57b22008-06-25 16:01:30 -0400369 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400370 * We don't want to deadlock with somebody trying to allocate a new
371 * extent for the extent root while also trying to search the extent
372 * root to add free space. So we skip locking and search the commit
373 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400374 */
375 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400377 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400378
Yan Zhenge4404d62008-12-12 10:03:26 -0500379 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400380 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400381 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400382again:
Yan Zheng11833d62009-09-11 16:11:19 -0400383 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400384 /* need to make sure the commit_root doesn't disappear */
385 down_read(&fs_info->extent_commit_sem);
386
Yan Zheng11833d62009-09-11 16:11:19 -0400387 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400388 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400389 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500390
Yan Zheng11833d62009-09-11 16:11:19 -0400391 leaf = path->nodes[0];
392 nritems = btrfs_header_nritems(leaf);
393
Chris Masond3977122009-01-05 21:25:51 -0500394 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200395 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400396 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400397 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400398 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400399
Yan Zheng11833d62009-09-11 16:11:19 -0400400 if (path->slots[0] < nritems) {
401 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
402 } else {
403 ret = find_next_key(path, 0, &key);
404 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400405 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400406
Josef Bacik589d8ad2011-05-11 17:30:53 -0400407 if (need_resched() ||
408 btrfs_next_leaf(extent_root, path)) {
409 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400410 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400411 up_read(&fs_info->extent_commit_sem);
412 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400413 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400414 goto again;
415 }
416 leaf = path->nodes[0];
417 nritems = btrfs_header_nritems(leaf);
418 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400419 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400420
Yan Zheng11833d62009-09-11 16:11:19 -0400421 if (key.objectid < block_group->key.objectid) {
422 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400423 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400424 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400425
Chris Masone37c9e62007-05-09 20:13:14 -0400426 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400427 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400428 break;
Yan7d7d6062007-09-14 16:15:28 -0400429
Yan Zheng11833d62009-09-11 16:11:19 -0400430 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400431 total_found += add_new_free_space(block_group,
432 fs_info, last,
433 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400434 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400435
Yan Zheng11833d62009-09-11 16:11:19 -0400436 if (total_found > (1024 * 1024 * 2)) {
437 total_found = 0;
438 wake_up(&caching_ctl->wait);
439 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400440 }
Chris Masone37c9e62007-05-09 20:13:14 -0400441 path->slots[0]++;
442 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400443 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400444
445 total_found += add_new_free_space(block_group, fs_info, last,
446 block_group->key.objectid +
447 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400448 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400449
450 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400451 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400452 block_group->cached = BTRFS_CACHE_FINISHED;
453 spin_unlock(&block_group->lock);
454
Chris Mason54aa1f42007-06-22 14:16:25 -0400455err:
Chris Masone37c9e62007-05-09 20:13:14 -0400456 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400457 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400458
Yan Zheng11833d62009-09-11 16:11:19 -0400459 free_excluded_extents(extent_root, block_group);
460
461 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400462out:
Yan Zheng11833d62009-09-11 16:11:19 -0400463 wake_up(&caching_ctl->wait);
464
465 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000466 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400467}
468
Josef Bacik9d66e232010-08-25 16:54:15 -0400469static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400470 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400471{
Josef Bacik291c7d22011-11-14 13:52:14 -0500472 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400473 struct btrfs_fs_info *fs_info = cache->fs_info;
474 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400475 int ret = 0;
476
Josef Bacik291c7d22011-11-14 13:52:14 -0500477 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100478 if (!caching_ctl)
479 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500480
481 INIT_LIST_HEAD(&caching_ctl->list);
482 mutex_init(&caching_ctl->mutex);
483 init_waitqueue_head(&caching_ctl->wait);
484 caching_ctl->block_group = cache;
485 caching_ctl->progress = cache->key.objectid;
486 atomic_set(&caching_ctl->count, 1);
487 caching_ctl->work.func = caching_thread;
488
489 spin_lock(&cache->lock);
490 /*
491 * This should be a rare occasion, but this could happen I think in the
492 * case where one thread starts to load the space cache info, and then
493 * some other thread starts a transaction commit which tries to do an
494 * allocation while the other thread is still loading the space cache
495 * info. The previous loop should have kept us from choosing this block
496 * group, but if we've moved to the state where we will wait on caching
497 * block groups we need to first check if we're doing a fast load here,
498 * so we can wait for it to finish, otherwise we could end up allocating
499 * from a block group who's cache gets evicted for one reason or
500 * another.
501 */
502 while (cache->cached == BTRFS_CACHE_FAST) {
503 struct btrfs_caching_control *ctl;
504
505 ctl = cache->caching_ctl;
506 atomic_inc(&ctl->count);
507 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
508 spin_unlock(&cache->lock);
509
510 schedule();
511
512 finish_wait(&ctl->wait, &wait);
513 put_caching_control(ctl);
514 spin_lock(&cache->lock);
515 }
516
517 if (cache->cached != BTRFS_CACHE_NO) {
518 spin_unlock(&cache->lock);
519 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400520 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500521 }
522 WARN_ON(cache->caching_ctl);
523 cache->caching_ctl = caching_ctl;
524 cache->cached = BTRFS_CACHE_FAST;
525 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400526
Josef Bacikd53ba472012-04-12 16:03:57 -0400527 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400528 ret = load_free_space_cache(fs_info, cache);
529
530 spin_lock(&cache->lock);
531 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500532 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400533 cache->cached = BTRFS_CACHE_FINISHED;
534 cache->last_byte_to_unpin = (u64)-1;
535 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500536 if (load_cache_only) {
537 cache->caching_ctl = NULL;
538 cache->cached = BTRFS_CACHE_NO;
539 } else {
540 cache->cached = BTRFS_CACHE_STARTED;
541 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400542 }
543 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500544 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000545 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500546 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000547 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400548 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000549 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500550 } else {
551 /*
552 * We are not going to do the fast caching, set cached to the
553 * appropriate value and wakeup any waiters.
554 */
555 spin_lock(&cache->lock);
556 if (load_cache_only) {
557 cache->caching_ctl = NULL;
558 cache->cached = BTRFS_CACHE_NO;
559 } else {
560 cache->cached = BTRFS_CACHE_STARTED;
561 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400562 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500563 wake_up(&caching_ctl->wait);
564 }
565
566 if (load_cache_only) {
567 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400568 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400569 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400570
Yan Zheng11833d62009-09-11 16:11:19 -0400571 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500572 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400573 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
574 up_write(&fs_info->extent_commit_sem);
575
Josef Bacik11dfe352009-11-13 20:12:59 +0000576 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400577
Josef Bacikbab39bf2011-06-30 14:42:28 -0400578 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400579
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400580 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400581}
582
Josef Bacik0f9dd462008-09-23 13:14:11 -0400583/*
584 * return the block group that starts at or after bytenr
585 */
Chris Masond3977122009-01-05 21:25:51 -0500586static struct btrfs_block_group_cache *
587btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400588{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400589 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400590
Josef Bacik0f9dd462008-09-23 13:14:11 -0400591 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400592
Josef Bacik0f9dd462008-09-23 13:14:11 -0400593 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400594}
595
Josef Bacik0f9dd462008-09-23 13:14:11 -0400596/*
Sankar P9f556842009-05-14 13:52:22 -0400597 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400598 */
Chris Masond3977122009-01-05 21:25:51 -0500599struct btrfs_block_group_cache *btrfs_lookup_block_group(
600 struct btrfs_fs_info *info,
601 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400602{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400603 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400604
Josef Bacik0f9dd462008-09-23 13:14:11 -0400605 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400606
Josef Bacik0f9dd462008-09-23 13:14:11 -0400607 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400608}
Chris Mason0b86a832008-03-24 15:01:56 -0400609
Josef Bacik0f9dd462008-09-23 13:14:11 -0400610static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
611 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400612{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400613 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400614 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400615
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200616 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb822010-05-16 10:46:24 -0400617
Chris Mason4184ea72009-03-10 12:39:20 -0400618 rcu_read_lock();
619 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400620 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400621 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400622 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400623 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400624 }
Chris Mason4184ea72009-03-10 12:39:20 -0400625 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400626 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400627}
628
Chris Mason4184ea72009-03-10 12:39:20 -0400629/*
630 * after adding space to the filesystem, we need to clear the full flags
631 * on all the space infos.
632 */
633void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
634{
635 struct list_head *head = &info->space_info;
636 struct btrfs_space_info *found;
637
638 rcu_read_lock();
639 list_for_each_entry_rcu(found, head, list)
640 found->full = 0;
641 rcu_read_unlock();
642}
643
Yan Zhengd2fb3432008-12-11 16:30:39 -0500644u64 btrfs_find_block_group(struct btrfs_root *root,
645 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400646{
Chris Mason96b51792007-10-15 16:15:19 -0400647 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400648 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500649 u64 last = max(search_hint, search_start);
650 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400651 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500652 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400653 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400654again:
Zheng Yane8569812008-09-26 10:05:48 -0400655 while (1) {
656 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400657 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400658 break;
Chris Mason96b51792007-10-15 16:15:19 -0400659
Chris Masonc286ac42008-07-22 23:06:41 -0400660 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400661 last = cache->key.objectid + cache->key.offset;
662 used = btrfs_block_group_used(&cache->item);
663
Yan Zhengd2fb3432008-12-11 16:30:39 -0500664 if ((full_search || !cache->ro) &&
665 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400666 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500667 div_factor(cache->key.offset, factor)) {
668 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400669 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400670 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400671 goto found;
672 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400673 }
Chris Masonc286ac42008-07-22 23:06:41 -0400674 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400675 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400676 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400677 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400678 if (!wrapped) {
679 last = search_start;
680 wrapped = 1;
681 goto again;
682 }
683 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400684 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400685 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400686 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400687 goto again;
688 }
Chris Masonbe744172007-05-06 10:15:01 -0400689found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500690 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400691}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400692
Chris Masone02119d2008-09-05 16:13:11 -0400693/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400694int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400695{
696 int ret;
697 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400698 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400699
Zheng Yan31840ae2008-09-23 13:14:14 -0400700 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700701 if (!path)
702 return -ENOMEM;
703
Chris Masone02119d2008-09-05 16:13:11 -0400704 key.objectid = start;
705 key.offset = len;
706 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
707 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
708 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400709 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500710 return ret;
711}
712
Chris Masond8d5f3e2007-12-11 12:42:00 -0500713/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400714 * helper function to lookup reference count and flags of extent.
715 *
716 * the head node for delayed ref is used to store the sum of all the
717 * reference count modifications queued up in the rbtree. the head
718 * node may also store the extent flags to set. This way you can check
719 * to see what the reference count and extent flags would be if all of
720 * the delayed refs are not processed.
721 */
722int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
723 struct btrfs_root *root, u64 bytenr,
724 u64 num_bytes, u64 *refs, u64 *flags)
725{
726 struct btrfs_delayed_ref_head *head;
727 struct btrfs_delayed_ref_root *delayed_refs;
728 struct btrfs_path *path;
729 struct btrfs_extent_item *ei;
730 struct extent_buffer *leaf;
731 struct btrfs_key key;
732 u32 item_size;
733 u64 num_refs;
734 u64 extent_flags;
735 int ret;
736
737 path = btrfs_alloc_path();
738 if (!path)
739 return -ENOMEM;
740
741 key.objectid = bytenr;
742 key.type = BTRFS_EXTENT_ITEM_KEY;
743 key.offset = num_bytes;
744 if (!trans) {
745 path->skip_locking = 1;
746 path->search_commit_root = 1;
747 }
748again:
749 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
750 &key, path, 0, 0);
751 if (ret < 0)
752 goto out_free;
753
754 if (ret == 0) {
755 leaf = path->nodes[0];
756 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
757 if (item_size >= sizeof(*ei)) {
758 ei = btrfs_item_ptr(leaf, path->slots[0],
759 struct btrfs_extent_item);
760 num_refs = btrfs_extent_refs(leaf, ei);
761 extent_flags = btrfs_extent_flags(leaf, ei);
762 } else {
763#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
764 struct btrfs_extent_item_v0 *ei0;
765 BUG_ON(item_size != sizeof(*ei0));
766 ei0 = btrfs_item_ptr(leaf, path->slots[0],
767 struct btrfs_extent_item_v0);
768 num_refs = btrfs_extent_refs_v0(leaf, ei0);
769 /* FIXME: this isn't correct for data */
770 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
771#else
772 BUG();
773#endif
774 }
775 BUG_ON(num_refs == 0);
776 } else {
777 num_refs = 0;
778 extent_flags = 0;
779 ret = 0;
780 }
781
782 if (!trans)
783 goto out;
784
785 delayed_refs = &trans->transaction->delayed_refs;
786 spin_lock(&delayed_refs->lock);
787 head = btrfs_find_delayed_ref_head(trans, bytenr);
788 if (head) {
789 if (!mutex_trylock(&head->mutex)) {
790 atomic_inc(&head->node.refs);
791 spin_unlock(&delayed_refs->lock);
792
David Sterbab3b4aa72011-04-21 01:20:15 +0200793 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400794
David Sterba8cc33e52011-05-02 15:29:25 +0200795 /*
796 * Mutex was contended, block until it's released and try
797 * again
798 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400799 mutex_lock(&head->mutex);
800 mutex_unlock(&head->mutex);
801 btrfs_put_delayed_ref(&head->node);
802 goto again;
803 }
804 if (head->extent_op && head->extent_op->update_flags)
805 extent_flags |= head->extent_op->flags_to_set;
806 else
807 BUG_ON(num_refs == 0);
808
809 num_refs += head->node.ref_mod;
810 mutex_unlock(&head->mutex);
811 }
812 spin_unlock(&delayed_refs->lock);
813out:
814 WARN_ON(num_refs == 0);
815 if (refs)
816 *refs = num_refs;
817 if (flags)
818 *flags = extent_flags;
819out_free:
820 btrfs_free_path(path);
821 return ret;
822}
823
824/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500825 * Back reference rules. Back refs have three main goals:
826 *
827 * 1) differentiate between all holders of references to an extent so that
828 * when a reference is dropped we can make sure it was a valid reference
829 * before freeing the extent.
830 *
831 * 2) Provide enough information to quickly find the holders of an extent
832 * if we notice a given block is corrupted or bad.
833 *
834 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
835 * maintenance. This is actually the same as #2, but with a slightly
836 * different use case.
837 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838 * There are two kinds of back refs. The implicit back refs is optimized
839 * for pointers in non-shared tree blocks. For a given pointer in a block,
840 * back refs of this kind provide information about the block's owner tree
841 * and the pointer's key. These information allow us to find the block by
842 * b-tree searching. The full back refs is for pointers in tree blocks not
843 * referenced by their owner trees. The location of tree block is recorded
844 * in the back refs. Actually the full back refs is generic, and can be
845 * used in all cases the implicit back refs is used. The major shortcoming
846 * of the full back refs is its overhead. Every time a tree block gets
847 * COWed, we have to update back refs entry for all pointers in it.
848 *
849 * For a newly allocated tree block, we use implicit back refs for
850 * pointers in it. This means most tree related operations only involve
851 * implicit back refs. For a tree block created in old transaction, the
852 * only way to drop a reference to it is COW it. So we can detect the
853 * event that tree block loses its owner tree's reference and do the
854 * back refs conversion.
855 *
856 * When a tree block is COW'd through a tree, there are four cases:
857 *
858 * The reference count of the block is one and the tree is the block's
859 * owner tree. Nothing to do in this case.
860 *
861 * The reference count of the block is one and the tree is not the
862 * block's owner tree. In this case, full back refs is used for pointers
863 * in the block. Remove these full back refs, add implicit back refs for
864 * every pointers in the new block.
865 *
866 * The reference count of the block is greater than one and the tree is
867 * the block's owner tree. In this case, implicit back refs is used for
868 * pointers in the block. Add full back refs for every pointers in the
869 * block, increase lower level extents' reference counts. The original
870 * implicit back refs are entailed to the new block.
871 *
872 * The reference count of the block is greater than one and the tree is
873 * not the block's owner tree. Add implicit back refs for every pointer in
874 * the new block, increase lower level extents' reference count.
875 *
876 * Back Reference Key composing:
877 *
878 * The key objectid corresponds to the first byte in the extent,
879 * The key type is used to differentiate between types of back refs.
880 * There are different meanings of the key offset for different types
881 * of back refs.
882 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500883 * File extents can be referenced by:
884 *
885 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400886 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500887 * - different offsets inside a file (bookend extents in file.c)
888 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400889 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500890 *
891 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500892 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893 * - original offset in the file
894 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400895 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400896 * The key offset for the implicit back refs is hash of the first
897 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500898 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500900 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400901 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500902 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400903 * The key offset for the implicit back refs is the first byte of
904 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500905 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 * When a file extent is allocated, The implicit back refs is used.
907 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500908 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400909 * (root_key.objectid, inode objectid, offset in file, 1)
910 *
911 * When a file extent is removed file truncation, we find the
912 * corresponding implicit back refs and check the following fields:
913 *
914 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500915 *
916 * Btree extents can be referenced by:
917 *
918 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500919 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 * Both the implicit back refs and the full back refs for tree blocks
921 * only consist of key. The key offset for the implicit back refs is
922 * objectid of block's owner tree. The key offset for the full back refs
923 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500924 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925 * When implicit back refs is used, information about the lowest key and
926 * level of the tree block are required. These information are stored in
927 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500928 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400929
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400930#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
931static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
932 struct btrfs_root *root,
933 struct btrfs_path *path,
934 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500935{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400936 struct btrfs_extent_item *item;
937 struct btrfs_extent_item_v0 *ei0;
938 struct btrfs_extent_ref_v0 *ref0;
939 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400940 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400941 struct btrfs_key key;
942 struct btrfs_key found_key;
943 u32 new_size = sizeof(*item);
944 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500945 int ret;
946
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947 leaf = path->nodes[0];
948 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500949
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
951 ei0 = btrfs_item_ptr(leaf, path->slots[0],
952 struct btrfs_extent_item_v0);
953 refs = btrfs_extent_refs_v0(leaf, ei0);
954
955 if (owner == (u64)-1) {
956 while (1) {
957 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
958 ret = btrfs_next_leaf(root, path);
959 if (ret < 0)
960 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100961 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 leaf = path->nodes[0];
963 }
964 btrfs_item_key_to_cpu(leaf, &found_key,
965 path->slots[0]);
966 BUG_ON(key.objectid != found_key.objectid);
967 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
968 path->slots[0]++;
969 continue;
970 }
971 ref0 = btrfs_item_ptr(leaf, path->slots[0],
972 struct btrfs_extent_ref_v0);
973 owner = btrfs_ref_objectid_v0(leaf, ref0);
974 break;
975 }
976 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200977 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400978
979 if (owner < BTRFS_FIRST_FREE_OBJECTID)
980 new_size += sizeof(*bi);
981
982 new_size -= sizeof(*ei0);
983 ret = btrfs_search_slot(trans, root, &key, path,
984 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400985 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988
Jeff Mahoney143bede2012-03-01 14:56:26 +0100989 btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990
991 leaf = path->nodes[0];
992 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
993 btrfs_set_extent_refs(leaf, item, refs);
994 /* FIXME: get real generation */
995 btrfs_set_extent_generation(leaf, item, 0);
996 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
997 btrfs_set_extent_flags(leaf, item,
998 BTRFS_EXTENT_FLAG_TREE_BLOCK |
999 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1000 bi = (struct btrfs_tree_block_info *)(item + 1);
1001 /* FIXME: get first key of the block */
1002 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1003 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1004 } else {
1005 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1006 }
1007 btrfs_mark_buffer_dirty(leaf);
1008 return 0;
1009}
1010#endif
1011
1012static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1013{
1014 u32 high_crc = ~(u32)0;
1015 u32 low_crc = ~(u32)0;
1016 __le64 lenum;
1017
1018 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001019 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001020 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001021 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001022 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001023 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001024
1025 return ((u64)high_crc << 31) ^ (u64)low_crc;
1026}
1027
1028static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1029 struct btrfs_extent_data_ref *ref)
1030{
1031 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1032 btrfs_extent_data_ref_objectid(leaf, ref),
1033 btrfs_extent_data_ref_offset(leaf, ref));
1034}
1035
1036static int match_extent_data_ref(struct extent_buffer *leaf,
1037 struct btrfs_extent_data_ref *ref,
1038 u64 root_objectid, u64 owner, u64 offset)
1039{
1040 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1041 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1042 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1043 return 0;
1044 return 1;
1045}
1046
1047static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1048 struct btrfs_root *root,
1049 struct btrfs_path *path,
1050 u64 bytenr, u64 parent,
1051 u64 root_objectid,
1052 u64 owner, u64 offset)
1053{
1054 struct btrfs_key key;
1055 struct btrfs_extent_data_ref *ref;
1056 struct extent_buffer *leaf;
1057 u32 nritems;
1058 int ret;
1059 int recow;
1060 int err = -ENOENT;
1061
1062 key.objectid = bytenr;
1063 if (parent) {
1064 key.type = BTRFS_SHARED_DATA_REF_KEY;
1065 key.offset = parent;
1066 } else {
1067 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1068 key.offset = hash_extent_data_ref(root_objectid,
1069 owner, offset);
1070 }
1071again:
1072 recow = 0;
1073 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1074 if (ret < 0) {
1075 err = ret;
1076 goto fail;
1077 }
1078
1079 if (parent) {
1080 if (!ret)
1081 return 0;
1082#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1083 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001084 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001085 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1086 if (ret < 0) {
1087 err = ret;
1088 goto fail;
1089 }
1090 if (!ret)
1091 return 0;
1092#endif
1093 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001094 }
1095
1096 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001097 nritems = btrfs_header_nritems(leaf);
1098 while (1) {
1099 if (path->slots[0] >= nritems) {
1100 ret = btrfs_next_leaf(root, path);
1101 if (ret < 0)
1102 err = ret;
1103 if (ret)
1104 goto fail;
1105
1106 leaf = path->nodes[0];
1107 nritems = btrfs_header_nritems(leaf);
1108 recow = 1;
1109 }
1110
1111 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1112 if (key.objectid != bytenr ||
1113 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1114 goto fail;
1115
1116 ref = btrfs_item_ptr(leaf, path->slots[0],
1117 struct btrfs_extent_data_ref);
1118
1119 if (match_extent_data_ref(leaf, ref, root_objectid,
1120 owner, offset)) {
1121 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001122 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001123 goto again;
1124 }
1125 err = 0;
1126 break;
1127 }
1128 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001129 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130fail:
1131 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001132}
1133
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1135 struct btrfs_root *root,
1136 struct btrfs_path *path,
1137 u64 bytenr, u64 parent,
1138 u64 root_objectid, u64 owner,
1139 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001140{
1141 struct btrfs_key key;
1142 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001143 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001144 u32 num_refs;
1145 int ret;
1146
1147 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001148 if (parent) {
1149 key.type = BTRFS_SHARED_DATA_REF_KEY;
1150 key.offset = parent;
1151 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001152 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001153 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1154 key.offset = hash_extent_data_ref(root_objectid,
1155 owner, offset);
1156 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001157 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158
1159 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1160 if (ret && ret != -EEXIST)
1161 goto fail;
1162
1163 leaf = path->nodes[0];
1164 if (parent) {
1165 struct btrfs_shared_data_ref *ref;
1166 ref = btrfs_item_ptr(leaf, path->slots[0],
1167 struct btrfs_shared_data_ref);
1168 if (ret == 0) {
1169 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1170 } else {
1171 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1172 num_refs += refs_to_add;
1173 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1174 }
1175 } else {
1176 struct btrfs_extent_data_ref *ref;
1177 while (ret == -EEXIST) {
1178 ref = btrfs_item_ptr(leaf, path->slots[0],
1179 struct btrfs_extent_data_ref);
1180 if (match_extent_data_ref(leaf, ref, root_objectid,
1181 owner, offset))
1182 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001183 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001184 key.offset++;
1185 ret = btrfs_insert_empty_item(trans, root, path, &key,
1186 size);
1187 if (ret && ret != -EEXIST)
1188 goto fail;
1189
1190 leaf = path->nodes[0];
1191 }
1192 ref = btrfs_item_ptr(leaf, path->slots[0],
1193 struct btrfs_extent_data_ref);
1194 if (ret == 0) {
1195 btrfs_set_extent_data_ref_root(leaf, ref,
1196 root_objectid);
1197 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1198 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1199 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1200 } else {
1201 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1202 num_refs += refs_to_add;
1203 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1204 }
1205 }
1206 btrfs_mark_buffer_dirty(leaf);
1207 ret = 0;
1208fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001209 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001210 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001211}
1212
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001213static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1214 struct btrfs_root *root,
1215 struct btrfs_path *path,
1216 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001217{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218 struct btrfs_key key;
1219 struct btrfs_extent_data_ref *ref1 = NULL;
1220 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001221 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001223 int ret = 0;
1224
1225 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001226 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1227
1228 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1229 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1230 struct btrfs_extent_data_ref);
1231 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1232 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1233 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1234 struct btrfs_shared_data_ref);
1235 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1236#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1237 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1238 struct btrfs_extent_ref_v0 *ref0;
1239 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_extent_ref_v0);
1241 num_refs = btrfs_ref_count_v0(leaf, ref0);
1242#endif
1243 } else {
1244 BUG();
1245 }
1246
Chris Mason56bec292009-03-13 10:10:06 -04001247 BUG_ON(num_refs < refs_to_drop);
1248 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249
Zheng Yan31840ae2008-09-23 13:14:14 -04001250 if (num_refs == 0) {
1251 ret = btrfs_del_item(trans, root, path);
1252 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001253 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1254 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1255 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1256 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1257#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1258 else {
1259 struct btrfs_extent_ref_v0 *ref0;
1260 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1261 struct btrfs_extent_ref_v0);
1262 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1263 }
1264#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001265 btrfs_mark_buffer_dirty(leaf);
1266 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001267 return ret;
1268}
1269
1270static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1271 struct btrfs_path *path,
1272 struct btrfs_extent_inline_ref *iref)
1273{
1274 struct btrfs_key key;
1275 struct extent_buffer *leaf;
1276 struct btrfs_extent_data_ref *ref1;
1277 struct btrfs_shared_data_ref *ref2;
1278 u32 num_refs = 0;
1279
1280 leaf = path->nodes[0];
1281 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1282 if (iref) {
1283 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1284 BTRFS_EXTENT_DATA_REF_KEY) {
1285 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1286 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1287 } else {
1288 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1289 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1290 }
1291 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1292 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1293 struct btrfs_extent_data_ref);
1294 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1295 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1296 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1297 struct btrfs_shared_data_ref);
1298 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1299#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1300 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1301 struct btrfs_extent_ref_v0 *ref0;
1302 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1303 struct btrfs_extent_ref_v0);
1304 num_refs = btrfs_ref_count_v0(leaf, ref0);
1305#endif
1306 } else {
1307 WARN_ON(1);
1308 }
1309 return num_refs;
1310}
1311
1312static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1313 struct btrfs_root *root,
1314 struct btrfs_path *path,
1315 u64 bytenr, u64 parent,
1316 u64 root_objectid)
1317{
1318 struct btrfs_key key;
1319 int ret;
1320
1321 key.objectid = bytenr;
1322 if (parent) {
1323 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1324 key.offset = parent;
1325 } else {
1326 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1327 key.offset = root_objectid;
1328 }
1329
1330 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1331 if (ret > 0)
1332 ret = -ENOENT;
1333#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1334 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001335 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001336 key.type = BTRFS_EXTENT_REF_V0_KEY;
1337 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1338 if (ret > 0)
1339 ret = -ENOENT;
1340 }
1341#endif
1342 return ret;
1343}
1344
1345static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1346 struct btrfs_root *root,
1347 struct btrfs_path *path,
1348 u64 bytenr, u64 parent,
1349 u64 root_objectid)
1350{
1351 struct btrfs_key key;
1352 int ret;
1353
1354 key.objectid = bytenr;
1355 if (parent) {
1356 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1357 key.offset = parent;
1358 } else {
1359 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1360 key.offset = root_objectid;
1361 }
1362
1363 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001364 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001365 return ret;
1366}
1367
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001368static inline int extent_ref_type(u64 parent, u64 owner)
1369{
1370 int type;
1371 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1372 if (parent > 0)
1373 type = BTRFS_SHARED_BLOCK_REF_KEY;
1374 else
1375 type = BTRFS_TREE_BLOCK_REF_KEY;
1376 } else {
1377 if (parent > 0)
1378 type = BTRFS_SHARED_DATA_REF_KEY;
1379 else
1380 type = BTRFS_EXTENT_DATA_REF_KEY;
1381 }
1382 return type;
1383}
1384
Yan Zheng2c47e6052009-06-27 21:07:35 -04001385static int find_next_key(struct btrfs_path *path, int level,
1386 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001387
1388{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001389 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001390 if (!path->nodes[level])
1391 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001392 if (path->slots[level] + 1 >=
1393 btrfs_header_nritems(path->nodes[level]))
1394 continue;
1395 if (level == 0)
1396 btrfs_item_key_to_cpu(path->nodes[level], key,
1397 path->slots[level] + 1);
1398 else
1399 btrfs_node_key_to_cpu(path->nodes[level], key,
1400 path->slots[level] + 1);
1401 return 0;
1402 }
1403 return 1;
1404}
1405
1406/*
1407 * look for inline back ref. if back ref is found, *ref_ret is set
1408 * to the address of inline back ref, and 0 is returned.
1409 *
1410 * if back ref isn't found, *ref_ret is set to the address where it
1411 * should be inserted, and -ENOENT is returned.
1412 *
1413 * if insert is true and there are too many inline back refs, the path
1414 * points to the extent item, and -EAGAIN is returned.
1415 *
1416 * NOTE: inline back refs are ordered in the same way that back ref
1417 * items in the tree are ordered.
1418 */
1419static noinline_for_stack
1420int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1421 struct btrfs_root *root,
1422 struct btrfs_path *path,
1423 struct btrfs_extent_inline_ref **ref_ret,
1424 u64 bytenr, u64 num_bytes,
1425 u64 parent, u64 root_objectid,
1426 u64 owner, u64 offset, int insert)
1427{
1428 struct btrfs_key key;
1429 struct extent_buffer *leaf;
1430 struct btrfs_extent_item *ei;
1431 struct btrfs_extent_inline_ref *iref;
1432 u64 flags;
1433 u64 item_size;
1434 unsigned long ptr;
1435 unsigned long end;
1436 int extra_size;
1437 int type;
1438 int want;
1439 int ret;
1440 int err = 0;
1441
1442 key.objectid = bytenr;
1443 key.type = BTRFS_EXTENT_ITEM_KEY;
1444 key.offset = num_bytes;
1445
1446 want = extent_ref_type(parent, owner);
1447 if (insert) {
1448 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001449 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001450 } else
1451 extra_size = -1;
1452 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1453 if (ret < 0) {
1454 err = ret;
1455 goto out;
1456 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001457 if (ret && !insert) {
1458 err = -ENOENT;
1459 goto out;
1460 }
1461 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001462
1463 leaf = path->nodes[0];
1464 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1465#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1466 if (item_size < sizeof(*ei)) {
1467 if (!insert) {
1468 err = -ENOENT;
1469 goto out;
1470 }
1471 ret = convert_extent_item_v0(trans, root, path, owner,
1472 extra_size);
1473 if (ret < 0) {
1474 err = ret;
1475 goto out;
1476 }
1477 leaf = path->nodes[0];
1478 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1479 }
1480#endif
1481 BUG_ON(item_size < sizeof(*ei));
1482
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001483 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1484 flags = btrfs_extent_flags(leaf, ei);
1485
1486 ptr = (unsigned long)(ei + 1);
1487 end = (unsigned long)ei + item_size;
1488
1489 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1490 ptr += sizeof(struct btrfs_tree_block_info);
1491 BUG_ON(ptr > end);
1492 } else {
1493 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1494 }
1495
1496 err = -ENOENT;
1497 while (1) {
1498 if (ptr >= end) {
1499 WARN_ON(ptr > end);
1500 break;
1501 }
1502 iref = (struct btrfs_extent_inline_ref *)ptr;
1503 type = btrfs_extent_inline_ref_type(leaf, iref);
1504 if (want < type)
1505 break;
1506 if (want > type) {
1507 ptr += btrfs_extent_inline_ref_size(type);
1508 continue;
1509 }
1510
1511 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1512 struct btrfs_extent_data_ref *dref;
1513 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1514 if (match_extent_data_ref(leaf, dref, root_objectid,
1515 owner, offset)) {
1516 err = 0;
1517 break;
1518 }
1519 if (hash_extent_data_ref_item(leaf, dref) <
1520 hash_extent_data_ref(root_objectid, owner, offset))
1521 break;
1522 } else {
1523 u64 ref_offset;
1524 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1525 if (parent > 0) {
1526 if (parent == ref_offset) {
1527 err = 0;
1528 break;
1529 }
1530 if (ref_offset < parent)
1531 break;
1532 } else {
1533 if (root_objectid == ref_offset) {
1534 err = 0;
1535 break;
1536 }
1537 if (ref_offset < root_objectid)
1538 break;
1539 }
1540 }
1541 ptr += btrfs_extent_inline_ref_size(type);
1542 }
1543 if (err == -ENOENT && insert) {
1544 if (item_size + extra_size >=
1545 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1546 err = -EAGAIN;
1547 goto out;
1548 }
1549 /*
1550 * To add new inline back ref, we have to make sure
1551 * there is no corresponding back ref item.
1552 * For simplicity, we just do not add new inline back
1553 * ref if there is any kind of item for this block
1554 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001555 if (find_next_key(path, 0, &key) == 0 &&
1556 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001557 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001558 err = -EAGAIN;
1559 goto out;
1560 }
1561 }
1562 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1563out:
Yan Zheng85d41982009-06-11 08:51:10 -04001564 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001565 path->keep_locks = 0;
1566 btrfs_unlock_up_safe(path, 1);
1567 }
1568 return err;
1569}
1570
1571/*
1572 * helper to add new inline back ref
1573 */
1574static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001575void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1576 struct btrfs_root *root,
1577 struct btrfs_path *path,
1578 struct btrfs_extent_inline_ref *iref,
1579 u64 parent, u64 root_objectid,
1580 u64 owner, u64 offset, int refs_to_add,
1581 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001582{
1583 struct extent_buffer *leaf;
1584 struct btrfs_extent_item *ei;
1585 unsigned long ptr;
1586 unsigned long end;
1587 unsigned long item_offset;
1588 u64 refs;
1589 int size;
1590 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591
1592 leaf = path->nodes[0];
1593 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1594 item_offset = (unsigned long)iref - (unsigned long)ei;
1595
1596 type = extent_ref_type(parent, owner);
1597 size = btrfs_extent_inline_ref_size(type);
1598
Jeff Mahoney143bede2012-03-01 14:56:26 +01001599 btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001600
1601 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1602 refs = btrfs_extent_refs(leaf, ei);
1603 refs += refs_to_add;
1604 btrfs_set_extent_refs(leaf, ei, refs);
1605 if (extent_op)
1606 __run_delayed_extent_op(extent_op, leaf, ei);
1607
1608 ptr = (unsigned long)ei + item_offset;
1609 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1610 if (ptr < end - size)
1611 memmove_extent_buffer(leaf, ptr + size, ptr,
1612 end - size - ptr);
1613
1614 iref = (struct btrfs_extent_inline_ref *)ptr;
1615 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1616 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1617 struct btrfs_extent_data_ref *dref;
1618 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1619 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1620 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1621 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1622 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1623 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1624 struct btrfs_shared_data_ref *sref;
1625 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1626 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1627 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1628 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1629 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1630 } else {
1631 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1632 }
1633 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001634}
1635
1636static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1637 struct btrfs_root *root,
1638 struct btrfs_path *path,
1639 struct btrfs_extent_inline_ref **ref_ret,
1640 u64 bytenr, u64 num_bytes, u64 parent,
1641 u64 root_objectid, u64 owner, u64 offset)
1642{
1643 int ret;
1644
1645 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1646 bytenr, num_bytes, parent,
1647 root_objectid, owner, offset, 0);
1648 if (ret != -ENOENT)
1649 return ret;
1650
David Sterbab3b4aa72011-04-21 01:20:15 +02001651 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001652 *ref_ret = NULL;
1653
1654 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1655 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1656 root_objectid);
1657 } else {
1658 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1659 root_objectid, owner, offset);
1660 }
1661 return ret;
1662}
1663
1664/*
1665 * helper to update/remove inline back ref
1666 */
1667static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001668void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1669 struct btrfs_root *root,
1670 struct btrfs_path *path,
1671 struct btrfs_extent_inline_ref *iref,
1672 int refs_to_mod,
1673 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001674{
1675 struct extent_buffer *leaf;
1676 struct btrfs_extent_item *ei;
1677 struct btrfs_extent_data_ref *dref = NULL;
1678 struct btrfs_shared_data_ref *sref = NULL;
1679 unsigned long ptr;
1680 unsigned long end;
1681 u32 item_size;
1682 int size;
1683 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001684 u64 refs;
1685
1686 leaf = path->nodes[0];
1687 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1688 refs = btrfs_extent_refs(leaf, ei);
1689 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1690 refs += refs_to_mod;
1691 btrfs_set_extent_refs(leaf, ei, refs);
1692 if (extent_op)
1693 __run_delayed_extent_op(extent_op, leaf, ei);
1694
1695 type = btrfs_extent_inline_ref_type(leaf, iref);
1696
1697 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1698 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1699 refs = btrfs_extent_data_ref_count(leaf, dref);
1700 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1701 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1702 refs = btrfs_shared_data_ref_count(leaf, sref);
1703 } else {
1704 refs = 1;
1705 BUG_ON(refs_to_mod != -1);
1706 }
1707
1708 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1709 refs += refs_to_mod;
1710
1711 if (refs > 0) {
1712 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1713 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1714 else
1715 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1716 } else {
1717 size = btrfs_extent_inline_ref_size(type);
1718 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1719 ptr = (unsigned long)iref;
1720 end = (unsigned long)ei + item_size;
1721 if (ptr + size < end)
1722 memmove_extent_buffer(leaf, ptr, ptr + size,
1723 end - ptr - size);
1724 item_size -= size;
Jeff Mahoney143bede2012-03-01 14:56:26 +01001725 btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001726 }
1727 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001728}
1729
1730static noinline_for_stack
1731int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1732 struct btrfs_root *root,
1733 struct btrfs_path *path,
1734 u64 bytenr, u64 num_bytes, u64 parent,
1735 u64 root_objectid, u64 owner,
1736 u64 offset, int refs_to_add,
1737 struct btrfs_delayed_extent_op *extent_op)
1738{
1739 struct btrfs_extent_inline_ref *iref;
1740 int ret;
1741
1742 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1743 bytenr, num_bytes, parent,
1744 root_objectid, owner, offset, 1);
1745 if (ret == 0) {
1746 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001747 update_inline_extent_backref(trans, root, path, iref,
1748 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001749 } else if (ret == -ENOENT) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001750 setup_inline_extent_backref(trans, root, path, iref, parent,
1751 root_objectid, owner, offset,
1752 refs_to_add, extent_op);
1753 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001754 }
1755 return ret;
1756}
1757
1758static int insert_extent_backref(struct btrfs_trans_handle *trans,
1759 struct btrfs_root *root,
1760 struct btrfs_path *path,
1761 u64 bytenr, u64 parent, u64 root_objectid,
1762 u64 owner, u64 offset, int refs_to_add)
1763{
1764 int ret;
1765 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1766 BUG_ON(refs_to_add != 1);
1767 ret = insert_tree_block_ref(trans, root, path, bytenr,
1768 parent, root_objectid);
1769 } else {
1770 ret = insert_extent_data_ref(trans, root, path, bytenr,
1771 parent, root_objectid,
1772 owner, offset, refs_to_add);
1773 }
1774 return ret;
1775}
1776
1777static int remove_extent_backref(struct btrfs_trans_handle *trans,
1778 struct btrfs_root *root,
1779 struct btrfs_path *path,
1780 struct btrfs_extent_inline_ref *iref,
1781 int refs_to_drop, int is_data)
1782{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001783 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001784
1785 BUG_ON(!is_data && refs_to_drop != 1);
1786 if (iref) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001787 update_inline_extent_backref(trans, root, path, iref,
1788 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001789 } else if (is_data) {
1790 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1791 } else {
1792 ret = btrfs_del_item(trans, root, path);
1793 }
1794 return ret;
1795}
1796
Li Dongyang5378e602011-03-24 10:24:27 +00001797static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001798 u64 start, u64 len)
1799{
Li Dongyang5378e602011-03-24 10:24:27 +00001800 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001801}
Chris Mason15916de2008-11-19 21:17:22 -05001802
Liu Hui1f3c79a2009-01-05 15:57:51 -05001803static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001804 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001805{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001806 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001807 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001808 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001809
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001810
Liu Hui1f3c79a2009-01-05 15:57:51 -05001811 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001812 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001813 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001814 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001815 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001816 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001817 int i;
1818
Liu Hui1f3c79a2009-01-05 15:57:51 -05001819
Jan Schmidta1d3c472011-08-04 17:15:33 +02001820 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001821 if (!stripe->dev->can_discard)
1822 continue;
1823
Li Dongyang5378e602011-03-24 10:24:27 +00001824 ret = btrfs_issue_discard(stripe->dev->bdev,
1825 stripe->physical,
1826 stripe->length);
1827 if (!ret)
1828 discarded_bytes += stripe->length;
1829 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001830 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001831
1832 /*
1833 * Just in case we get back EOPNOTSUPP for some reason,
1834 * just ignore the return value so we don't screw up
1835 * people calling discard_extent.
1836 */
1837 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001838 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001839 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001840 }
Li Dongyang5378e602011-03-24 10:24:27 +00001841
1842 if (actual_bytes)
1843 *actual_bytes = discarded_bytes;
1844
Liu Hui1f3c79a2009-01-05 15:57:51 -05001845
1846 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001847}
1848
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001849/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001850int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1851 struct btrfs_root *root,
1852 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001853 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001854{
1855 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001856 struct btrfs_fs_info *fs_info = root->fs_info;
1857
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001858 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1859 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001860
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001862 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1863 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001864 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001865 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001866 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001867 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1868 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001869 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001870 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001871 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001872 return ret;
1873}
1874
Chris Mason925baed2008-06-25 16:01:30 -04001875static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 struct btrfs_root *root,
1877 u64 bytenr, u64 num_bytes,
1878 u64 parent, u64 root_objectid,
1879 u64 owner, u64 offset, int refs_to_add,
1880 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001881{
Chris Mason5caf2a02007-04-02 11:20:42 -04001882 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001884 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001885 u64 refs;
1886 int ret;
1887 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001888
Chris Mason5caf2a02007-04-02 11:20:42 -04001889 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001890 if (!path)
1891 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001892
Chris Mason3c12ac72008-04-21 12:01:38 -04001893 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001894 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001895 /* this will setup the path even if it fails to insert the back ref */
1896 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1897 path, bytenr, num_bytes, parent,
1898 root_objectid, owner, offset,
1899 refs_to_add, extent_op);
1900 if (ret == 0)
1901 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001902
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001903 if (ret != -EAGAIN) {
1904 err = ret;
1905 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001906 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001907
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001908 leaf = path->nodes[0];
1909 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1910 refs = btrfs_extent_refs(leaf, item);
1911 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1912 if (extent_op)
1913 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001914
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001915 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001916 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001917
Chris Mason3c12ac72008-04-21 12:01:38 -04001918 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001919 path->leave_spinning = 1;
1920
Chris Mason56bec292009-03-13 10:10:06 -04001921 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001922 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001923 path, bytenr, parent, root_objectid,
1924 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001925 if (ret)
1926 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001927out:
Chris Mason74493f72007-12-11 09:25:06 -05001928 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001929 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001930}
1931
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001932static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1933 struct btrfs_root *root,
1934 struct btrfs_delayed_ref_node *node,
1935 struct btrfs_delayed_extent_op *extent_op,
1936 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001937{
Chris Mason56bec292009-03-13 10:10:06 -04001938 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001939 struct btrfs_delayed_data_ref *ref;
1940 struct btrfs_key ins;
1941 u64 parent = 0;
1942 u64 ref_root = 0;
1943 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001944
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001945 ins.objectid = node->bytenr;
1946 ins.offset = node->num_bytes;
1947 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001948
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001949 ref = btrfs_delayed_node_to_data_ref(node);
1950 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1951 parent = ref->parent;
1952 else
1953 ref_root = ref->root;
1954
1955 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1956 if (extent_op) {
1957 BUG_ON(extent_op->update_key);
1958 flags |= extent_op->flags_to_set;
1959 }
1960 ret = alloc_reserved_file_extent(trans, root,
1961 parent, ref_root, flags,
1962 ref->objectid, ref->offset,
1963 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001964 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1965 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1966 node->num_bytes, parent,
1967 ref_root, ref->objectid,
1968 ref->offset, node->ref_mod,
1969 extent_op);
1970 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1971 ret = __btrfs_free_extent(trans, root, node->bytenr,
1972 node->num_bytes, parent,
1973 ref_root, ref->objectid,
1974 ref->offset, node->ref_mod,
1975 extent_op);
1976 } else {
1977 BUG();
1978 }
Chris Mason56bec292009-03-13 10:10:06 -04001979 return ret;
1980}
1981
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001982static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1983 struct extent_buffer *leaf,
1984 struct btrfs_extent_item *ei)
1985{
1986 u64 flags = btrfs_extent_flags(leaf, ei);
1987 if (extent_op->update_flags) {
1988 flags |= extent_op->flags_to_set;
1989 btrfs_set_extent_flags(leaf, ei, flags);
1990 }
1991
1992 if (extent_op->update_key) {
1993 struct btrfs_tree_block_info *bi;
1994 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1995 bi = (struct btrfs_tree_block_info *)(ei + 1);
1996 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1997 }
1998}
1999
2000static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2001 struct btrfs_root *root,
2002 struct btrfs_delayed_ref_node *node,
2003 struct btrfs_delayed_extent_op *extent_op)
2004{
2005 struct btrfs_key key;
2006 struct btrfs_path *path;
2007 struct btrfs_extent_item *ei;
2008 struct extent_buffer *leaf;
2009 u32 item_size;
2010 int ret;
2011 int err = 0;
2012
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002013 if (trans->aborted)
2014 return 0;
2015
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002016 path = btrfs_alloc_path();
2017 if (!path)
2018 return -ENOMEM;
2019
2020 key.objectid = node->bytenr;
2021 key.type = BTRFS_EXTENT_ITEM_KEY;
2022 key.offset = node->num_bytes;
2023
2024 path->reada = 1;
2025 path->leave_spinning = 1;
2026 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2027 path, 0, 1);
2028 if (ret < 0) {
2029 err = ret;
2030 goto out;
2031 }
2032 if (ret > 0) {
2033 err = -EIO;
2034 goto out;
2035 }
2036
2037 leaf = path->nodes[0];
2038 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2039#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2040 if (item_size < sizeof(*ei)) {
2041 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2042 path, (u64)-1, 0);
2043 if (ret < 0) {
2044 err = ret;
2045 goto out;
2046 }
2047 leaf = path->nodes[0];
2048 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2049 }
2050#endif
2051 BUG_ON(item_size < sizeof(*ei));
2052 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2053 __run_delayed_extent_op(extent_op, leaf, ei);
2054
2055 btrfs_mark_buffer_dirty(leaf);
2056out:
2057 btrfs_free_path(path);
2058 return err;
2059}
2060
2061static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2062 struct btrfs_root *root,
2063 struct btrfs_delayed_ref_node *node,
2064 struct btrfs_delayed_extent_op *extent_op,
2065 int insert_reserved)
2066{
2067 int ret = 0;
2068 struct btrfs_delayed_tree_ref *ref;
2069 struct btrfs_key ins;
2070 u64 parent = 0;
2071 u64 ref_root = 0;
2072
2073 ins.objectid = node->bytenr;
2074 ins.offset = node->num_bytes;
2075 ins.type = BTRFS_EXTENT_ITEM_KEY;
2076
2077 ref = btrfs_delayed_node_to_tree_ref(node);
2078 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2079 parent = ref->parent;
2080 else
2081 ref_root = ref->root;
2082
2083 BUG_ON(node->ref_mod != 1);
2084 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2085 BUG_ON(!extent_op || !extent_op->update_flags ||
2086 !extent_op->update_key);
2087 ret = alloc_reserved_tree_block(trans, root,
2088 parent, ref_root,
2089 extent_op->flags_to_set,
2090 &extent_op->key,
2091 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002092 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2093 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2094 node->num_bytes, parent, ref_root,
2095 ref->level, 0, 1, extent_op);
2096 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2097 ret = __btrfs_free_extent(trans, root, node->bytenr,
2098 node->num_bytes, parent, ref_root,
2099 ref->level, 0, 1, extent_op);
2100 } else {
2101 BUG();
2102 }
2103 return ret;
2104}
2105
Chris Mason56bec292009-03-13 10:10:06 -04002106/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002107static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2108 struct btrfs_root *root,
2109 struct btrfs_delayed_ref_node *node,
2110 struct btrfs_delayed_extent_op *extent_op,
2111 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002112{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002113 int ret = 0;
2114
2115 if (trans->aborted)
2116 return 0;
2117
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002118 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002119 struct btrfs_delayed_ref_head *head;
2120 /*
2121 * we've hit the end of the chain and we were supposed
2122 * to insert this extent into the tree. But, it got
2123 * deleted before we ever needed to insert it, so all
2124 * we have to do is clean up the accounting
2125 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002126 BUG_ON(extent_op);
2127 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002128 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002129 btrfs_pin_extent(root, node->bytenr,
2130 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002131 if (head->is_data) {
2132 ret = btrfs_del_csums(trans, root,
2133 node->bytenr,
2134 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002135 }
Chris Mason56bec292009-03-13 10:10:06 -04002136 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002137 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002138 }
Josef Bacikeb099672009-02-12 09:27:38 -05002139
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002140 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2141 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2142 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2143 insert_reserved);
2144 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2145 node->type == BTRFS_SHARED_DATA_REF_KEY)
2146 ret = run_delayed_data_ref(trans, root, node, extent_op,
2147 insert_reserved);
2148 else
2149 BUG();
2150 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002151}
2152
Chris Mason56bec292009-03-13 10:10:06 -04002153static noinline struct btrfs_delayed_ref_node *
2154select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002155{
Chris Mason56bec292009-03-13 10:10:06 -04002156 struct rb_node *node;
2157 struct btrfs_delayed_ref_node *ref;
2158 int action = BTRFS_ADD_DELAYED_REF;
2159again:
2160 /*
2161 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2162 * this prevents ref count from going down to zero when
2163 * there still are pending delayed ref.
2164 */
2165 node = rb_prev(&head->node.rb_node);
2166 while (1) {
2167 if (!node)
2168 break;
2169 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2170 rb_node);
2171 if (ref->bytenr != head->node.bytenr)
2172 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002173 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002174 return ref;
2175 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002176 }
Chris Mason56bec292009-03-13 10:10:06 -04002177 if (action == BTRFS_ADD_DELAYED_REF) {
2178 action = BTRFS_DROP_DELAYED_REF;
2179 goto again;
2180 }
2181 return NULL;
2182}
2183
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002184/*
2185 * Returns 0 on success or if called with an already aborted transaction.
2186 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2187 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002188static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2189 struct btrfs_root *root,
2190 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002191{
Chris Mason56bec292009-03-13 10:10:06 -04002192 struct btrfs_delayed_ref_root *delayed_refs;
2193 struct btrfs_delayed_ref_node *ref;
2194 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002196 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002197 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002198 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002199 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002200
2201 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002202 while (1) {
2203 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002204 /* pick a new head ref from the cluster list */
2205 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002206 break;
Chris Mason56bec292009-03-13 10:10:06 -04002207
Chris Masonc3e69d52009-03-13 10:17:05 -04002208 locked_ref = list_entry(cluster->next,
2209 struct btrfs_delayed_ref_head, cluster);
2210
2211 /* grab the lock that says we are going to process
2212 * all the refs for this head */
2213 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2214
2215 /*
2216 * we may have dropped the spin lock to get the head
2217 * mutex lock, and that might have given someone else
2218 * time to free the head. If that's true, it has been
2219 * removed from our list and we can move on.
2220 */
2221 if (ret == -EAGAIN) {
2222 locked_ref = NULL;
2223 count++;
2224 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002225 }
2226 }
2227
2228 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002229 * We need to try and merge add/drops of the same ref since we
2230 * can run into issues with relocate dropping the implicit ref
2231 * and then it being added back again before the drop can
2232 * finish. If we merged anything we need to re-loop so we can
2233 * get a good ref.
2234 */
2235 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2236 locked_ref);
2237
2238 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002239 * locked_ref is the head node, so we have to go one
2240 * node back for any delayed ref updates
2241 */
2242 ref = select_delayed_ref(locked_ref);
2243
2244 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002245 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002246 /*
2247 * there are still refs with lower seq numbers in the
2248 * process of being added. Don't run this ref yet.
2249 */
2250 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002251 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002252 locked_ref = NULL;
2253 delayed_refs->num_heads_ready++;
2254 spin_unlock(&delayed_refs->lock);
2255 cond_resched();
2256 spin_lock(&delayed_refs->lock);
2257 continue;
2258 }
2259
2260 /*
Chris Mason56bec292009-03-13 10:10:06 -04002261 * record the must insert reserved flag before we
2262 * drop the spin lock.
2263 */
2264 must_insert_reserved = locked_ref->must_insert_reserved;
2265 locked_ref->must_insert_reserved = 0;
2266
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002267 extent_op = locked_ref->extent_op;
2268 locked_ref->extent_op = NULL;
2269
Chris Mason56bec292009-03-13 10:10:06 -04002270 if (!ref) {
2271 /* All delayed refs have been processed, Go ahead
2272 * and send the head node to run_one_delayed_ref,
2273 * so that any accounting fixes can happen
2274 */
2275 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002276
2277 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002278 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002279 extent_op = NULL;
2280 }
2281
2282 if (extent_op) {
2283 spin_unlock(&delayed_refs->lock);
2284
2285 ret = run_delayed_extent_op(trans, root,
2286 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002287 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002288
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002289 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002290 printk(KERN_DEBUG
2291 "btrfs: run_delayed_extent_op "
2292 "returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002293 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002294 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002295 return ret;
2296 }
2297
Chris Mason203bf282012-01-06 15:23:57 -05002298 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 }
Chris Mason56bec292009-03-13 10:10:06 -04002300 }
2301
2302 ref->in_tree = 0;
2303 rb_erase(&ref->rb_node, &delayed_refs->root);
2304 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002305 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002306 /*
2307 * when we play the delayed ref, also correct the
2308 * ref_mod on head
2309 */
2310 switch (ref->action) {
2311 case BTRFS_ADD_DELAYED_REF:
2312 case BTRFS_ADD_DELAYED_EXTENT:
2313 locked_ref->node.ref_mod -= ref->ref_mod;
2314 break;
2315 case BTRFS_DROP_DELAYED_REF:
2316 locked_ref->node.ref_mod += ref->ref_mod;
2317 break;
2318 default:
2319 WARN_ON(1);
2320 }
2321 }
Chris Mason56bec292009-03-13 10:10:06 -04002322 spin_unlock(&delayed_refs->lock);
2323
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002324 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002325 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002326
Miao Xie78a61842012-11-21 02:21:28 +00002327 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002328 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002329 btrfs_delayed_ref_unlock(locked_ref);
2330 btrfs_put_delayed_ref(ref);
2331 printk(KERN_DEBUG
2332 "btrfs: run_one_delayed_ref returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002333 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002334 return ret;
2335 }
2336
Miao Xie093486c2012-12-19 08:10:10 +00002337 /*
2338 * If this node is a head, that means all the refs in this head
2339 * have been dealt with, and we will pick the next head to deal
2340 * with, so we must unlock the head and drop it from the cluster
2341 * list before we release it.
2342 */
2343 if (btrfs_delayed_ref_is_head(ref)) {
2344 list_del_init(&locked_ref->cluster);
2345 btrfs_delayed_ref_unlock(locked_ref);
2346 locked_ref = NULL;
2347 }
2348 btrfs_put_delayed_ref(ref);
2349 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002350next:
Chris Mason1887be62009-03-13 10:11:24 -04002351 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002352 spin_lock(&delayed_refs->lock);
2353 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002354 return count;
2355}
2356
Arne Jansen709c0482011-09-12 12:22:57 +02002357#ifdef SCRAMBLE_DELAYED_REFS
2358/*
2359 * Normally delayed refs get processed in ascending bytenr order. This
2360 * correlates in most cases to the order added. To expose dependencies on this
2361 * order, we start to process the tree in the middle instead of the beginning
2362 */
2363static u64 find_middle(struct rb_root *root)
2364{
2365 struct rb_node *n = root->rb_node;
2366 struct btrfs_delayed_ref_node *entry;
2367 int alt = 1;
2368 u64 middle;
2369 u64 first = 0, last = 0;
2370
2371 n = rb_first(root);
2372 if (n) {
2373 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2374 first = entry->bytenr;
2375 }
2376 n = rb_last(root);
2377 if (n) {
2378 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2379 last = entry->bytenr;
2380 }
2381 n = root->rb_node;
2382
2383 while (n) {
2384 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2385 WARN_ON(!entry->in_tree);
2386
2387 middle = entry->bytenr;
2388
2389 if (alt)
2390 n = n->rb_left;
2391 else
2392 n = n->rb_right;
2393
2394 alt = 1 - alt;
2395 }
2396 return middle;
2397}
2398#endif
2399
Arne Jansenbed92ea2012-06-28 18:03:02 +02002400int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2401 struct btrfs_fs_info *fs_info)
2402{
2403 struct qgroup_update *qgroup_update;
2404 int ret = 0;
2405
2406 if (list_empty(&trans->qgroup_ref_list) !=
2407 !trans->delayed_ref_elem.seq) {
2408 /* list without seq or seq without list */
2409 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2410 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2411 trans->delayed_ref_elem.seq);
2412 BUG();
2413 }
2414
2415 if (!trans->delayed_ref_elem.seq)
2416 return 0;
2417
2418 while (!list_empty(&trans->qgroup_ref_list)) {
2419 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2420 struct qgroup_update, list);
2421 list_del(&qgroup_update->list);
2422 if (!ret)
2423 ret = btrfs_qgroup_account_ref(
2424 trans, fs_info, qgroup_update->node,
2425 qgroup_update->extent_op);
2426 kfree(qgroup_update);
2427 }
2428
2429 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2430
2431 return ret;
2432}
2433
Chris Masonc3e69d52009-03-13 10:17:05 -04002434/*
2435 * this starts processing the delayed reference count updates and
2436 * extent insertions we have queued up so far. count can be
2437 * 0, which means to process everything in the tree at the start
2438 * of the run (but not newly added entries), or it can be some target
2439 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002440 *
2441 * Returns 0 on success or if called with an aborted transaction
2442 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002443 */
2444int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2445 struct btrfs_root *root, unsigned long count)
2446{
2447 struct rb_node *node;
2448 struct btrfs_delayed_ref_root *delayed_refs;
2449 struct btrfs_delayed_ref_node *ref;
2450 struct list_head cluster;
2451 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002452 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002453 int run_all = count == (unsigned long)-1;
2454 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002455 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002456
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002457 /* We'll clean this up in btrfs_cleanup_transaction */
2458 if (trans->aborted)
2459 return 0;
2460
Chris Masonc3e69d52009-03-13 10:17:05 -04002461 if (root == root->fs_info->extent_root)
2462 root = root->fs_info->tree_root;
2463
Jan Schmidtedf39272012-06-28 18:04:55 +02002464 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2465
Chris Masonc3e69d52009-03-13 10:17:05 -04002466 delayed_refs = &trans->transaction->delayed_refs;
2467 INIT_LIST_HEAD(&cluster);
2468again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002469 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002470 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002471
Arne Jansen709c0482011-09-12 12:22:57 +02002472#ifdef SCRAMBLE_DELAYED_REFS
2473 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2474#endif
2475
Chris Masonc3e69d52009-03-13 10:17:05 -04002476 if (count == 0) {
2477 count = delayed_refs->num_entries * 2;
2478 run_most = 1;
2479 }
2480 while (1) {
2481 if (!(run_all || run_most) &&
2482 delayed_refs->num_heads_ready < 64)
2483 break;
2484
2485 /*
2486 * go find something we can process in the rbtree. We start at
2487 * the beginning of the tree, and then build a cluster
2488 * of refs to process starting at the first one we are able to
2489 * lock
2490 */
Jan Schmidta1686502011-12-12 16:10:07 +01002491 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002492 ret = btrfs_find_ref_cluster(trans, &cluster,
2493 delayed_refs->run_delayed_start);
2494 if (ret)
2495 break;
2496
2497 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002498 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002499 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002500 spin_unlock(&delayed_refs->lock);
2501 btrfs_abort_transaction(trans, root, ret);
2502 return ret;
2503 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002504
2505 count -= min_t(unsigned long, ret, count);
2506
2507 if (count == 0)
2508 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002509
Arne Jansen1fa11e22012-08-06 14:18:51 -06002510 if (delayed_start >= delayed_refs->run_delayed_start) {
2511 if (loops == 0) {
2512 /*
2513 * btrfs_find_ref_cluster looped. let's do one
2514 * more cycle. if we don't run any delayed ref
2515 * during that cycle (because we can't because
2516 * all of them are blocked), bail out.
2517 */
2518 loops = 1;
2519 } else {
2520 /*
2521 * no runnable refs left, stop trying
2522 */
2523 BUG_ON(run_all);
2524 break;
2525 }
2526 }
2527 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002528 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002529 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002530 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002531 }
2532
Chris Mason56bec292009-03-13 10:10:06 -04002533 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002534 if (!list_empty(&trans->new_bgs)) {
2535 spin_unlock(&delayed_refs->lock);
2536 btrfs_create_pending_block_groups(trans, root);
2537 spin_lock(&delayed_refs->lock);
2538 }
2539
Chris Mason56bec292009-03-13 10:10:06 -04002540 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002541 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002542 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002543 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002544
2545 while (node) {
2546 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2547 rb_node);
2548 if (btrfs_delayed_ref_is_head(ref)) {
2549 struct btrfs_delayed_ref_head *head;
2550
2551 head = btrfs_delayed_node_to_head(ref);
2552 atomic_inc(&ref->refs);
2553
2554 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002555 /*
2556 * Mutex was contended, block until it's
2557 * released and try again
2558 */
Chris Mason56bec292009-03-13 10:10:06 -04002559 mutex_lock(&head->mutex);
2560 mutex_unlock(&head->mutex);
2561
2562 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002563 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002564 goto again;
2565 }
2566 node = rb_next(node);
2567 }
2568 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002569 schedule_timeout(1);
2570 goto again;
2571 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002572out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002573 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002574 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002575 return 0;
2576}
2577
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002578int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2579 struct btrfs_root *root,
2580 u64 bytenr, u64 num_bytes, u64 flags,
2581 int is_data)
2582{
2583 struct btrfs_delayed_extent_op *extent_op;
2584 int ret;
2585
Miao Xie78a61842012-11-21 02:21:28 +00002586 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002587 if (!extent_op)
2588 return -ENOMEM;
2589
2590 extent_op->flags_to_set = flags;
2591 extent_op->update_flags = 1;
2592 extent_op->update_key = 0;
2593 extent_op->is_data = is_data ? 1 : 0;
2594
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002595 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2596 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002597 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002598 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002599 return ret;
2600}
2601
2602static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2603 struct btrfs_root *root,
2604 struct btrfs_path *path,
2605 u64 objectid, u64 offset, u64 bytenr)
2606{
2607 struct btrfs_delayed_ref_head *head;
2608 struct btrfs_delayed_ref_node *ref;
2609 struct btrfs_delayed_data_ref *data_ref;
2610 struct btrfs_delayed_ref_root *delayed_refs;
2611 struct rb_node *node;
2612 int ret = 0;
2613
2614 ret = -ENOENT;
2615 delayed_refs = &trans->transaction->delayed_refs;
2616 spin_lock(&delayed_refs->lock);
2617 head = btrfs_find_delayed_ref_head(trans, bytenr);
2618 if (!head)
2619 goto out;
2620
2621 if (!mutex_trylock(&head->mutex)) {
2622 atomic_inc(&head->node.refs);
2623 spin_unlock(&delayed_refs->lock);
2624
David Sterbab3b4aa72011-04-21 01:20:15 +02002625 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002626
David Sterba8cc33e52011-05-02 15:29:25 +02002627 /*
2628 * Mutex was contended, block until it's released and let
2629 * caller try again
2630 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002631 mutex_lock(&head->mutex);
2632 mutex_unlock(&head->mutex);
2633 btrfs_put_delayed_ref(&head->node);
2634 return -EAGAIN;
2635 }
2636
2637 node = rb_prev(&head->node.rb_node);
2638 if (!node)
2639 goto out_unlock;
2640
2641 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2642
2643 if (ref->bytenr != bytenr)
2644 goto out_unlock;
2645
2646 ret = 1;
2647 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2648 goto out_unlock;
2649
2650 data_ref = btrfs_delayed_node_to_data_ref(ref);
2651
2652 node = rb_prev(node);
2653 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002654 int seq = ref->seq;
2655
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002656 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002657 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002658 goto out_unlock;
2659 }
2660
2661 if (data_ref->root != root->root_key.objectid ||
2662 data_ref->objectid != objectid || data_ref->offset != offset)
2663 goto out_unlock;
2664
2665 ret = 0;
2666out_unlock:
2667 mutex_unlock(&head->mutex);
2668out:
2669 spin_unlock(&delayed_refs->lock);
2670 return ret;
2671}
2672
2673static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2674 struct btrfs_root *root,
2675 struct btrfs_path *path,
2676 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002677{
2678 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002679 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002680 struct btrfs_extent_data_ref *ref;
2681 struct btrfs_extent_inline_ref *iref;
2682 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002683 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002684 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002685 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002686
Chris Masonbe20aa92007-12-17 20:14:01 -05002687 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002688 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002689 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002690
Chris Masonbe20aa92007-12-17 20:14:01 -05002691 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2692 if (ret < 0)
2693 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002694 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002695
2696 ret = -ENOENT;
2697 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002698 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002699
Zheng Yan31840ae2008-09-23 13:14:14 -04002700 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002701 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002702 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002703
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002704 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002705 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002706
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002707 ret = 1;
2708 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2709#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2710 if (item_size < sizeof(*ei)) {
2711 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2712 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002713 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002714#endif
2715 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2716
2717 if (item_size != sizeof(*ei) +
2718 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2719 goto out;
2720
2721 if (btrfs_extent_generation(leaf, ei) <=
2722 btrfs_root_last_snapshot(&root->root_item))
2723 goto out;
2724
2725 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2726 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2727 BTRFS_EXTENT_DATA_REF_KEY)
2728 goto out;
2729
2730 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2731 if (btrfs_extent_refs(leaf, ei) !=
2732 btrfs_extent_data_ref_count(leaf, ref) ||
2733 btrfs_extent_data_ref_root(leaf, ref) !=
2734 root->root_key.objectid ||
2735 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2736 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2737 goto out;
2738
Yan Zhengf321e492008-07-30 09:26:11 -04002739 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002740out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002741 return ret;
2742}
2743
2744int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2745 struct btrfs_root *root,
2746 u64 objectid, u64 offset, u64 bytenr)
2747{
2748 struct btrfs_path *path;
2749 int ret;
2750 int ret2;
2751
2752 path = btrfs_alloc_path();
2753 if (!path)
2754 return -ENOENT;
2755
2756 do {
2757 ret = check_committed_ref(trans, root, path, objectid,
2758 offset, bytenr);
2759 if (ret && ret != -ENOENT)
2760 goto out;
2761
2762 ret2 = check_delayed_ref(trans, root, path, objectid,
2763 offset, bytenr);
2764 } while (ret2 == -EAGAIN);
2765
2766 if (ret2 && ret2 != -ENOENT) {
2767 ret = ret2;
2768 goto out;
2769 }
2770
2771 if (ret != -ENOENT || ret2 != -ENOENT)
2772 ret = 0;
2773out:
Yan Zhengf321e492008-07-30 09:26:11 -04002774 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002775 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2776 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002777 return ret;
2778}
2779
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002780static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002781 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002782 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002783 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002784{
2785 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002786 u64 num_bytes;
2787 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002788 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002789 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002790 struct btrfs_key key;
2791 struct btrfs_file_extent_item *fi;
2792 int i;
2793 int level;
2794 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002795 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002796 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002797
2798 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002799 nritems = btrfs_header_nritems(buf);
2800 level = btrfs_header_level(buf);
2801
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002802 if (!root->ref_cows && level == 0)
2803 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002804
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002805 if (inc)
2806 process_func = btrfs_inc_extent_ref;
2807 else
2808 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002809
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002810 if (full_backref)
2811 parent = buf->start;
2812 else
2813 parent = 0;
2814
Zheng Yan31840ae2008-09-23 13:14:14 -04002815 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002816 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002817 btrfs_item_key_to_cpu(buf, &key, i);
2818 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002819 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002820 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002821 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002822 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002823 BTRFS_FILE_EXTENT_INLINE)
2824 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002825 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2826 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002827 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002828
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002829 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2830 key.offset -= btrfs_file_extent_offset(buf, fi);
2831 ret = process_func(trans, root, bytenr, num_bytes,
2832 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002833 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002834 if (ret)
2835 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002836 } else {
2837 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002838 num_bytes = btrfs_level_size(root, level - 1);
2839 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002840 parent, ref_root, level - 1, 0,
2841 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002842 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002843 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002844 }
2845 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002846 return 0;
2847fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002848 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002849}
2850
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002851int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002852 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002853{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002854 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002855}
Zheng Yan31840ae2008-09-23 13:14:14 -04002856
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002857int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002858 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002859{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002860 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002861}
2862
Chris Mason9078a3e2007-04-26 16:46:15 -04002863static int write_one_cache_group(struct btrfs_trans_handle *trans,
2864 struct btrfs_root *root,
2865 struct btrfs_path *path,
2866 struct btrfs_block_group_cache *cache)
2867{
2868 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002869 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002870 unsigned long bi;
2871 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002872
Chris Mason9078a3e2007-04-26 16:46:15 -04002873 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002874 if (ret < 0)
2875 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002876 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04002877
2878 leaf = path->nodes[0];
2879 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2880 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2881 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002882 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002883fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002884 if (ret) {
2885 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04002886 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002887 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002888 return 0;
2889
2890}
2891
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002892static struct btrfs_block_group_cache *
2893next_block_group(struct btrfs_root *root,
2894 struct btrfs_block_group_cache *cache)
2895{
2896 struct rb_node *node;
2897 spin_lock(&root->fs_info->block_group_cache_lock);
2898 node = rb_next(&cache->cache_node);
2899 btrfs_put_block_group(cache);
2900 if (node) {
2901 cache = rb_entry(node, struct btrfs_block_group_cache,
2902 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002903 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002904 } else
2905 cache = NULL;
2906 spin_unlock(&root->fs_info->block_group_cache_lock);
2907 return cache;
2908}
2909
Josef Bacik0af3d002010-06-21 14:48:16 -04002910static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2911 struct btrfs_trans_handle *trans,
2912 struct btrfs_path *path)
2913{
2914 struct btrfs_root *root = block_group->fs_info->tree_root;
2915 struct inode *inode = NULL;
2916 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002917 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002918 int num_pages = 0;
2919 int retries = 0;
2920 int ret = 0;
2921
2922 /*
2923 * If this block group is smaller than 100 megs don't bother caching the
2924 * block group.
2925 */
2926 if (block_group->key.offset < (100 * 1024 * 1024)) {
2927 spin_lock(&block_group->lock);
2928 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2929 spin_unlock(&block_group->lock);
2930 return 0;
2931 }
2932
2933again:
2934 inode = lookup_free_space_inode(root, block_group, path);
2935 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2936 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002937 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002938 goto out;
2939 }
2940
2941 if (IS_ERR(inode)) {
2942 BUG_ON(retries);
2943 retries++;
2944
2945 if (block_group->ro)
2946 goto out_free;
2947
2948 ret = create_free_space_inode(root, trans, block_group, path);
2949 if (ret)
2950 goto out_free;
2951 goto again;
2952 }
2953
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002954 /* We've already setup this transaction, go ahead and exit */
2955 if (block_group->cache_generation == trans->transid &&
2956 i_size_read(inode)) {
2957 dcs = BTRFS_DC_SETUP;
2958 goto out_put;
2959 }
2960
Josef Bacik0af3d002010-06-21 14:48:16 -04002961 /*
2962 * We want to set the generation to 0, that way if anything goes wrong
2963 * from here on out we know not to trust this cache when we load up next
2964 * time.
2965 */
2966 BTRFS_I(inode)->generation = 0;
2967 ret = btrfs_update_inode(trans, root, inode);
2968 WARN_ON(ret);
2969
2970 if (i_size_read(inode) > 0) {
2971 ret = btrfs_truncate_free_space_cache(root, trans, path,
2972 inode);
2973 if (ret)
2974 goto out_put;
2975 }
2976
2977 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06002978 if (block_group->cached != BTRFS_CACHE_FINISHED ||
2979 !btrfs_test_opt(root, SPACE_CACHE)) {
2980 /*
2981 * don't bother trying to write stuff out _if_
2982 * a) we're not cached,
2983 * b) we're with nospace_cache mount option.
2984 */
Josef Bacik2b209822010-12-03 13:17:53 -05002985 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002986 spin_unlock(&block_group->lock);
2987 goto out_put;
2988 }
2989 spin_unlock(&block_group->lock);
2990
Josef Bacik6fc823b2012-08-06 13:46:38 -06002991 /*
2992 * Try to preallocate enough space based on how big the block group is.
2993 * Keep in mind this has to include any pinned space which could end up
2994 * taking up quite a bit since it's not folded into the other space
2995 * cache.
2996 */
2997 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04002998 if (!num_pages)
2999 num_pages = 1;
3000
Josef Bacik0af3d002010-06-21 14:48:16 -04003001 num_pages *= 16;
3002 num_pages *= PAGE_CACHE_SIZE;
3003
3004 ret = btrfs_check_data_free_space(inode, num_pages);
3005 if (ret)
3006 goto out_put;
3007
3008 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3009 num_pages, num_pages,
3010 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003011 if (!ret)
3012 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003013 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003014
Josef Bacik0af3d002010-06-21 14:48:16 -04003015out_put:
3016 iput(inode);
3017out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003018 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003019out:
3020 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003021 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003022 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003023 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003024 spin_unlock(&block_group->lock);
3025
3026 return ret;
3027}
3028
Chris Mason96b51792007-10-15 16:15:19 -04003029int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3030 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003031{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003032 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003033 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003034 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003035 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003036
3037 path = btrfs_alloc_path();
3038 if (!path)
3039 return -ENOMEM;
3040
Josef Bacik0af3d002010-06-21 14:48:16 -04003041again:
3042 while (1) {
3043 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3044 while (cache) {
3045 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3046 break;
3047 cache = next_block_group(root, cache);
3048 }
3049 if (!cache) {
3050 if (last == 0)
3051 break;
3052 last = 0;
3053 continue;
3054 }
3055 err = cache_save_setup(cache, trans, path);
3056 last = cache->key.objectid + cache->key.offset;
3057 btrfs_put_block_group(cache);
3058 }
3059
Chris Masond3977122009-01-05 21:25:51 -05003060 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003061 if (last == 0) {
3062 err = btrfs_run_delayed_refs(trans, root,
3063 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003064 if (err) /* File system offline */
3065 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003066 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003067
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003068 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3069 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003070 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3071 btrfs_put_block_group(cache);
3072 goto again;
3073 }
3074
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003075 if (cache->dirty)
3076 break;
3077 cache = next_block_group(root, cache);
3078 }
3079 if (!cache) {
3080 if (last == 0)
3081 break;
3082 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003083 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003084 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003085
Josef Bacik0cb59c92010-07-02 12:14:14 -04003086 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3087 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003088 cache->dirty = 0;
3089 last = cache->key.objectid + cache->key.offset;
3090
3091 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003092 if (err) /* File system offline */
3093 goto out;
3094
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003095 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003096 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003097
Josef Bacik0cb59c92010-07-02 12:14:14 -04003098 while (1) {
3099 /*
3100 * I don't think this is needed since we're just marking our
3101 * preallocated extent as written, but just in case it can't
3102 * hurt.
3103 */
3104 if (last == 0) {
3105 err = btrfs_run_delayed_refs(trans, root,
3106 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003107 if (err) /* File system offline */
3108 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003109 }
3110
3111 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3112 while (cache) {
3113 /*
3114 * Really this shouldn't happen, but it could if we
3115 * couldn't write the entire preallocated extent and
3116 * splitting the extent resulted in a new block.
3117 */
3118 if (cache->dirty) {
3119 btrfs_put_block_group(cache);
3120 goto again;
3121 }
3122 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3123 break;
3124 cache = next_block_group(root, cache);
3125 }
3126 if (!cache) {
3127 if (last == 0)
3128 break;
3129 last = 0;
3130 continue;
3131 }
3132
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003133 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003134
3135 /*
3136 * If we didn't have an error then the cache state is still
3137 * NEED_WRITE, so we can set it to WRITTEN.
3138 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003139 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003140 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3141 last = cache->key.objectid + cache->key.offset;
3142 btrfs_put_block_group(cache);
3143 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003144out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003145
Chris Mason9078a3e2007-04-26 16:46:15 -04003146 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003147 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003148}
3149
Yan Zhengd2fb3432008-12-11 16:30:39 -05003150int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3151{
3152 struct btrfs_block_group_cache *block_group;
3153 int readonly = 0;
3154
3155 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3156 if (!block_group || block_group->ro)
3157 readonly = 1;
3158 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003159 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003160 return readonly;
3161}
3162
Chris Mason593060d2008-03-25 16:50:33 -04003163static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3164 u64 total_bytes, u64 bytes_used,
3165 struct btrfs_space_info **space_info)
3166{
3167 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003168 int i;
3169 int factor;
3170
3171 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3172 BTRFS_BLOCK_GROUP_RAID10))
3173 factor = 2;
3174 else
3175 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003176
3177 found = __find_space_info(info, flags);
3178 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003179 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003180 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003181 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003182 found->bytes_used += bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003183 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003184 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003185 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003186 *space_info = found;
3187 return 0;
3188 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003189 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003190 if (!found)
3191 return -ENOMEM;
3192
Yan, Zhengb742bb822010-05-16 10:46:24 -04003193 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3194 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003195 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003196 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003197 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003198 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003199 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003200 found->bytes_used = bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003201 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003202 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003203 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003204 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003205 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003206 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003207 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003208 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003209 found->flush = 0;
3210 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003211 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003212 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003213 if (flags & BTRFS_BLOCK_GROUP_DATA)
3214 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003215 return 0;
3216}
3217
Chris Mason8790d502008-04-03 16:29:03 -04003218static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3219{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003220 u64 extra_flags = chunk_to_extended(flags) &
3221 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003222
3223 if (flags & BTRFS_BLOCK_GROUP_DATA)
3224 fs_info->avail_data_alloc_bits |= extra_flags;
3225 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3226 fs_info->avail_metadata_alloc_bits |= extra_flags;
3227 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3228 fs_info->avail_system_alloc_bits |= extra_flags;
Chris Mason8790d502008-04-03 16:29:03 -04003229}
Chris Mason593060d2008-03-25 16:50:33 -04003230
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003231/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003232 * returns target flags in extended format or 0 if restripe for this
3233 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003234 *
3235 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003236 */
3237static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3238{
3239 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3240 u64 target = 0;
3241
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003242 if (!bctl)
3243 return 0;
3244
3245 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3246 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3247 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3248 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3249 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3250 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3251 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3252 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3253 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3254 }
3255
3256 return target;
3257}
3258
3259/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003260 * @flags: available profiles in extended format (see ctree.h)
3261 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003262 * Returns reduced profile in chunk format. If profile changing is in
3263 * progress (either running or paused) picks the target profile (if it's
3264 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003265 */
Yan Zheng2b820322008-11-17 21:11:30 -05003266u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003267{
Chris Masoncd02dca2010-12-13 14:56:23 -05003268 /*
3269 * we add in the count of missing devices because we want
3270 * to make sure that any RAID levels on a degraded FS
3271 * continue to be honored.
3272 */
3273 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3274 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003275 u64 target;
Chris Masona061fc82008-05-07 11:43:44 -04003276
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003277 /*
3278 * see if restripe for this chunk_type is in progress, if so
3279 * try to reduce to the target profile
3280 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003281 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003282 target = get_restripe_target(root->fs_info, flags);
3283 if (target) {
3284 /* pick target profile only if it's already available */
3285 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003286 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003287 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003288 }
3289 }
3290 spin_unlock(&root->fs_info->balance_lock);
3291
Chris Masona061fc82008-05-07 11:43:44 -04003292 if (num_devices == 1)
3293 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3294 if (num_devices < 4)
3295 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3296
Chris Masonec44a352008-04-28 15:29:52 -04003297 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3298 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003299 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003300 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003301 }
Chris Masonec44a352008-04-28 15:29:52 -04003302
3303 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003304 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003305 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003306 }
Chris Masonec44a352008-04-28 15:29:52 -04003307
3308 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3309 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3310 (flags & BTRFS_BLOCK_GROUP_RAID10) |
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003311 (flags & BTRFS_BLOCK_GROUP_DUP))) {
Chris Masonec44a352008-04-28 15:29:52 -04003312 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003313 }
3314
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003315 return extended_to_chunk(flags);
Chris Masonec44a352008-04-28 15:29:52 -04003316}
3317
Yan, Zhengb742bb822010-05-16 10:46:24 -04003318static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003319{
Yan, Zhengb742bb822010-05-16 10:46:24 -04003320 if (flags & BTRFS_BLOCK_GROUP_DATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003321 flags |= root->fs_info->avail_data_alloc_bits;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003322 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003323 flags |= root->fs_info->avail_system_alloc_bits;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003324 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003325 flags |= root->fs_info->avail_metadata_alloc_bits;
3326
Yan, Zhengb742bb822010-05-16 10:46:24 -04003327 return btrfs_reduce_alloc_profile(root, flags);
3328}
Josef Bacik6a632092009-02-20 11:00:09 -05003329
Miao Xie6d07bce2011-01-05 10:07:31 +00003330u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003331{
3332 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003333
Yan, Zhengb742bb822010-05-16 10:46:24 -04003334 if (data)
3335 flags = BTRFS_BLOCK_GROUP_DATA;
3336 else if (root == root->fs_info->chunk_root)
3337 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3338 else
3339 flags = BTRFS_BLOCK_GROUP_METADATA;
3340
3341 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003342}
3343
Josef Bacik6a632092009-02-20 11:00:09 -05003344/*
3345 * This will check the space that the inode allocates from to make sure we have
3346 * enough space for bytes.
3347 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003348int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003349{
3350 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003351 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003352 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003353 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003354 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003355
3356 /* make sure bytes are sectorsize aligned */
3357 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3358
Li Zefan82d59022011-04-20 10:33:24 +08003359 if (root == root->fs_info->tree_root ||
3360 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003361 alloc_chunk = 0;
3362 committed = 1;
3363 }
3364
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003365 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003366 if (!data_sinfo)
3367 goto alloc;
3368
Josef Bacik6a632092009-02-20 11:00:09 -05003369again:
3370 /* make sure we have enough space to handle the data first */
3371 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003372 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3373 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3374 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003375
3376 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003377 struct btrfs_trans_handle *trans;
3378
Josef Bacik6a632092009-02-20 11:00:09 -05003379 /*
3380 * if we don't have enough free bytes in this space then we need
3381 * to alloc a new chunk.
3382 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003383 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003384 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003385
Chris Mason0e4f8f82011-04-15 16:05:44 -04003386 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003387 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003388alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003389 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003390 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003391 if (IS_ERR(trans))
3392 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003393
3394 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003395 alloc_target,
3396 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003397 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003398 if (ret < 0) {
3399 if (ret != -ENOSPC)
3400 return ret;
3401 else
3402 goto commit_trans;
3403 }
Chris Mason33b4d472009-09-22 14:45:50 -04003404
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003405 if (!data_sinfo)
3406 data_sinfo = fs_info->data_sinfo;
3407
Josef Bacik6a632092009-02-20 11:00:09 -05003408 goto again;
3409 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003410
3411 /*
3412 * If we have less pinned bytes than we want to allocate then
3413 * don't bother committing the transaction, it won't help us.
3414 */
3415 if (data_sinfo->bytes_pinned < bytes)
3416 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003417 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003418
3419 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003420commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003421 if (!committed &&
3422 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003423 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003424 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003425 if (IS_ERR(trans))
3426 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003427 ret = btrfs_commit_transaction(trans, root);
3428 if (ret)
3429 return ret;
3430 goto again;
3431 }
3432
Josef Bacik6a632092009-02-20 11:00:09 -05003433 return -ENOSPC;
3434 }
3435 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003436 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003437 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003438 spin_unlock(&data_sinfo->lock);
3439
Josef Bacik9ed74f22009-09-11 16:12:44 -04003440 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003441}
3442
3443/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003444 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003445 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003446void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003447{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003448 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003449 struct btrfs_space_info *data_sinfo;
3450
3451 /* make sure bytes are sectorsize aligned */
3452 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3453
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003454 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003455 spin_lock(&data_sinfo->lock);
3456 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003457 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003458 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003459 spin_unlock(&data_sinfo->lock);
3460}
3461
Josef Bacik97e728d2009-04-21 17:40:57 -04003462static void force_metadata_allocation(struct btrfs_fs_info *info)
3463{
3464 struct list_head *head = &info->space_info;
3465 struct btrfs_space_info *found;
3466
3467 rcu_read_lock();
3468 list_for_each_entry_rcu(found, head, list) {
3469 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003470 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003471 }
3472 rcu_read_unlock();
3473}
3474
Chris Masone5bc2452010-10-26 13:37:56 -04003475static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003476 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003477{
Josef Bacikfb25e912011-07-26 17:00:46 -04003478 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003479 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003480 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003481 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003482
Chris Mason0e4f8f82011-04-15 16:05:44 -04003483 if (force == CHUNK_ALLOC_FORCE)
3484 return 1;
3485
3486 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003487 * We need to take into account the global rsv because for all intents
3488 * and purposes it's used space. Don't worry about locking the
3489 * global_rsv, it doesn't change except when the transaction commits.
3490 */
Josef Bacik54338b52012-08-14 16:20:52 -04003491 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3492 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003493
3494 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003495 * in limited mode, we want to have some free space up to
3496 * about 1% of the FS size.
3497 */
3498 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003499 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003500 thresh = max_t(u64, 64 * 1024 * 1024,
3501 div_factor_fine(thresh, 1));
3502
3503 if (num_bytes - num_allocated < thresh)
3504 return 1;
3505 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003506
Josef Bacik698d0082012-09-12 14:08:47 -04003507 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003508 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003509 return 1;
3510}
3511
Liu Bo15d1ff82012-03-29 09:57:44 -04003512static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3513{
3514 u64 num_dev;
3515
3516 if (type & BTRFS_BLOCK_GROUP_RAID10 ||
3517 type & BTRFS_BLOCK_GROUP_RAID0)
3518 num_dev = root->fs_info->fs_devices->rw_devices;
3519 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3520 num_dev = 2;
3521 else
3522 num_dev = 1; /* DUP or single */
3523
3524 /* metadata for updaing devices and chunk tree */
3525 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3526}
3527
3528static void check_system_chunk(struct btrfs_trans_handle *trans,
3529 struct btrfs_root *root, u64 type)
3530{
3531 struct btrfs_space_info *info;
3532 u64 left;
3533 u64 thresh;
3534
3535 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3536 spin_lock(&info->lock);
3537 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3538 info->bytes_reserved - info->bytes_readonly;
3539 spin_unlock(&info->lock);
3540
3541 thresh = get_system_chunk_thresh(root, type);
3542 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3543 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3544 left, thresh, type);
3545 dump_space_info(info, 0, 0);
3546 }
3547
3548 if (left < thresh) {
3549 u64 flags;
3550
3551 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3552 btrfs_alloc_chunk(trans, root, flags);
3553 }
3554}
3555
Chris Mason6324fbf2008-03-24 15:01:59 -04003556static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003557 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003558{
3559 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003560 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003561 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003562 int ret = 0;
3563
Josef Bacikc6b305a2012-12-18 09:16:16 -05003564 /* Don't re-enter if we're already allocating a chunk */
3565 if (trans->allocating_chunk)
3566 return -ENOSPC;
3567
Chris Mason6324fbf2008-03-24 15:01:59 -04003568 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003569 if (!space_info) {
3570 ret = update_space_info(extent_root->fs_info, flags,
3571 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003572 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003573 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003574 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003575
Josef Bacik6d741192011-04-11 20:20:11 -04003576again:
Josef Bacik25179202008-10-29 14:49:05 -04003577 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003578 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003579 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003580 if (space_info->full) {
3581 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003582 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003583 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003584
Josef Bacik698d0082012-09-12 14:08:47 -04003585 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003586 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003587 return 0;
3588 } else if (space_info->chunk_alloc) {
3589 wait_for_alloc = 1;
3590 } else {
3591 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003592 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003593
Josef Bacik25179202008-10-29 14:49:05 -04003594 spin_unlock(&space_info->lock);
3595
Josef Bacik6d741192011-04-11 20:20:11 -04003596 mutex_lock(&fs_info->chunk_mutex);
3597
3598 /*
3599 * The chunk_mutex is held throughout the entirety of a chunk
3600 * allocation, so once we've acquired the chunk_mutex we know that the
3601 * other guy is done and we need to recheck and see if we should
3602 * allocate.
3603 */
3604 if (wait_for_alloc) {
3605 mutex_unlock(&fs_info->chunk_mutex);
3606 wait_for_alloc = 0;
3607 goto again;
3608 }
3609
Josef Bacikc6b305a2012-12-18 09:16:16 -05003610 trans->allocating_chunk = true;
3611
Josef Bacik97e728d2009-04-21 17:40:57 -04003612 /*
Josef Bacik67377732010-09-16 16:19:09 -04003613 * If we have mixed data/metadata chunks we want to make sure we keep
3614 * allocating mixed chunks instead of individual chunks.
3615 */
3616 if (btrfs_mixed_space_info(space_info))
3617 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3618
3619 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003620 * if we're doing a data chunk, go ahead and make sure that
3621 * we keep a reasonable number of metadata chunks allocated in the
3622 * FS as well.
3623 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003624 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003625 fs_info->data_chunk_allocations++;
3626 if (!(fs_info->data_chunk_allocations %
3627 fs_info->metadata_ratio))
3628 force_metadata_allocation(fs_info);
3629 }
3630
Liu Bo15d1ff82012-03-29 09:57:44 -04003631 /*
3632 * Check if we have enough space in SYSTEM chunk because we may need
3633 * to update devices.
3634 */
3635 check_system_chunk(trans, extent_root, flags);
3636
Yan Zheng2b820322008-11-17 21:11:30 -05003637 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003638 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003639 if (ret < 0 && ret != -ENOSPC)
3640 goto out;
3641
Josef Bacik9ed74f22009-09-11 16:12:44 -04003642 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003643 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003644 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003645 else
3646 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003647
Chris Mason0e4f8f82011-04-15 16:05:44 -04003648 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003649 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003650 spin_unlock(&space_info->lock);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003651out:
Dan Carpentera25c75d2012-04-18 09:59:29 +03003652 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003653 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003654}
3655
Josef Bacika80c8dc2012-09-06 16:59:33 -04003656static int can_overcommit(struct btrfs_root *root,
3657 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003658 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003659{
3660 u64 profile = btrfs_get_alloc_profile(root, 0);
3661 u64 avail;
3662 u64 used;
3663
3664 used = space_info->bytes_used + space_info->bytes_reserved +
3665 space_info->bytes_pinned + space_info->bytes_readonly +
3666 space_info->bytes_may_use;
3667
3668 spin_lock(&root->fs_info->free_chunk_lock);
3669 avail = root->fs_info->free_chunk_space;
3670 spin_unlock(&root->fs_info->free_chunk_lock);
3671
3672 /*
3673 * If we have dup, raid1 or raid10 then only half of the free
3674 * space is actually useable.
3675 */
3676 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3677 BTRFS_BLOCK_GROUP_RAID1 |
3678 BTRFS_BLOCK_GROUP_RAID10))
3679 avail >>= 1;
3680
3681 /*
Miao Xie561c2942012-10-16 11:32:18 +00003682 * If we aren't flushing all things, let us overcommit up to
3683 * 1/2th of the space. If we can flush, don't let us overcommit
3684 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003685 */
Miao Xie08e007d2012-10-16 11:33:38 +00003686 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003687 avail >>= 3;
3688 else
3689 avail >>= 1;
3690
3691 if (used + bytes < space_info->total_bytes + avail)
3692 return 1;
3693 return 0;
3694}
3695
Miao Xieda633a42012-12-20 11:19:09 +00003696static inline int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
3697 unsigned long nr_pages,
3698 enum wb_reason reason)
Josef Bacik1135d6d2012-12-14 13:46:43 -05003699{
Miao Xieda633a42012-12-20 11:19:09 +00003700 /* the flusher is dealing with the dirty inodes now. */
3701 if (writeback_in_progress(sb->s_bdi))
3702 return 1;
3703
3704 if (down_read_trylock(&sb->s_umount)) {
Josef Bacik1135d6d2012-12-14 13:46:43 -05003705 writeback_inodes_sb_nr(sb, nr_pages, reason);
3706 up_read(&sb->s_umount);
3707 return 1;
3708 }
3709
3710 return 0;
3711}
3712
Miao Xieda633a42012-12-20 11:19:09 +00003713void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3714 unsigned long nr_pages)
3715{
3716 struct super_block *sb = root->fs_info->sb;
3717 int started;
3718
3719 /* If we can not start writeback, just sync all the delalloc file. */
3720 started = writeback_inodes_sb_nr_if_idle_safe(sb, nr_pages,
3721 WB_REASON_FS_FREE_SPACE);
3722 if (!started) {
3723 /*
3724 * We needn't worry the filesystem going from r/w to r/o though
3725 * we don't acquire ->s_umount mutex, because the filesystem
3726 * should guarantee the delalloc inodes list be empty after
3727 * the filesystem is readonly(all dirty pages are written to
3728 * the disk).
3729 */
3730 btrfs_start_delalloc_inodes(root, 0);
3731 btrfs_wait_ordered_extents(root, 0);
3732 }
3733}
3734
Yan, Zheng5da9d012010-05-16 10:46:25 -04003735/*
3736 * shrink metadata reservation for delalloc
3737 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003738static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3739 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003740{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003741 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003742 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003743 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003744 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003745 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003746 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003747 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003748 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003749 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003750
Josef Bacik663350a2011-11-03 22:54:25 -04003751 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003752 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003753 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003754
3755 smp_mb();
Josef Bacikf4c738c2012-07-02 17:10:51 -04003756 delalloc_bytes = root->fs_info->delalloc_bytes;
3757 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003758 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003759 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003760 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003761 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003762 }
3763
Josef Bacikf4c738c2012-07-02 17:10:51 -04003764 while (delalloc_bytes && loops < 3) {
3765 max_reclaim = min(delalloc_bytes, to_reclaim);
3766 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003767 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003768 /*
3769 * We need to wait for the async pages to actually start before
3770 * we do anything.
3771 */
3772 wait_event(root->fs_info->async_submit_wait,
3773 !atomic_read(&root->fs_info->async_delalloc_pages));
3774
Miao Xie08e007d2012-10-16 11:33:38 +00003775 if (!trans)
3776 flush = BTRFS_RESERVE_FLUSH_ALL;
3777 else
3778 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003779 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003780 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003781 spin_unlock(&space_info->lock);
3782 break;
3783 }
Josef Bacik0019f102010-10-15 15:18:40 -04003784 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003785
Chris Mason36e39c42011-03-12 07:08:42 -05003786 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003787 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003788 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003789 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003790 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003791 if (time_left)
3792 break;
3793 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003794 smp_mb();
3795 delalloc_bytes = root->fs_info->delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003796 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003797}
3798
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003799/**
Josef Bacik663350a2011-11-03 22:54:25 -04003800 * maybe_commit_transaction - possibly commit the transaction if its ok to
3801 * @root - the root we're allocating for
3802 * @bytes - the number of bytes we want to reserve
3803 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003804 *
Josef Bacik663350a2011-11-03 22:54:25 -04003805 * This will check to make sure that committing the transaction will actually
3806 * get us somewhere and then commit the transaction if it does. Otherwise it
3807 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003808 */
Josef Bacik663350a2011-11-03 22:54:25 -04003809static int may_commit_transaction(struct btrfs_root *root,
3810 struct btrfs_space_info *space_info,
3811 u64 bytes, int force)
3812{
3813 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3814 struct btrfs_trans_handle *trans;
3815
3816 trans = (struct btrfs_trans_handle *)current->journal_info;
3817 if (trans)
3818 return -EAGAIN;
3819
3820 if (force)
3821 goto commit;
3822
3823 /* See if there is enough pinned space to make this reservation */
3824 spin_lock(&space_info->lock);
3825 if (space_info->bytes_pinned >= bytes) {
3826 spin_unlock(&space_info->lock);
3827 goto commit;
3828 }
3829 spin_unlock(&space_info->lock);
3830
3831 /*
3832 * See if there is some space in the delayed insertion reservation for
3833 * this reservation.
3834 */
3835 if (space_info != delayed_rsv->space_info)
3836 return -ENOSPC;
3837
Liu Bod9b02182012-02-16 18:34:39 +08003838 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003839 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003840 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04003841 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003842 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003843 return -ENOSPC;
3844 }
3845 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003846 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003847
3848commit:
3849 trans = btrfs_join_transaction(root);
3850 if (IS_ERR(trans))
3851 return -ENOSPC;
3852
3853 return btrfs_commit_transaction(trans, root);
3854}
3855
Josef Bacik96c3f432012-06-21 14:05:49 -04003856enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04003857 FLUSH_DELAYED_ITEMS_NR = 1,
3858 FLUSH_DELAYED_ITEMS = 2,
3859 FLUSH_DELALLOC = 3,
3860 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04003861 ALLOC_CHUNK = 5,
3862 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04003863};
3864
3865static int flush_space(struct btrfs_root *root,
3866 struct btrfs_space_info *space_info, u64 num_bytes,
3867 u64 orig_bytes, int state)
3868{
3869 struct btrfs_trans_handle *trans;
3870 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003871 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04003872
3873 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04003874 case FLUSH_DELAYED_ITEMS_NR:
3875 case FLUSH_DELAYED_ITEMS:
3876 if (state == FLUSH_DELAYED_ITEMS_NR) {
3877 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3878
3879 nr = (int)div64_u64(num_bytes, bytes);
3880 if (!nr)
3881 nr = 1;
3882 nr *= 2;
3883 } else {
3884 nr = -1;
3885 }
3886 trans = btrfs_join_transaction(root);
3887 if (IS_ERR(trans)) {
3888 ret = PTR_ERR(trans);
3889 break;
3890 }
3891 ret = btrfs_run_delayed_items_nr(trans, root, nr);
3892 btrfs_end_transaction(trans, root);
3893 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003894 case FLUSH_DELALLOC:
3895 case FLUSH_DELALLOC_WAIT:
3896 shrink_delalloc(root, num_bytes, orig_bytes,
3897 state == FLUSH_DELALLOC_WAIT);
3898 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04003899 case ALLOC_CHUNK:
3900 trans = btrfs_join_transaction(root);
3901 if (IS_ERR(trans)) {
3902 ret = PTR_ERR(trans);
3903 break;
3904 }
3905 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04003906 btrfs_get_alloc_profile(root, 0),
3907 CHUNK_ALLOC_NO_FORCE);
3908 btrfs_end_transaction(trans, root);
3909 if (ret == -ENOSPC)
3910 ret = 0;
3911 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04003912 case COMMIT_TRANS:
3913 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3914 break;
3915 default:
3916 ret = -ENOSPC;
3917 break;
3918 }
3919
3920 return ret;
3921}
Josef Bacik663350a2011-11-03 22:54:25 -04003922/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003923 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3924 * @root - the root we're allocating for
3925 * @block_rsv - the block_rsv we're allocating for
3926 * @orig_bytes - the number of bytes we want
3927 * @flush - wether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003928 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003929 * This will reserve orgi_bytes number of bytes from the space info associated
3930 * with the block_rsv. If there is not enough space it will make an attempt to
3931 * flush out space to make room. It will do this by flushing delalloc if
3932 * possible or committing the transaction. If flush is 0 then no attempts to
3933 * regain reservations will be made and this will fail if there is not enough
3934 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003935 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003936static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003937 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00003938 u64 orig_bytes,
3939 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003940{
3941 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04003942 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003943 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003944 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003945 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003946 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003947
3948again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003949 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003950 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003951 /*
Miao Xie08e007d2012-10-16 11:33:38 +00003952 * We only want to wait if somebody other than us is flushing and we
3953 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003954 */
Miao Xie08e007d2012-10-16 11:33:38 +00003955 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
3956 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003957 spin_unlock(&space_info->lock);
3958 /*
3959 * If we have a trans handle we can't wait because the flusher
3960 * may have to commit the transaction, which would mean we would
3961 * deadlock since we are waiting for the flusher to finish, but
3962 * hold the current transaction open.
3963 */
Josef Bacik663350a2011-11-03 22:54:25 -04003964 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003965 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02003966 ret = wait_event_killable(space_info->wait, !space_info->flush);
3967 /* Must have been killed, return */
3968 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003969 return -EINTR;
3970
3971 spin_lock(&space_info->lock);
3972 }
3973
3974 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04003975 used = space_info->bytes_used + space_info->bytes_reserved +
3976 space_info->bytes_pinned + space_info->bytes_readonly +
3977 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003978
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003979 /*
3980 * The idea here is that we've not already over-reserved the block group
3981 * then we can go ahead and save our reservation first and then start
3982 * flushing if we need to. Otherwise if we've already overcommitted
3983 * lets start flushing stuff first and then come back and try to make
3984 * our reservation.
3985 */
Josef Bacik2bf64752011-09-26 17:12:22 -04003986 if (used <= space_info->total_bytes) {
3987 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04003988 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003989 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04003990 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003991 ret = 0;
3992 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003993 /*
3994 * Ok set num_bytes to orig_bytes since we aren't
3995 * overocmmitted, this way we only try and reclaim what
3996 * we need.
3997 */
3998 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003999 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004000 } else {
4001 /*
4002 * Ok we're over committed, set num_bytes to the overcommitted
4003 * amount plus the amount of bytes that we need for this
4004 * reservation.
4005 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004006 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004007 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004008 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004009
Josef Bacik44734ed2012-09-28 16:04:19 -04004010 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4011 space_info->bytes_may_use += orig_bytes;
4012 trace_btrfs_space_reservation(root->fs_info, "space_info",
4013 space_info->flags, orig_bytes,
4014 1);
4015 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004016 }
4017
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004018 /*
4019 * Couldn't make our reservation, save our place so while we're trying
4020 * to reclaim space we can actually use it instead of somebody else
4021 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004022 *
4023 * We make the other tasks wait for the flush only when we can flush
4024 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004025 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004026 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004027 flushing = true;
4028 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004029 }
4030
Yan, Zhengf0486c62010-05-16 10:46:25 -04004031 spin_unlock(&space_info->lock);
4032
Miao Xie08e007d2012-10-16 11:33:38 +00004033 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004034 goto out;
4035
Josef Bacik96c3f432012-06-21 14:05:49 -04004036 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4037 flush_state);
4038 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004039
4040 /*
4041 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4042 * would happen. So skip delalloc flush.
4043 */
4044 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4045 (flush_state == FLUSH_DELALLOC ||
4046 flush_state == FLUSH_DELALLOC_WAIT))
4047 flush_state = ALLOC_CHUNK;
4048
Josef Bacik96c3f432012-06-21 14:05:49 -04004049 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004050 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004051 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4052 flush_state < COMMIT_TRANS)
4053 goto again;
4054 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4055 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004056 goto again;
4057
4058out:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004059 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004060 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004061 space_info->flush = 0;
4062 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004063 spin_unlock(&space_info->lock);
4064 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004065 return ret;
4066}
4067
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004068static struct btrfs_block_rsv *get_block_rsv(
4069 const struct btrfs_trans_handle *trans,
4070 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004071{
Josef Bacik4c13d752011-08-30 11:31:29 -04004072 struct btrfs_block_rsv *block_rsv = NULL;
4073
Josef Bacik0e721102012-06-26 16:13:18 -04004074 if (root->ref_cows)
4075 block_rsv = trans->block_rsv;
4076
4077 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004078 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004079
4080 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004081 block_rsv = root->block_rsv;
4082
4083 if (!block_rsv)
4084 block_rsv = &root->fs_info->empty_block_rsv;
4085
4086 return block_rsv;
4087}
4088
4089static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4090 u64 num_bytes)
4091{
4092 int ret = -ENOSPC;
4093 spin_lock(&block_rsv->lock);
4094 if (block_rsv->reserved >= num_bytes) {
4095 block_rsv->reserved -= num_bytes;
4096 if (block_rsv->reserved < block_rsv->size)
4097 block_rsv->full = 0;
4098 ret = 0;
4099 }
4100 spin_unlock(&block_rsv->lock);
4101 return ret;
4102}
4103
4104static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4105 u64 num_bytes, int update_size)
4106{
4107 spin_lock(&block_rsv->lock);
4108 block_rsv->reserved += num_bytes;
4109 if (update_size)
4110 block_rsv->size += num_bytes;
4111 else if (block_rsv->reserved >= block_rsv->size)
4112 block_rsv->full = 1;
4113 spin_unlock(&block_rsv->lock);
4114}
4115
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004116static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4117 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004118 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004119{
4120 struct btrfs_space_info *space_info = block_rsv->space_info;
4121
4122 spin_lock(&block_rsv->lock);
4123 if (num_bytes == (u64)-1)
4124 num_bytes = block_rsv->size;
4125 block_rsv->size -= num_bytes;
4126 if (block_rsv->reserved >= block_rsv->size) {
4127 num_bytes = block_rsv->reserved - block_rsv->size;
4128 block_rsv->reserved = block_rsv->size;
4129 block_rsv->full = 1;
4130 } else {
4131 num_bytes = 0;
4132 }
4133 spin_unlock(&block_rsv->lock);
4134
4135 if (num_bytes > 0) {
4136 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004137 spin_lock(&dest->lock);
4138 if (!dest->full) {
4139 u64 bytes_to_add;
4140
4141 bytes_to_add = dest->size - dest->reserved;
4142 bytes_to_add = min(num_bytes, bytes_to_add);
4143 dest->reserved += bytes_to_add;
4144 if (dest->reserved >= dest->size)
4145 dest->full = 1;
4146 num_bytes -= bytes_to_add;
4147 }
4148 spin_unlock(&dest->lock);
4149 }
4150 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004151 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004152 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004153 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004154 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004155 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004156 spin_unlock(&space_info->lock);
4157 }
4158 }
4159}
4160
4161static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4162 struct btrfs_block_rsv *dst, u64 num_bytes)
4163{
4164 int ret;
4165
4166 ret = block_rsv_use_bytes(src, num_bytes);
4167 if (ret)
4168 return ret;
4169
4170 block_rsv_add_bytes(dst, num_bytes, 1);
4171 return 0;
4172}
4173
Miao Xie66d8f3d2012-09-06 04:02:28 -06004174void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004175{
4176 memset(rsv, 0, sizeof(*rsv));
4177 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004178 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004179}
4180
Miao Xie66d8f3d2012-09-06 04:02:28 -06004181struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4182 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004183{
4184 struct btrfs_block_rsv *block_rsv;
4185 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004186
4187 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4188 if (!block_rsv)
4189 return NULL;
4190
Miao Xie66d8f3d2012-09-06 04:02:28 -06004191 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004192 block_rsv->space_info = __find_space_info(fs_info,
4193 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004194 return block_rsv;
4195}
4196
4197void btrfs_free_block_rsv(struct btrfs_root *root,
4198 struct btrfs_block_rsv *rsv)
4199{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004200 if (!rsv)
4201 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004202 btrfs_block_rsv_release(root, rsv, (u64)-1);
4203 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004204}
4205
Miao Xie08e007d2012-10-16 11:33:38 +00004206int btrfs_block_rsv_add(struct btrfs_root *root,
4207 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4208 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004209{
4210 int ret;
4211
4212 if (num_bytes == 0)
4213 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004214
Miao Xie61b520a2011-11-10 20:45:05 -05004215 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004216 if (!ret) {
4217 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4218 return 0;
4219 }
4220
Yan, Zhengf0486c62010-05-16 10:46:25 -04004221 return ret;
4222}
4223
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004224int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004225 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004226{
4227 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004228 int ret = -ENOSPC;
4229
4230 if (!block_rsv)
4231 return 0;
4232
4233 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004234 num_bytes = div_factor(block_rsv->size, min_factor);
4235 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004236 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004237 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004238
Josef Bacik36ba0222011-10-18 12:15:48 -04004239 return ret;
4240}
4241
Miao Xie08e007d2012-10-16 11:33:38 +00004242int btrfs_block_rsv_refill(struct btrfs_root *root,
4243 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4244 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004245{
4246 u64 num_bytes = 0;
4247 int ret = -ENOSPC;
4248
4249 if (!block_rsv)
4250 return 0;
4251
4252 spin_lock(&block_rsv->lock);
4253 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004254 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004255 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004256 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004257 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004258 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004259
Yan, Zhengf0486c62010-05-16 10:46:25 -04004260 if (!ret)
4261 return 0;
4262
Miao Xieaa38a712011-11-18 17:43:00 +08004263 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004264 if (!ret) {
4265 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004266 return 0;
4267 }
4268
Josef Bacik13553e52011-08-08 13:33:21 -04004269 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004270}
4271
4272int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4273 struct btrfs_block_rsv *dst_rsv,
4274 u64 num_bytes)
4275{
4276 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4277}
4278
4279void btrfs_block_rsv_release(struct btrfs_root *root,
4280 struct btrfs_block_rsv *block_rsv,
4281 u64 num_bytes)
4282{
4283 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4284 if (global_rsv->full || global_rsv == block_rsv ||
4285 block_rsv->space_info != global_rsv->space_info)
4286 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004287 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4288 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004289}
4290
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004291/*
4292 * helper to calculate size of global block reservation.
4293 * the desired value is sum of space used by extent tree,
4294 * checksum tree and root tree
4295 */
4296static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4297{
4298 struct btrfs_space_info *sinfo;
4299 u64 num_bytes;
4300 u64 meta_used;
4301 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004302 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004303
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004304 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4305 spin_lock(&sinfo->lock);
4306 data_used = sinfo->bytes_used;
4307 spin_unlock(&sinfo->lock);
4308
4309 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4310 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004311 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4312 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004313 meta_used = sinfo->bytes_used;
4314 spin_unlock(&sinfo->lock);
4315
4316 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4317 csum_size * 2;
4318 num_bytes += div64_u64(data_used + meta_used, 50);
4319
4320 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004321 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004322
4323 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4324}
4325
4326static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4327{
4328 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4329 struct btrfs_space_info *sinfo = block_rsv->space_info;
4330 u64 num_bytes;
4331
4332 num_bytes = calc_global_metadata_size(fs_info);
4333
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004334 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004335 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004336
4337 block_rsv->size = num_bytes;
4338
4339 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004340 sinfo->bytes_reserved + sinfo->bytes_readonly +
4341 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004342
4343 if (sinfo->total_bytes > num_bytes) {
4344 num_bytes = sinfo->total_bytes - num_bytes;
4345 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004346 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004347 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004348 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004349 }
4350
4351 if (block_rsv->reserved >= block_rsv->size) {
4352 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004353 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004354 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004355 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004356 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004357 block_rsv->reserved = block_rsv->size;
4358 block_rsv->full = 1;
4359 }
David Sterba182608c2011-05-05 13:13:16 +02004360
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004361 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004362 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004363}
4364
Yan, Zhengf0486c62010-05-16 10:46:25 -04004365static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4366{
4367 struct btrfs_space_info *space_info;
4368
4369 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4370 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004371
4372 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004373 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004374 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004375 fs_info->trans_block_rsv.space_info = space_info;
4376 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004377 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004378
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004379 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4380 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4381 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4382 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004383 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004384
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004385 update_global_block_rsv(fs_info);
4386}
4387
4388static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4389{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004390 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4391 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004392 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4393 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4394 WARN_ON(fs_info->trans_block_rsv.size > 0);
4395 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4396 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4397 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004398 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4399 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004400}
4401
Yan, Zhenga22285a2010-05-16 10:48:46 -04004402void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4403 struct btrfs_root *root)
4404{
Josef Bacik0e721102012-06-26 16:13:18 -04004405 if (!trans->block_rsv)
4406 return;
4407
Yan, Zhenga22285a2010-05-16 10:48:46 -04004408 if (!trans->bytes_reserved)
4409 return;
4410
Chris Masone77266e2012-02-24 10:39:05 -05004411 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004412 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004413 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004414 trans->bytes_reserved = 0;
4415}
4416
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004417/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004418int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4419 struct inode *inode)
4420{
4421 struct btrfs_root *root = BTRFS_I(inode)->root;
4422 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4423 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4424
4425 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004426 * We need to hold space in order to delete our orphan item once we've
4427 * added it, so this takes the reservation so we can release it later
4428 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004429 */
Chris Masonff5714c2011-05-28 07:00:39 -04004430 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004431 trace_btrfs_space_reservation(root->fs_info, "orphan",
4432 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004433 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4434}
4435
4436void btrfs_orphan_release_metadata(struct inode *inode)
4437{
4438 struct btrfs_root *root = BTRFS_I(inode)->root;
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, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004442 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4443}
4444
Yan, Zhenga22285a2010-05-16 10:48:46 -04004445int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4446 struct btrfs_pending_snapshot *pending)
4447{
4448 struct btrfs_root *root = pending->root;
4449 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4450 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4451 /*
Miao Xie48c03c42012-09-06 04:03:56 -06004452 * two for root back/forward refs, two for directory entries,
4453 * one for root of the snapshot and one for parent inode.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004454 */
Miao Xie48c03c42012-09-06 04:03:56 -06004455 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004456 dst_rsv->space_info = src_rsv->space_info;
4457 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4458}
4459
Josef Bacik7709cde2011-08-04 10:25:02 -04004460/**
4461 * drop_outstanding_extent - drop an outstanding extent
4462 * @inode: the inode we're dropping the extent for
4463 *
4464 * This is called when we are freeing up an outstanding extent, either called
4465 * after an error or after an extent is written. This will return the number of
4466 * reserved extents that need to be freed. This must be called with
4467 * BTRFS_I(inode)->lock held.
4468 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004469static unsigned drop_outstanding_extent(struct inode *inode)
4470{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004471 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004472 unsigned dropped_extents = 0;
4473
Josef Bacik9e0baf62011-07-15 15:16:44 +00004474 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4475 BTRFS_I(inode)->outstanding_extents--;
4476
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004477 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004478 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4479 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004480 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004481
Josef Bacik9e0baf62011-07-15 15:16:44 +00004482 /*
4483 * If we have more or the same amount of outsanding extents than we have
4484 * reserved then we need to leave the reserved extents count alone.
4485 */
4486 if (BTRFS_I(inode)->outstanding_extents >=
4487 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004488 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004489
4490 dropped_extents = BTRFS_I(inode)->reserved_extents -
4491 BTRFS_I(inode)->outstanding_extents;
4492 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004493 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004494}
4495
Josef Bacik7709cde2011-08-04 10:25:02 -04004496/**
4497 * calc_csum_metadata_size - return the amount of metada space that must be
4498 * reserved/free'd for the given bytes.
4499 * @inode: the inode we're manipulating
4500 * @num_bytes: the number of bytes in question
4501 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4502 *
4503 * This adjusts the number of csum_bytes in the inode and then returns the
4504 * correct amount of metadata that must either be reserved or freed. We
4505 * calculate how many checksums we can fit into one leaf and then divide the
4506 * number of bytes that will need to be checksumed by this value to figure out
4507 * how many checksums will be required. If we are adding bytes then the number
4508 * may go up and we will return the number of additional bytes that must be
4509 * reserved. If it is going down we will return the number of bytes that must
4510 * be freed.
4511 *
4512 * This must be called with BTRFS_I(inode)->lock held.
4513 */
4514static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4515 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004516{
Josef Bacik7709cde2011-08-04 10:25:02 -04004517 struct btrfs_root *root = BTRFS_I(inode)->root;
4518 u64 csum_size;
4519 int num_csums_per_leaf;
4520 int num_csums;
4521 int old_csums;
4522
4523 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4524 BTRFS_I(inode)->csum_bytes == 0)
4525 return 0;
4526
4527 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4528 if (reserve)
4529 BTRFS_I(inode)->csum_bytes += num_bytes;
4530 else
4531 BTRFS_I(inode)->csum_bytes -= num_bytes;
4532 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4533 num_csums_per_leaf = (int)div64_u64(csum_size,
4534 sizeof(struct btrfs_csum_item) +
4535 sizeof(struct btrfs_disk_key));
4536 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4537 num_csums = num_csums + num_csums_per_leaf - 1;
4538 num_csums = num_csums / num_csums_per_leaf;
4539
4540 old_csums = old_csums + num_csums_per_leaf - 1;
4541 old_csums = old_csums / num_csums_per_leaf;
4542
4543 /* No change, no need to reserve more */
4544 if (old_csums == num_csums)
4545 return 0;
4546
4547 if (reserve)
4548 return btrfs_calc_trans_metadata_size(root,
4549 num_csums - old_csums);
4550
4551 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004552}
4553
4554int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4555{
4556 struct btrfs_root *root = BTRFS_I(inode)->root;
4557 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004558 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004559 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004560 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004561 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004562 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004563 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004564 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004565
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004566 /* If we are a free space inode we need to not flush since we will be in
4567 * the middle of a transaction commit. We also don't need the delalloc
4568 * mutex since we won't race with anybody. We need this mostly to make
4569 * lockdep shut its filthy mouth.
4570 */
4571 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004572 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004573 delalloc_lock = false;
4574 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004575
Miao Xie08e007d2012-10-16 11:33:38 +00004576 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4577 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004578 schedule_timeout(1);
4579
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004580 if (delalloc_lock)
4581 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4582
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004583 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004584
Josef Bacik9e0baf62011-07-15 15:16:44 +00004585 spin_lock(&BTRFS_I(inode)->lock);
4586 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004587
Josef Bacik9e0baf62011-07-15 15:16:44 +00004588 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004589 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004590 nr_extents = BTRFS_I(inode)->outstanding_extents -
4591 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004592
4593 /*
4594 * Add an item to reserve for updating the inode when we complete the
4595 * delalloc io.
4596 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004597 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4598 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004599 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004600 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004601 }
4602
4603 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004604 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004605 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004606 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004607
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004608 if (root->fs_info->quota_enabled)
Arne Jansenc5567232011-09-14 15:44:05 +02004609 ret = btrfs_qgroup_reserve(root, num_bytes +
4610 nr_extents * root->leafsize);
Arne Jansenc5567232011-09-14 15:44:05 +02004611
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004612 /*
4613 * ret != 0 here means the qgroup reservation failed, we go straight to
4614 * the shared error handling then.
4615 */
4616 if (ret == 0)
4617 ret = reserve_metadata_bytes(root, block_rsv,
4618 to_reserve, flush);
4619
Josef Bacik9e0baf62011-07-15 15:16:44 +00004620 if (ret) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004621 u64 to_free = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004622 unsigned dropped;
Josef Bacik7ed49f12011-08-19 15:45:52 -04004623
Josef Bacik7709cde2011-08-04 10:25:02 -04004624 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004625 dropped = drop_outstanding_extent(inode);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004626 /*
Josef Bacik660d3f62011-12-09 11:18:51 -05004627 * If the inodes csum_bytes is the same as the original
4628 * csum_bytes then we know we haven't raced with any free()ers
4629 * so we can just reduce our inodes csum bytes and carry on.
4630 * Otherwise we have to do the normal free thing to account for
4631 * the case that the free side didn't free up its reserve
4632 * because of this outstanding reservation.
Josef Bacik7ed49f12011-08-19 15:45:52 -04004633 */
Josef Bacik660d3f62011-12-09 11:18:51 -05004634 if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4635 calc_csum_metadata_size(inode, num_bytes, 0);
4636 else
4637 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4638 spin_unlock(&BTRFS_I(inode)->lock);
4639 if (dropped)
4640 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4641
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004642 if (to_free) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004643 btrfs_block_rsv_release(root, block_rsv, to_free);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004644 trace_btrfs_space_reservation(root->fs_info,
4645 "delalloc",
4646 btrfs_ino(inode),
4647 to_free, 0);
4648 }
Miao Xie4b5829a2012-12-05 10:53:25 +00004649 if (root->fs_info->quota_enabled) {
4650 btrfs_qgroup_free(root, num_bytes +
4651 nr_extents * root->leafsize);
4652 }
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004653 if (delalloc_lock)
4654 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004655 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004656 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004657
Josef Bacik660d3f62011-12-09 11:18:51 -05004658 spin_lock(&BTRFS_I(inode)->lock);
4659 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004660 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4661 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004662 nr_extents--;
4663 }
4664 BTRFS_I(inode)->reserved_extents += nr_extents;
4665 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004666
4667 if (delalloc_lock)
4668 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004669
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004670 if (to_reserve)
4671 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4672 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004673 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4674
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004675 return 0;
4676}
4677
Josef Bacik7709cde2011-08-04 10:25:02 -04004678/**
4679 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4680 * @inode: the inode to release the reservation for
4681 * @num_bytes: the number of bytes we're releasing
4682 *
4683 * This will release the metadata reservation for an inode. This can be called
4684 * once we complete IO for a given set of bytes to release their metadata
4685 * reservations.
4686 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004687void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4688{
4689 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004690 u64 to_free = 0;
4691 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004692
4693 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004694 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004695 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004696
Josef Bacik7709cde2011-08-04 10:25:02 -04004697 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4698 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004699 if (dropped > 0)
4700 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004701
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004702 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4703 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004704 if (root->fs_info->quota_enabled) {
4705 btrfs_qgroup_free(root, num_bytes +
4706 dropped * root->leafsize);
4707 }
4708
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004709 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4710 to_free);
4711}
4712
Josef Bacik7709cde2011-08-04 10:25:02 -04004713/**
4714 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4715 * @inode: inode we're writing to
4716 * @num_bytes: the number of bytes we want to allocate
4717 *
4718 * This will do the following things
4719 *
4720 * o reserve space in the data space info for num_bytes
4721 * o reserve space in the metadata space info based on number of outstanding
4722 * extents and how much csums will be needed
4723 * o add to the inodes ->delalloc_bytes
4724 * o add it to the fs_info's delalloc inodes list.
4725 *
4726 * This will return 0 for success and -ENOSPC if there is no space left.
4727 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004728int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4729{
4730 int ret;
4731
4732 ret = btrfs_check_data_free_space(inode, num_bytes);
4733 if (ret)
4734 return ret;
4735
4736 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4737 if (ret) {
4738 btrfs_free_reserved_data_space(inode, num_bytes);
4739 return ret;
4740 }
4741
4742 return 0;
4743}
4744
Josef Bacik7709cde2011-08-04 10:25:02 -04004745/**
4746 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4747 * @inode: inode we're releasing space for
4748 * @num_bytes: the number of bytes we want to free up
4749 *
4750 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4751 * called in the case that we don't need the metadata AND data reservations
4752 * anymore. So if there is an error or we insert an inline extent.
4753 *
4754 * This function will release the metadata space that was not used and will
4755 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4756 * list if there are no delalloc bytes left.
4757 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004758void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4759{
4760 btrfs_delalloc_release_metadata(inode, num_bytes);
4761 btrfs_free_reserved_data_space(inode, num_bytes);
4762}
4763
Liu Boc53d6132012-12-27 09:01:19 +00004764static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004765 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004766{
Josef Bacik0af3d002010-06-21 14:48:16 -04004767 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004768 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004769 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004770 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004771 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004772 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004773
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004774 /* block accounting for super block */
4775 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02004776 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004777 if (alloc)
4778 old_val += num_bytes;
4779 else
4780 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02004781 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004782 spin_unlock(&info->delalloc_lock);
4783
Chris Masond3977122009-01-05 21:25:51 -05004784 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004785 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004786 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004787 return -ENOENT;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004788 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4789 BTRFS_BLOCK_GROUP_RAID1 |
4790 BTRFS_BLOCK_GROUP_RAID10))
4791 factor = 2;
4792 else
4793 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004794 /*
4795 * If this block group has free space cache written out, we
4796 * need to make sure to load it if we are removing space. This
4797 * is because we need the unpinning stage to actually add the
4798 * space back to the block group, otherwise we will leak space.
4799 */
4800 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00004801 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004802
Chris Masondb945352007-10-15 16:15:53 -04004803 byte_in_group = bytenr - cache->key.objectid;
4804 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004805
Josef Bacik25179202008-10-29 14:49:05 -04004806 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004807 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004808
Josef Bacik73bc1872011-10-03 14:07:49 -04004809 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04004810 cache->disk_cache_state < BTRFS_DC_CLEAR)
4811 cache->disk_cache_state = BTRFS_DC_CLEAR;
4812
Josef Bacik0f9dd462008-09-23 13:14:11 -04004813 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004814 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004815 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004816 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004817 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004818 btrfs_set_block_group_used(&cache->item, old_val);
4819 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004820 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004821 cache->space_info->bytes_used += num_bytes;
4822 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004823 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004824 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004825 } else {
Chris Masondb945352007-10-15 16:15:53 -04004826 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004827 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004828 cache->pinned += num_bytes;
4829 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004830 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004831 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);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004834
Yan, Zhengf0486c62010-05-16 10:46:25 -04004835 set_extent_dirty(info->pinned_extents,
4836 bytenr, bytenr + num_bytes - 1,
4837 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004838 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004839 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004840 total -= num_bytes;
4841 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004842 }
4843 return 0;
4844}
Chris Mason6324fbf2008-03-24 15:01:59 -04004845
Chris Masona061fc82008-05-07 11:43:44 -04004846static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4847{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004848 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004849 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004850
4851 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4852 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004853 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004854
Yan Zhengd2fb3432008-12-11 16:30:39 -05004855 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004856 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004857
4858 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004859}
4860
Yan, Zhengf0486c62010-05-16 10:46:25 -04004861static int pin_down_extent(struct btrfs_root *root,
4862 struct btrfs_block_group_cache *cache,
4863 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004864{
Yan Zheng11833d62009-09-11 16:11:19 -04004865 spin_lock(&cache->space_info->lock);
4866 spin_lock(&cache->lock);
4867 cache->pinned += num_bytes;
4868 cache->space_info->bytes_pinned += num_bytes;
4869 if (reserved) {
4870 cache->reserved -= num_bytes;
4871 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004872 }
Yan Zheng11833d62009-09-11 16:11:19 -04004873 spin_unlock(&cache->lock);
4874 spin_unlock(&cache->space_info->lock);
4875
Yan, Zhengf0486c62010-05-16 10:46:25 -04004876 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4877 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004878 return 0;
4879}
Chris Mason9078a3e2007-04-26 16:46:15 -04004880
Yan, Zhengf0486c62010-05-16 10:46:25 -04004881/*
4882 * this function must be called within transaction
4883 */
4884int btrfs_pin_extent(struct btrfs_root *root,
4885 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004886{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004887 struct btrfs_block_group_cache *cache;
4888
4889 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004890 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04004891
4892 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4893
4894 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004895 return 0;
4896}
Zheng Yane8569812008-09-26 10:05:48 -04004897
Yan, Zhengf0486c62010-05-16 10:46:25 -04004898/*
Chris Masone688b7252011-10-31 20:52:39 -04004899 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04004900 */
Liu Bodcfac412012-12-27 09:01:20 +00004901int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04004902 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004903{
Chris Masone688b7252011-10-31 20:52:39 -04004904 struct btrfs_block_group_cache *cache;
4905
4906 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004907 BUG_ON(!cache); /* Logic error */
Chris Masone688b7252011-10-31 20:52:39 -04004908
4909 /*
4910 * pull in the free space cache (if any) so that our pin
4911 * removes the free space from the cache. We have load_only set
4912 * to one because the slow code to read in the free extents does check
4913 * the pinned extents.
4914 */
Liu Bof6373bf2012-12-27 09:01:18 +00004915 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04004916
4917 pin_down_extent(root, cache, bytenr, num_bytes, 0);
4918
4919 /* remove us from the free space cache (if we're there at all) */
4920 btrfs_remove_free_space(cache, bytenr, num_bytes);
4921 btrfs_put_block_group(cache);
4922 return 0;
4923}
4924
Josef Bacikfb25e912011-07-26 17:00:46 -04004925/**
4926 * btrfs_update_reserved_bytes - update the block_group and space info counters
4927 * @cache: The cache we are manipulating
4928 * @num_bytes: The number of bytes in question
4929 * @reserve: One of the reservation enums
4930 *
4931 * This is called by the allocator when it reserves space, or by somebody who is
4932 * freeing space that was never actually used on disk. For example if you
4933 * reserve some space for a new leaf in transaction A and before transaction A
4934 * commits you free that leaf, you call this with reserve set to 0 in order to
4935 * clear the reservation.
4936 *
4937 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4938 * ENOSPC accounting. For data we handle the reservation through clearing the
4939 * delalloc bits in the io_tree. We have to do this since we could end up
4940 * allocating less disk space for the amount of data we have reserved in the
4941 * case of compression.
4942 *
4943 * If this is a reservation and the block group has become read only we cannot
4944 * make the reservation and return -EAGAIN, otherwise this function always
4945 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04004946 */
Josef Bacikfb25e912011-07-26 17:00:46 -04004947static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4948 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004949{
Josef Bacikfb25e912011-07-26 17:00:46 -04004950 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004951 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004952
Josef Bacikfb25e912011-07-26 17:00:46 -04004953 spin_lock(&space_info->lock);
4954 spin_lock(&cache->lock);
4955 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004956 if (cache->ro) {
4957 ret = -EAGAIN;
4958 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04004959 cache->reserved += num_bytes;
4960 space_info->bytes_reserved += num_bytes;
4961 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004962 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004963 "space_info", space_info->flags,
4964 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04004965 space_info->bytes_may_use -= num_bytes;
4966 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004967 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004968 } else {
4969 if (cache->ro)
4970 space_info->bytes_readonly += num_bytes;
4971 cache->reserved -= num_bytes;
4972 space_info->bytes_reserved -= num_bytes;
4973 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004974 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004975 spin_unlock(&cache->lock);
4976 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004977 return ret;
4978}
4979
Jeff Mahoney143bede2012-03-01 14:56:26 +01004980void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004981 struct btrfs_root *root)
4982{
4983 struct btrfs_fs_info *fs_info = root->fs_info;
4984 struct btrfs_caching_control *next;
4985 struct btrfs_caching_control *caching_ctl;
4986 struct btrfs_block_group_cache *cache;
4987
4988 down_write(&fs_info->extent_commit_sem);
4989
4990 list_for_each_entry_safe(caching_ctl, next,
4991 &fs_info->caching_block_groups, list) {
4992 cache = caching_ctl->block_group;
4993 if (block_group_cache_done(cache)) {
4994 cache->last_byte_to_unpin = (u64)-1;
4995 list_del_init(&caching_ctl->list);
4996 put_caching_control(caching_ctl);
4997 } else {
4998 cache->last_byte_to_unpin = caching_ctl->progress;
4999 }
5000 }
5001
5002 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5003 fs_info->pinned_extents = &fs_info->freed_extents[1];
5004 else
5005 fs_info->pinned_extents = &fs_info->freed_extents[0];
5006
5007 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005008
5009 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005010}
5011
5012static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5013{
5014 struct btrfs_fs_info *fs_info = root->fs_info;
5015 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005016 struct btrfs_space_info *space_info;
5017 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005018 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005019 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005020
5021 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005022 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005023 if (!cache ||
5024 start >= cache->key.objectid + cache->key.offset) {
5025 if (cache)
5026 btrfs_put_block_group(cache);
5027 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005028 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005029 }
5030
5031 len = cache->key.objectid + cache->key.offset - start;
5032 len = min(len, end + 1 - start);
5033
5034 if (start < cache->last_byte_to_unpin) {
5035 len = min(len, cache->last_byte_to_unpin - start);
5036 btrfs_add_free_space(cache, start, len);
5037 }
Josef Bacik25179202008-10-29 14:49:05 -04005038
Yan, Zhengf0486c62010-05-16 10:46:25 -04005039 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005040 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005041
Josef Bacik7b398f82012-10-22 15:52:28 -04005042 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005043 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005044 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005045 space_info->bytes_pinned -= len;
5046 if (cache->ro) {
5047 space_info->bytes_readonly += len;
5048 readonly = true;
5049 }
Josef Bacik25179202008-10-29 14:49:05 -04005050 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005051 if (!readonly && global_rsv->space_info == space_info) {
5052 spin_lock(&global_rsv->lock);
5053 if (!global_rsv->full) {
5054 len = min(len, global_rsv->size -
5055 global_rsv->reserved);
5056 global_rsv->reserved += len;
5057 space_info->bytes_may_use += len;
5058 if (global_rsv->reserved >= global_rsv->size)
5059 global_rsv->full = 1;
5060 }
5061 spin_unlock(&global_rsv->lock);
5062 }
5063 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005064 }
5065
5066 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005067 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005068 return 0;
5069}
5070
5071int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005072 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005073{
Yan Zheng11833d62009-09-11 16:11:19 -04005074 struct btrfs_fs_info *fs_info = root->fs_info;
5075 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005076 u64 start;
5077 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005078 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005079
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005080 if (trans->aborted)
5081 return 0;
5082
Yan Zheng11833d62009-09-11 16:11:19 -04005083 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5084 unpin = &fs_info->freed_extents[1];
5085 else
5086 unpin = &fs_info->freed_extents[0];
5087
Chris Masond3977122009-01-05 21:25:51 -05005088 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005089 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005090 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005091 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005092 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005093
Li Dongyang5378e602011-03-24 10:24:27 +00005094 if (btrfs_test_opt(root, DISCARD))
5095 ret = btrfs_discard_extent(root, start,
5096 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005097
Chris Mason1a5bc162007-10-15 16:15:26 -04005098 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005099 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005100 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005101 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005102
Chris Masone20d96d2007-03-22 12:13:20 -04005103 return 0;
5104}
5105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005106static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5107 struct btrfs_root *root,
5108 u64 bytenr, u64 num_bytes, u64 parent,
5109 u64 root_objectid, u64 owner_objectid,
5110 u64 owner_offset, int refs_to_drop,
5111 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005112{
Chris Masone2fa7222007-03-12 16:22:34 -04005113 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005114 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005115 struct btrfs_fs_info *info = root->fs_info;
5116 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005117 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005118 struct btrfs_extent_item *ei;
5119 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005120 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005121 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005122 int extent_slot = 0;
5123 int found_extent = 0;
5124 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005125 u32 item_size;
5126 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05005127
Chris Mason5caf2a02007-04-02 11:20:42 -04005128 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005129 if (!path)
5130 return -ENOMEM;
5131
Chris Mason3c12ac72008-04-21 12:01:38 -04005132 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005133 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005134
5135 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5136 BUG_ON(!is_data && refs_to_drop != 1);
5137
5138 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5139 bytenr, num_bytes, parent,
5140 root_objectid, owner_objectid,
5141 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005142 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005143 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005144 while (extent_slot >= 0) {
5145 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005146 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005147 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005148 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005149 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5150 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005151 found_extent = 1;
5152 break;
5153 }
5154 if (path->slots[0] - extent_slot > 5)
5155 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005156 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005157 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005158#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5159 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5160 if (found_extent && item_size < sizeof(*ei))
5161 found_extent = 0;
5162#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005163 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005164 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005165 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005166 NULL, refs_to_drop,
5167 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005168 if (ret) {
5169 btrfs_abort_transaction(trans, extent_root, ret);
5170 goto out;
5171 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005172 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005173 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005174
5175 key.objectid = bytenr;
5176 key.type = BTRFS_EXTENT_ITEM_KEY;
5177 key.offset = num_bytes;
5178
Zheng Yan31840ae2008-09-23 13:14:14 -04005179 ret = btrfs_search_slot(trans, extent_root,
5180 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005181 if (ret) {
5182 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05005183 ", was looking for %llu\n", ret,
5184 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005185 if (ret > 0)
5186 btrfs_print_leaf(extent_root,
5187 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005188 }
David Sterba005d6422012-09-18 07:52:32 -06005189 if (ret < 0) {
5190 btrfs_abort_transaction(trans, extent_root, ret);
5191 goto out;
5192 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005193 extent_slot = path->slots[0];
5194 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005195 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005196 btrfs_print_leaf(extent_root, path->nodes[0]);
5197 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05005198 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005199 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05005200 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04005201 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05005202 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005203 (unsigned long long)owner_objectid,
5204 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005205 } else {
David Sterba005d6422012-09-18 07:52:32 -06005206 btrfs_abort_transaction(trans, extent_root, ret);
5207 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005208 }
Chris Mason5f39d392007-10-15 16:14:19 -04005209
5210 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005211 item_size = btrfs_item_size_nr(leaf, extent_slot);
5212#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5213 if (item_size < sizeof(*ei)) {
5214 BUG_ON(found_extent || extent_slot != path->slots[0]);
5215 ret = convert_extent_item_v0(trans, extent_root, path,
5216 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005217 if (ret < 0) {
5218 btrfs_abort_transaction(trans, extent_root, ret);
5219 goto out;
5220 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005221
David Sterbab3b4aa72011-04-21 01:20:15 +02005222 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005223 path->leave_spinning = 1;
5224
5225 key.objectid = bytenr;
5226 key.type = BTRFS_EXTENT_ITEM_KEY;
5227 key.offset = num_bytes;
5228
5229 ret = btrfs_search_slot(trans, extent_root, &key, path,
5230 -1, 1);
5231 if (ret) {
5232 printk(KERN_ERR "umm, got %d back from search"
5233 ", was looking for %llu\n", ret,
5234 (unsigned long long)bytenr);
5235 btrfs_print_leaf(extent_root, path->nodes[0]);
5236 }
David Sterba005d6422012-09-18 07:52:32 -06005237 if (ret < 0) {
5238 btrfs_abort_transaction(trans, extent_root, ret);
5239 goto out;
5240 }
5241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005242 extent_slot = path->slots[0];
5243 leaf = path->nodes[0];
5244 item_size = btrfs_item_size_nr(leaf, extent_slot);
5245 }
5246#endif
5247 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005248 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005249 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005250 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5251 struct btrfs_tree_block_info *bi;
5252 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5253 bi = (struct btrfs_tree_block_info *)(ei + 1);
5254 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005255 }
5256
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005257 refs = btrfs_extent_refs(leaf, ei);
5258 BUG_ON(refs < refs_to_drop);
5259 refs -= refs_to_drop;
5260
5261 if (refs > 0) {
5262 if (extent_op)
5263 __run_delayed_extent_op(extent_op, leaf, ei);
5264 /*
5265 * In the case of inline back ref, reference count will
5266 * be updated by remove_extent_backref
5267 */
5268 if (iref) {
5269 BUG_ON(!found_extent);
5270 } else {
5271 btrfs_set_extent_refs(leaf, ei, refs);
5272 btrfs_mark_buffer_dirty(leaf);
5273 }
5274 if (found_extent) {
5275 ret = remove_extent_backref(trans, extent_root, path,
5276 iref, refs_to_drop,
5277 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005278 if (ret) {
5279 btrfs_abort_transaction(trans, extent_root, ret);
5280 goto out;
5281 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005282 }
5283 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005284 if (found_extent) {
5285 BUG_ON(is_data && refs_to_drop !=
5286 extent_data_ref_count(root, path, iref));
5287 if (iref) {
5288 BUG_ON(path->slots[0] != extent_slot);
5289 } else {
5290 BUG_ON(path->slots[0] != extent_slot + 1);
5291 path->slots[0] = extent_slot;
5292 num_to_del = 2;
5293 }
Chris Mason78fae272007-03-25 11:35:08 -04005294 }
Chris Masonb9473432009-03-13 11:00:37 -04005295
Chris Mason952fcca2008-02-18 16:33:44 -05005296 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5297 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005298 if (ret) {
5299 btrfs_abort_transaction(trans, extent_root, ret);
5300 goto out;
5301 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005302 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005303
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005304 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005305 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005306 if (ret) {
5307 btrfs_abort_transaction(trans, extent_root, ret);
5308 goto out;
5309 }
Chris Mason459931e2008-12-10 09:10:46 -05005310 }
5311
Liu Boc53d6132012-12-27 09:01:19 +00005312 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005313 if (ret) {
5314 btrfs_abort_transaction(trans, extent_root, ret);
5315 goto out;
5316 }
Chris Masona28ec192007-03-06 20:08:01 -05005317 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005318out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005319 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005320 return ret;
5321}
5322
5323/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005324 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005325 * delayed ref for that extent as well. This searches the delayed ref tree for
5326 * a given extent, and if there are no other delayed refs to be processed, it
5327 * removes it from the tree.
5328 */
5329static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5330 struct btrfs_root *root, u64 bytenr)
5331{
5332 struct btrfs_delayed_ref_head *head;
5333 struct btrfs_delayed_ref_root *delayed_refs;
5334 struct btrfs_delayed_ref_node *ref;
5335 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005336 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005337
5338 delayed_refs = &trans->transaction->delayed_refs;
5339 spin_lock(&delayed_refs->lock);
5340 head = btrfs_find_delayed_ref_head(trans, bytenr);
5341 if (!head)
5342 goto out;
5343
5344 node = rb_prev(&head->node.rb_node);
5345 if (!node)
5346 goto out;
5347
5348 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5349
5350 /* there are still entries for this ref, we can't drop it */
5351 if (ref->bytenr == bytenr)
5352 goto out;
5353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005354 if (head->extent_op) {
5355 if (!head->must_insert_reserved)
5356 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005357 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005358 head->extent_op = NULL;
5359 }
5360
Chris Mason1887be62009-03-13 10:11:24 -04005361 /*
5362 * waiting for the lock here would deadlock. If someone else has it
5363 * locked they are already in the process of dropping it anyway
5364 */
5365 if (!mutex_trylock(&head->mutex))
5366 goto out;
5367
5368 /*
5369 * at this point we have a head with no other entries. Go
5370 * ahead and process it.
5371 */
5372 head->node.in_tree = 0;
5373 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005374
Chris Mason1887be62009-03-13 10:11:24 -04005375 delayed_refs->num_entries--;
5376
5377 /*
5378 * we don't take a ref on the node because we're removing it from the
5379 * tree, so we just steal the ref the tree was holding.
5380 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005381 delayed_refs->num_heads--;
5382 if (list_empty(&head->cluster))
5383 delayed_refs->num_heads_ready--;
5384
5385 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005386 spin_unlock(&delayed_refs->lock);
5387
Yan, Zhengf0486c62010-05-16 10:46:25 -04005388 BUG_ON(head->extent_op);
5389 if (head->must_insert_reserved)
5390 ret = 1;
5391
5392 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005393 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005394 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005395out:
5396 spin_unlock(&delayed_refs->lock);
5397 return 0;
5398}
5399
Yan, Zhengf0486c62010-05-16 10:46:25 -04005400void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5401 struct btrfs_root *root,
5402 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005403 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005404{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005405 struct btrfs_block_group_cache *cache = NULL;
5406 int ret;
5407
5408 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005409 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5410 buf->start, buf->len,
5411 parent, root->root_key.objectid,
5412 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005413 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005414 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005415 }
5416
5417 if (!last_ref)
5418 return;
5419
Yan, Zhengf0486c62010-05-16 10:46:25 -04005420 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005421
5422 if (btrfs_header_generation(buf) == trans->transid) {
5423 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5424 ret = check_ref_cleanup(trans, root, buf->start);
5425 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005426 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005427 }
5428
5429 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5430 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005431 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005432 }
5433
5434 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5435
5436 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005437 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005438 }
5439out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005440 /*
5441 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5442 * anymore.
5443 */
5444 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005445 btrfs_put_block_group(cache);
5446}
5447
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005448/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005449int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5450 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5451 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005452{
5453 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005454 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005455
Chris Mason56bec292009-03-13 10:10:06 -04005456 /*
5457 * tree log blocks never actually go into the extent allocation
5458 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005459 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005460 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5461 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005462 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005463 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005464 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005465 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005466 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5467 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005468 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005469 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005470 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005471 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5472 num_bytes,
5473 parent, root_objectid, owner,
5474 offset, BTRFS_DROP_DELAYED_REF,
5475 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005476 }
Chris Mason925baed2008-06-25 16:01:30 -04005477 return ret;
5478}
5479
Chris Mason87ee04e2007-11-30 11:30:34 -05005480static u64 stripe_align(struct btrfs_root *root, u64 val)
5481{
5482 u64 mask = ((u64)root->stripesize - 1);
5483 u64 ret = (val + mask) & ~mask;
5484 return ret;
5485}
5486
Chris Masonfec577f2007-02-26 10:40:21 -05005487/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005488 * when we wait for progress in the block group caching, its because
5489 * our allocation attempt failed at least once. So, we must sleep
5490 * and let some progress happen before we try again.
5491 *
5492 * This function will sleep at least once waiting for new free space to
5493 * show up, and then it will check the block group free space numbers
5494 * for our min num_bytes. Another option is to have it go ahead
5495 * and look in the rbtree for a free extent of a given size, but this
5496 * is a good start.
5497 */
5498static noinline int
5499wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5500 u64 num_bytes)
5501{
Yan Zheng11833d62009-09-11 16:11:19 -04005502 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005503 DEFINE_WAIT(wait);
5504
Yan Zheng11833d62009-09-11 16:11:19 -04005505 caching_ctl = get_caching_control(cache);
5506 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005507 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005508
Yan Zheng11833d62009-09-11 16:11:19 -04005509 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005510 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005511
5512 put_caching_control(caching_ctl);
5513 return 0;
5514}
5515
5516static noinline int
5517wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5518{
5519 struct btrfs_caching_control *caching_ctl;
5520 DEFINE_WAIT(wait);
5521
5522 caching_ctl = get_caching_control(cache);
5523 if (!caching_ctl)
5524 return 0;
5525
5526 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5527
5528 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005529 return 0;
5530}
5531
Liu Bo31e50222012-11-21 14:18:10 +00005532int __get_raid_index(u64 flags)
Yan, Zhengb742bb822010-05-16 10:46:24 -04005533{
5534 int index;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005535
5536 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Yan, Zhengb742bb822010-05-16 10:46:24 -04005537 index = 0;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005538 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Yan, Zhengb742bb822010-05-16 10:46:24 -04005539 index = 1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005540 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Yan, Zhengb742bb822010-05-16 10:46:24 -04005541 index = 2;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005542 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Yan, Zhengb742bb822010-05-16 10:46:24 -04005543 index = 3;
5544 else
5545 index = 4;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005546
Yan, Zhengb742bb822010-05-16 10:46:24 -04005547 return index;
5548}
5549
Ilya Dryomov7738a532012-03-27 17:09:17 +03005550static int get_block_group_index(struct btrfs_block_group_cache *cache)
5551{
Liu Bo31e50222012-11-21 14:18:10 +00005552 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005553}
5554
Josef Bacik817d52f2009-07-13 21:29:25 -04005555enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005556 LOOP_CACHING_NOWAIT = 0,
5557 LOOP_CACHING_WAIT = 1,
5558 LOOP_ALLOC_CHUNK = 2,
5559 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005560};
5561
5562/*
Chris Masonfec577f2007-02-26 10:40:21 -05005563 * walks the btree of allocated extents and find a hole of a given size.
5564 * The key ins is changed to record the hole:
5565 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005566 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005567 * ins->offset == number of blocks
5568 * Any available blocks before search_start are skipped.
5569 */
Chris Masond3977122009-01-05 21:25:51 -05005570static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005571 struct btrfs_root *orig_root,
5572 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005573 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005574 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005575{
Josef Bacik80eb2342008-10-29 14:49:05 -04005576 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005577 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005578 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005579 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005580 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005581 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005582 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005583 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005584 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005585 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005586 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5587 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005588 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005589 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005590 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005591 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005592 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005593
Chris Masondb945352007-10-15 16:15:53 -04005594 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005595 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005596 ins->objectid = 0;
5597 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005598
Josef Bacik3f7de032011-11-10 08:29:20 -05005599 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5600
Josef Bacik2552d172009-04-03 10:14:19 -04005601 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005602 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00005603 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005604 return -ENOSPC;
5605 }
Josef Bacik2552d172009-04-03 10:14:19 -04005606
Josef Bacik67377732010-09-16 16:19:09 -04005607 /*
5608 * If the space info is for both data and metadata it means we have a
5609 * small filesystem and we can't use the clustering stuff.
5610 */
5611 if (btrfs_mixed_space_info(space_info))
5612 use_cluster = false;
5613
Josef Bacik67377732010-09-16 16:19:09 -04005614 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005615 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005616 if (!btrfs_test_opt(root, SSD))
5617 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005618 }
5619
Josef Bacik67377732010-09-16 16:19:09 -04005620 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5621 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005622 last_ptr = &root->fs_info->data_alloc_cluster;
5623 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005624
Chris Mason239b14b2008-03-24 15:02:07 -04005625 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005626 spin_lock(&last_ptr->lock);
5627 if (last_ptr->block_group)
5628 hint_byte = last_ptr->window_start;
5629 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005630 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005631
Chris Masona061fc82008-05-07 11:43:44 -04005632 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005633 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005634
Josef Bacik817d52f2009-07-13 21:29:25 -04005635 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005636 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005637
Josef Bacik2552d172009-04-03 10:14:19 -04005638 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005639 block_group = btrfs_lookup_block_group(root->fs_info,
5640 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005641 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005642 /*
5643 * we don't want to use the block group if it doesn't match our
5644 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005645 *
5646 * However if we are re-searching with an ideal block group
5647 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005648 */
5649 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005650 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005651 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005652 if (list_empty(&block_group->list) ||
5653 block_group->ro) {
5654 /*
5655 * someone is removing this block group,
5656 * we can't jump into the have_block_group
5657 * target because our list pointers are not
5658 * valid
5659 */
5660 btrfs_put_block_group(block_group);
5661 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005662 } else {
Yan, Zhengb742bb822010-05-16 10:46:24 -04005663 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005664 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005665 }
Josef Bacik2552d172009-04-03 10:14:19 -04005666 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005667 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005668 }
Chris Mason42e70e72008-11-07 18:17:11 -05005669 }
Josef Bacik2552d172009-04-03 10:14:19 -04005670search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005671 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005672 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04005673 list_for_each_entry(block_group, &space_info->block_groups[index],
5674 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005675 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005676 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05005677
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005678 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005679 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005680 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005681
Chris Mason83a50de2010-12-13 15:06:46 -05005682 /*
5683 * this can happen if we end up cycling through all the
5684 * raid types, but we want to make sure we only allocate
5685 * for the proper type.
5686 */
5687 if (!block_group_bits(block_group, data)) {
5688 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5689 BTRFS_BLOCK_GROUP_RAID1 |
5690 BTRFS_BLOCK_GROUP_RAID10;
5691
5692 /*
5693 * if they asked for extra copies and this block group
5694 * doesn't provide them, bail. This does allow us to
5695 * fill raid0 from raid1.
5696 */
5697 if ((data & extra) && !(block_group->flags & extra))
5698 goto loop;
5699 }
5700
Josef Bacik2552d172009-04-03 10:14:19 -04005701have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05005702 cached = block_group_cache_done(block_group);
5703 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05005704 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00005705 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005706 BUG_ON(ret < 0);
5707 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005708 }
5709
Josef Bacikea6a4782008-11-20 12:16:16 -05005710 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005711 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005712
Josef Bacik0a243252009-09-11 16:11:20 -04005713 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005714 * Ok we want to try and use the cluster allocator, so
5715 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005716 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005717 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005718 /*
5719 * the refill lock keeps out other
5720 * people trying to start a new cluster
5721 */
5722 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005723 used_block_group = last_ptr->block_group;
5724 if (used_block_group != block_group &&
5725 (!used_block_group ||
5726 used_block_group->ro ||
5727 !block_group_bits(used_block_group, data))) {
5728 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04005729 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005730 }
Chris Mason44fb5512009-06-04 15:34:51 -04005731
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005732 if (used_block_group != block_group)
5733 btrfs_get_block_group(used_block_group);
5734
5735 offset = btrfs_alloc_from_cluster(used_block_group,
5736 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005737 if (offset) {
5738 /* we have a block, we're done */
5739 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005740 trace_btrfs_reserve_extent_cluster(root,
5741 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005742 goto checks;
5743 }
5744
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005745 WARN_ON(last_ptr->block_group != used_block_group);
5746 if (used_block_group != block_group) {
5747 btrfs_put_block_group(used_block_group);
5748 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005749 }
Chris Mason44fb5512009-06-04 15:34:51 -04005750refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005751 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005752 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5753 * set up a new clusters, so lets just skip it
5754 * and let the allocator find whatever block
5755 * it can find. If we reach this point, we
5756 * will have tried the cluster allocator
5757 * plenty of times and not have found
5758 * anything, so we are likely way too
5759 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005760 * anything.
5761 *
5762 * However, if the cluster is taken from the
5763 * current block group, release the cluster
5764 * first, so that we stand a better chance of
5765 * succeeding in the unclustered
5766 * allocation. */
5767 if (loop >= LOOP_NO_EMPTY_SIZE &&
5768 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005769 spin_unlock(&last_ptr->refill_lock);
5770 goto unclustered_alloc;
5771 }
5772
Chris Masonfa9c0d792009-04-03 09:47:43 -04005773 /*
5774 * this cluster didn't work out, free it and
5775 * start over
5776 */
5777 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5778
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005779 if (loop >= LOOP_NO_EMPTY_SIZE) {
5780 spin_unlock(&last_ptr->refill_lock);
5781 goto unclustered_alloc;
5782 }
5783
Chris Masonfa9c0d792009-04-03 09:47:43 -04005784 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005785 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005786 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05005787 search_start, num_bytes,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005788 empty_cluster + empty_size);
5789 if (ret == 0) {
5790 /*
5791 * now pull our allocation out of this
5792 * cluster
5793 */
5794 offset = btrfs_alloc_from_cluster(block_group,
5795 last_ptr, num_bytes,
5796 search_start);
5797 if (offset) {
5798 /* we found one, proceed */
5799 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005800 trace_btrfs_reserve_extent_cluster(root,
5801 block_group, search_start,
5802 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005803 goto checks;
5804 }
Josef Bacik0a243252009-09-11 16:11:20 -04005805 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5806 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005807 spin_unlock(&last_ptr->refill_lock);
5808
Josef Bacik0a243252009-09-11 16:11:20 -04005809 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005810 wait_block_group_cache_progress(block_group,
5811 num_bytes + empty_cluster + empty_size);
5812 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005813 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005814
Chris Masonfa9c0d792009-04-03 09:47:43 -04005815 /*
5816 * at this point we either didn't find a cluster
5817 * or we weren't able to allocate a block from our
5818 * cluster. Free the cluster we've been trying
5819 * to use, and go to the next block group
5820 */
Josef Bacik0a243252009-09-11 16:11:20 -04005821 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005822 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005823 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005824 }
5825
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005826unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005827 spin_lock(&block_group->free_space_ctl->tree_lock);
5828 if (cached &&
5829 block_group->free_space_ctl->free_space <
5830 num_bytes + empty_cluster + empty_size) {
5831 spin_unlock(&block_group->free_space_ctl->tree_lock);
5832 goto loop;
5833 }
5834 spin_unlock(&block_group->free_space_ctl->tree_lock);
5835
Josef Bacik6226cb02009-04-03 10:14:18 -04005836 offset = btrfs_find_space_for_alloc(block_group, search_start,
5837 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005838 /*
5839 * If we didn't find a chunk, and we haven't failed on this
5840 * block group before, and this block group is in the middle of
5841 * caching and we are ok with waiting, then go ahead and wait
5842 * for progress to be made, and set failed_alloc to true.
5843 *
5844 * If failed_alloc is true then we've already waited on this
5845 * block group once and should move on to the next block group.
5846 */
5847 if (!offset && !failed_alloc && !cached &&
5848 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005849 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005850 num_bytes + empty_size);
5851 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005852 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005853 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08005854 if (!cached)
5855 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005856 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005857 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005858checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005859 search_start = stripe_align(root, offset);
Chris Masone37c9e62007-05-09 20:13:14 -04005860
Josef Bacik2552d172009-04-03 10:14:19 -04005861 /* move on to the next group */
5862 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005863 used_block_group->key.objectid + used_block_group->key.offset) {
5864 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005865 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005866 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005867
Josef Bacik6226cb02009-04-03 10:14:18 -04005868 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005869 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04005870 search_start - offset);
5871 BUG_ON(offset > search_start);
5872
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005873 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04005874 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005875 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005876 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005877 goto loop;
5878 }
Yan Zheng11833d62009-09-11 16:11:19 -04005879
Josef Bacik2552d172009-04-03 10:14:19 -04005880 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005881 ins->objectid = search_start;
5882 ins->offset = num_bytes;
5883
Josef Bacik3f7de032011-11-10 08:29:20 -05005884 trace_btrfs_reserve_extent(orig_root, block_group,
5885 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005886 if (used_block_group != block_group)
5887 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005888 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005889 break;
5890loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005891 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005892 failed_alloc = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005893 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005894 if (used_block_group != block_group)
5895 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005896 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005897 }
5898 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005899
Miao Xie60d2adb2011-09-09 17:34:35 +08005900 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
5901 goto search;
5902
Yan, Zhengb742bb822010-05-16 10:46:24 -04005903 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5904 goto search;
5905
Josef Bacik285ff5a2012-01-13 15:27:45 -05005906 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005907 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5908 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005909 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5910 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5911 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5912 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005913 */
Josef Bacik723bda22011-05-27 16:11:38 -04005914 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04005915 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04005916 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005917 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04005918 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04005919 CHUNK_ALLOC_FORCE);
5920 /*
5921 * Do not bail out on ENOSPC since we
5922 * can do more things.
5923 */
5924 if (ret < 0 && ret != -ENOSPC) {
5925 btrfs_abort_transaction(trans,
5926 root, ret);
5927 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04005928 }
Josef Bacik723bda22011-05-27 16:11:38 -04005929 }
5930
5931 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005932 empty_size = 0;
5933 empty_cluster = 0;
5934 }
Chris Mason42e70e72008-11-07 18:17:11 -05005935
Josef Bacik723bda22011-05-27 16:11:38 -04005936 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005937 } else if (!ins->objectid) {
5938 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005939 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005940 ret = 0;
5941 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005942out:
Chris Mason0b86a832008-03-24 15:01:56 -04005943
Chris Mason0f70abe2007-02-28 16:46:22 -05005944 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005945}
Chris Masonec44a352008-04-28 15:29:52 -04005946
Josef Bacik9ed74f22009-09-11 16:12:44 -04005947static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5948 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005949{
5950 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005951 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005952
Josef Bacik9ed74f22009-09-11 16:12:44 -04005953 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04005954 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
5955 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05005956 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005957 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005958 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005959 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005960 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5961 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005962 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005963 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005964 (unsigned long long)info->bytes_pinned,
5965 (unsigned long long)info->bytes_reserved,
5966 (unsigned long long)info->bytes_may_use,
5967 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005968 spin_unlock(&info->lock);
5969
5970 if (!dump_block_groups)
5971 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005972
Josef Bacik80eb2342008-10-29 14:49:05 -04005973 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04005974again:
5975 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005976 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06005977 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05005978 (unsigned long long)cache->key.objectid,
5979 (unsigned long long)cache->key.offset,
5980 (unsigned long long)btrfs_block_group_used(&cache->item),
5981 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06005982 (unsigned long long)cache->reserved,
5983 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04005984 btrfs_dump_free_space(cache, bytes);
5985 spin_unlock(&cache->lock);
5986 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04005987 if (++index < BTRFS_NR_RAID_TYPES)
5988 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005989 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005990}
Zheng Yane8569812008-09-26 10:05:48 -04005991
Yan Zheng11833d62009-09-11 16:11:19 -04005992int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5993 struct btrfs_root *root,
5994 u64 num_bytes, u64 min_alloc_size,
5995 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005996 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005997{
Miao Xie9e622d62012-01-26 15:01:12 -05005998 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005999 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006000
Josef Bacik6a632092009-02-20 11:00:09 -05006001 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006002again:
Chris Masondb945352007-10-15 16:15:53 -04006003 WARN_ON(num_bytes < root->sectorsize);
6004 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006005 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006006
Miao Xie9e622d62012-01-26 15:01:12 -05006007 if (ret == -ENOSPC) {
6008 if (!final_tried) {
6009 num_bytes = num_bytes >> 1;
6010 num_bytes = num_bytes & ~(root->sectorsize - 1);
6011 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006012 if (num_bytes == min_alloc_size)
6013 final_tried = true;
6014 goto again;
6015 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6016 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006017
Miao Xie9e622d62012-01-26 15:01:12 -05006018 sinfo = __find_space_info(root->fs_info, data);
6019 printk(KERN_ERR "btrfs allocation failed flags %llu, "
6020 "wanted %llu\n", (unsigned long long)data,
6021 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006022 if (sinfo)
6023 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006024 }
Chris Mason925baed2008-06-25 16:01:30 -04006025 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006026
liubo1abe9b82011-03-24 11:18:59 +00006027 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6028
Josef Bacik0f9dd462008-09-23 13:14:11 -04006029 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006030}
6031
Chris Masone688b7252011-10-31 20:52:39 -04006032static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6033 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006034{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006035 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006036 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006037
Josef Bacik0f9dd462008-09-23 13:14:11 -04006038 cache = btrfs_lookup_block_group(root->fs_info, start);
6039 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05006040 printk(KERN_ERR "Unable to find block group for %llu\n",
6041 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006042 return -ENOSPC;
6043 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006044
Li Dongyang5378e602011-03-24 10:24:27 +00006045 if (btrfs_test_opt(root, DISCARD))
6046 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006047
Chris Masone688b7252011-10-31 20:52:39 -04006048 if (pin)
6049 pin_down_extent(root, cache, start, len, 1);
6050 else {
6051 btrfs_add_free_space(cache, start, len);
6052 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6053 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006054 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006055
liubo1abe9b82011-03-24 11:18:59 +00006056 trace_btrfs_reserved_extent_free(root, start, len);
6057
Chris Masone6dcd2d2008-07-17 12:53:50 -04006058 return ret;
6059}
6060
Chris Masone688b7252011-10-31 20:52:39 -04006061int btrfs_free_reserved_extent(struct btrfs_root *root,
6062 u64 start, u64 len)
6063{
6064 return __btrfs_free_reserved_extent(root, start, len, 0);
6065}
6066
6067int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6068 u64 start, u64 len)
6069{
6070 return __btrfs_free_reserved_extent(root, start, len, 1);
6071}
6072
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006073static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6074 struct btrfs_root *root,
6075 u64 parent, u64 root_objectid,
6076 u64 flags, u64 owner, u64 offset,
6077 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006078{
6079 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006080 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006081 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006082 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006083 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006084 struct extent_buffer *leaf;
6085 int type;
6086 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006087
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006088 if (parent > 0)
6089 type = BTRFS_SHARED_DATA_REF_KEY;
6090 else
6091 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006092
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006093 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006094
6095 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006096 if (!path)
6097 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006098
Chris Masonb9473432009-03-13 11:00:37 -04006099 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006100 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6101 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006102 if (ret) {
6103 btrfs_free_path(path);
6104 return ret;
6105 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006106
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006107 leaf = path->nodes[0];
6108 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006109 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006110 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6111 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6112 btrfs_set_extent_flags(leaf, extent_item,
6113 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006114
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006115 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6116 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6117 if (parent > 0) {
6118 struct btrfs_shared_data_ref *ref;
6119 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6120 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6121 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6122 } else {
6123 struct btrfs_extent_data_ref *ref;
6124 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6125 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6126 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6127 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6128 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6129 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006130
6131 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006132 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006133
Liu Boc53d6132012-12-27 09:01:19 +00006134 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006135 if (ret) { /* -ENOENT, logic error */
Chris Masond3977122009-01-05 21:25:51 -05006136 printk(KERN_ERR "btrfs update block group failed for %llu "
6137 "%llu\n", (unsigned long long)ins->objectid,
6138 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006139 BUG();
6140 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006141 return ret;
6142}
6143
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006144static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6145 struct btrfs_root *root,
6146 u64 parent, u64 root_objectid,
6147 u64 flags, struct btrfs_disk_key *key,
6148 int level, struct btrfs_key *ins)
6149{
6150 int ret;
6151 struct btrfs_fs_info *fs_info = root->fs_info;
6152 struct btrfs_extent_item *extent_item;
6153 struct btrfs_tree_block_info *block_info;
6154 struct btrfs_extent_inline_ref *iref;
6155 struct btrfs_path *path;
6156 struct extent_buffer *leaf;
6157 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
6158
6159 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006160 if (!path)
6161 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006162
6163 path->leave_spinning = 1;
6164 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6165 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006166 if (ret) {
6167 btrfs_free_path(path);
6168 return ret;
6169 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006170
6171 leaf = path->nodes[0];
6172 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6173 struct btrfs_extent_item);
6174 btrfs_set_extent_refs(leaf, extent_item, 1);
6175 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6176 btrfs_set_extent_flags(leaf, extent_item,
6177 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6178 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6179
6180 btrfs_set_tree_block_key(leaf, block_info, key);
6181 btrfs_set_tree_block_level(leaf, block_info, level);
6182
6183 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6184 if (parent > 0) {
6185 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6186 btrfs_set_extent_inline_ref_type(leaf, iref,
6187 BTRFS_SHARED_BLOCK_REF_KEY);
6188 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6189 } else {
6190 btrfs_set_extent_inline_ref_type(leaf, iref,
6191 BTRFS_TREE_BLOCK_REF_KEY);
6192 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6193 }
6194
6195 btrfs_mark_buffer_dirty(leaf);
6196 btrfs_free_path(path);
6197
Liu Boc53d6132012-12-27 09:01:19 +00006198 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006199 if (ret) { /* -ENOENT, logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006200 printk(KERN_ERR "btrfs update block group failed for %llu "
6201 "%llu\n", (unsigned long long)ins->objectid,
6202 (unsigned long long)ins->offset);
6203 BUG();
6204 }
6205 return ret;
6206}
6207
6208int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6209 struct btrfs_root *root,
6210 u64 root_objectid, u64 owner,
6211 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006212{
6213 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04006214
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006215 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006216
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006217 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6218 ins->offset, 0,
6219 root_objectid, owner, offset,
6220 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006221 return ret;
6222}
Chris Masone02119d2008-09-05 16:13:11 -04006223
6224/*
6225 * this is used by the tree logging recovery code. It records that
6226 * an extent has been allocated and makes sure to clear the free
6227 * space cache bits as well
6228 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006229int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6230 struct btrfs_root *root,
6231 u64 root_objectid, u64 owner, u64 offset,
6232 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006233{
6234 int ret;
6235 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006236 struct btrfs_caching_control *caching_ctl;
6237 u64 start = ins->objectid;
6238 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006239
Chris Masone02119d2008-09-05 16:13:11 -04006240 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006241 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006242 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006243
Yan Zheng11833d62009-09-11 16:11:19 -04006244 if (!caching_ctl) {
6245 BUG_ON(!block_group_cache_done(block_group));
6246 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006247 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006248 } else {
6249 mutex_lock(&caching_ctl->mutex);
6250
6251 if (start >= caching_ctl->progress) {
6252 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006253 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006254 } else if (start + num_bytes <= caching_ctl->progress) {
6255 ret = btrfs_remove_free_space(block_group,
6256 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006257 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006258 } else {
6259 num_bytes = caching_ctl->progress - start;
6260 ret = btrfs_remove_free_space(block_group,
6261 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006262 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006263
6264 start = caching_ctl->progress;
6265 num_bytes = ins->objectid + ins->offset -
6266 caching_ctl->progress;
6267 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006268 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006269 }
6270
6271 mutex_unlock(&caching_ctl->mutex);
6272 put_caching_control(caching_ctl);
6273 }
6274
Josef Bacikfb25e912011-07-26 17:00:46 -04006275 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6276 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006277 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d792009-04-03 09:47:43 -04006278 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006279 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6280 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006281 return ret;
6282}
6283
Chris Mason65b51a02008-08-01 15:11:20 -04006284struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6285 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006286 u64 bytenr, u32 blocksize,
6287 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006288{
6289 struct extent_buffer *buf;
6290
6291 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6292 if (!buf)
6293 return ERR_PTR(-ENOMEM);
6294 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006295 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006296 btrfs_tree_lock(buf);
6297 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006298 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006299
6300 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006301 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006302
Chris Masond0c803c2008-09-11 16:17:57 -04006303 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006304 /*
6305 * we allow two log transactions at a time, use different
6306 * EXENT bit to differentiate dirty pages.
6307 */
6308 if (root->log_transid % 2 == 0)
6309 set_extent_dirty(&root->dirty_log_pages, buf->start,
6310 buf->start + buf->len - 1, GFP_NOFS);
6311 else
6312 set_extent_new(&root->dirty_log_pages, buf->start,
6313 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006314 } else {
6315 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6316 buf->start + buf->len - 1, GFP_NOFS);
6317 }
Chris Mason65b51a02008-08-01 15:11:20 -04006318 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006319 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006320 return buf;
6321}
6322
Yan, Zhengf0486c62010-05-16 10:46:25 -04006323static struct btrfs_block_rsv *
6324use_block_rsv(struct btrfs_trans_handle *trans,
6325 struct btrfs_root *root, u32 blocksize)
6326{
6327 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006328 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006329 int ret;
6330
6331 block_rsv = get_block_rsv(trans, root);
6332
6333 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006334 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6335 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006336 /*
6337 * If we couldn't reserve metadata bytes try and use some from
6338 * the global reserve.
6339 */
6340 if (ret && block_rsv != global_rsv) {
6341 ret = block_rsv_use_bytes(global_rsv, blocksize);
6342 if (!ret)
6343 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006344 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006345 } else if (ret) {
6346 return ERR_PTR(ret);
6347 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006348 return block_rsv;
6349 }
6350
6351 ret = block_rsv_use_bytes(block_rsv, blocksize);
6352 if (!ret)
6353 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006354 if (ret && !block_rsv->failfast) {
David Sterbadff51cd2011-06-14 12:52:17 +02006355 static DEFINE_RATELIMIT_STATE(_rs,
6356 DEFAULT_RATELIMIT_INTERVAL,
6357 /*DEFAULT_RATELIMIT_BURST*/ 2);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006358 if (__ratelimit(&_rs))
6359 WARN(1, KERN_DEBUG "btrfs: block rsv returned %d\n",
6360 ret);
Miao Xie08e007d2012-10-16 11:33:38 +00006361 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6362 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006363 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006364 return block_rsv;
6365 } else if (ret && block_rsv != global_rsv) {
6366 ret = block_rsv_use_bytes(global_rsv, blocksize);
6367 if (!ret)
6368 return global_rsv;
6369 }
6370 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006371
Yan, Zhengf0486c62010-05-16 10:46:25 -04006372 return ERR_PTR(-ENOSPC);
6373}
6374
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006375static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6376 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006377{
6378 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006379 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006380}
6381
Chris Masonfec577f2007-02-26 10:40:21 -05006382/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006383 * finds a free extent and does all the dirty work required for allocation
6384 * returns the key for the extent through ins, and a tree buffer for
6385 * the first block of the extent through buf.
6386 *
Chris Masonfec577f2007-02-26 10:40:21 -05006387 * returns the tree buffer or NULL.
6388 */
Chris Mason5f39d392007-10-15 16:14:19 -04006389struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006390 struct btrfs_root *root, u32 blocksize,
6391 u64 parent, u64 root_objectid,
6392 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006393 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006394{
Chris Masone2fa7222007-03-12 16:22:34 -04006395 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006396 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006397 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006398 u64 flags = 0;
6399 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006400
Yan, Zhengf0486c62010-05-16 10:46:25 -04006401
6402 block_rsv = use_block_rsv(trans, root, blocksize);
6403 if (IS_ERR(block_rsv))
6404 return ERR_CAST(block_rsv);
6405
6406 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006407 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006408 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006409 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006410 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006411 }
Chris Mason55c69072008-01-09 15:55:33 -05006412
Chris Mason4008c042009-02-12 14:09:45 -05006413 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6414 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006415 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006416
6417 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6418 if (parent == 0)
6419 parent = ins.objectid;
6420 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6421 } else
6422 BUG_ON(parent > 0);
6423
6424 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6425 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006426 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006427 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006428 if (key)
6429 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6430 else
6431 memset(&extent_op->key, 0, sizeof(extent_op->key));
6432 extent_op->flags_to_set = flags;
6433 extent_op->update_key = 1;
6434 extent_op->update_flags = 1;
6435 extent_op->is_data = 0;
6436
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006437 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6438 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006439 ins.offset, parent, root_objectid,
6440 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006441 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006442 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006443 }
Chris Masonfec577f2007-02-26 10:40:21 -05006444 return buf;
6445}
Chris Masona28ec192007-03-06 20:08:01 -05006446
Yan Zheng2c47e6052009-06-27 21:07:35 -04006447struct walk_control {
6448 u64 refs[BTRFS_MAX_LEVEL];
6449 u64 flags[BTRFS_MAX_LEVEL];
6450 struct btrfs_key update_progress;
6451 int stage;
6452 int level;
6453 int shared_level;
6454 int update_ref;
6455 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006456 int reada_slot;
6457 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006458 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006459};
6460
6461#define DROP_REFERENCE 1
6462#define UPDATE_BACKREF 2
6463
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006464static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6465 struct btrfs_root *root,
6466 struct walk_control *wc,
6467 struct btrfs_path *path)
6468{
6469 u64 bytenr;
6470 u64 generation;
6471 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006472 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006473 u32 nritems;
6474 u32 blocksize;
6475 struct btrfs_key key;
6476 struct extent_buffer *eb;
6477 int ret;
6478 int slot;
6479 int nread = 0;
6480
6481 if (path->slots[wc->level] < wc->reada_slot) {
6482 wc->reada_count = wc->reada_count * 2 / 3;
6483 wc->reada_count = max(wc->reada_count, 2);
6484 } else {
6485 wc->reada_count = wc->reada_count * 3 / 2;
6486 wc->reada_count = min_t(int, wc->reada_count,
6487 BTRFS_NODEPTRS_PER_BLOCK(root));
6488 }
6489
6490 eb = path->nodes[wc->level];
6491 nritems = btrfs_header_nritems(eb);
6492 blocksize = btrfs_level_size(root, wc->level - 1);
6493
6494 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6495 if (nread >= wc->reada_count)
6496 break;
6497
6498 cond_resched();
6499 bytenr = btrfs_node_blockptr(eb, slot);
6500 generation = btrfs_node_ptr_generation(eb, slot);
6501
6502 if (slot == path->slots[wc->level])
6503 goto reada;
6504
6505 if (wc->stage == UPDATE_BACKREF &&
6506 generation <= root->root_key.offset)
6507 continue;
6508
Yan, Zheng94fcca92009-10-09 09:25:16 -04006509 /* We don't lock the tree block, it's OK to be racy here */
6510 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6511 &refs, &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006512 /* We don't care about errors in readahead. */
6513 if (ret < 0)
6514 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006515 BUG_ON(refs == 0);
6516
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006517 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006518 if (refs == 1)
6519 goto reada;
6520
Yan, Zheng94fcca92009-10-09 09:25:16 -04006521 if (wc->level == 1 &&
6522 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6523 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006524 if (!wc->update_ref ||
6525 generation <= root->root_key.offset)
6526 continue;
6527 btrfs_node_key_to_cpu(eb, &key, slot);
6528 ret = btrfs_comp_cpu_keys(&key,
6529 &wc->update_progress);
6530 if (ret < 0)
6531 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006532 } else {
6533 if (wc->level == 1 &&
6534 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6535 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006536 }
6537reada:
6538 ret = readahead_tree_block(root, bytenr, blocksize,
6539 generation);
6540 if (ret)
6541 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006542 nread++;
6543 }
6544 wc->reada_slot = slot;
6545}
6546
Chris Mason9aca1d52007-03-13 11:09:37 -04006547/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006548 * hepler to process tree block while walking down the tree.
6549 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006550 * when wc->stage == UPDATE_BACKREF, this function updates
6551 * back refs for pointers in the block.
6552 *
6553 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006554 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006555static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6556 struct btrfs_root *root,
6557 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006558 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006559{
6560 int level = wc->level;
6561 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006562 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6563 int ret;
6564
6565 if (wc->stage == UPDATE_BACKREF &&
6566 btrfs_header_owner(eb) != root->root_key.objectid)
6567 return 1;
6568
6569 /*
6570 * when reference count of tree block is 1, it won't increase
6571 * again. once full backref flag is set, we never clear it.
6572 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006573 if (lookup_info &&
6574 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6575 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006576 BUG_ON(!path->locks[level]);
6577 ret = btrfs_lookup_extent_info(trans, root,
6578 eb->start, eb->len,
6579 &wc->refs[level],
6580 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006581 BUG_ON(ret == -ENOMEM);
6582 if (ret)
6583 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006584 BUG_ON(wc->refs[level] == 0);
6585 }
6586
Yan Zheng2c47e6052009-06-27 21:07:35 -04006587 if (wc->stage == DROP_REFERENCE) {
6588 if (wc->refs[level] > 1)
6589 return 1;
6590
6591 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006592 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006593 path->locks[level] = 0;
6594 }
6595 return 0;
6596 }
6597
6598 /* wc->stage == UPDATE_BACKREF */
6599 if (!(wc->flags[level] & flag)) {
6600 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006601 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006602 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006603 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006604 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006605 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6606 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006607 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006608 wc->flags[level] |= flag;
6609 }
6610
6611 /*
6612 * the block is shared by multiple trees, so it's not good to
6613 * keep the tree lock
6614 */
6615 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006616 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006617 path->locks[level] = 0;
6618 }
6619 return 0;
6620}
6621
6622/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006623 * hepler to process tree block pointer.
6624 *
6625 * when wc->stage == DROP_REFERENCE, this function checks
6626 * reference count of the block pointed to. if the block
6627 * is shared and we need update back refs for the subtree
6628 * rooted at the block, this function changes wc->stage to
6629 * UPDATE_BACKREF. if the block is shared and there is no
6630 * need to update back, this function drops the reference
6631 * to the block.
6632 *
6633 * NOTE: return value 1 means we should stop walking down.
6634 */
6635static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6636 struct btrfs_root *root,
6637 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006638 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006639{
6640 u64 bytenr;
6641 u64 generation;
6642 u64 parent;
6643 u32 blocksize;
6644 struct btrfs_key key;
6645 struct extent_buffer *next;
6646 int level = wc->level;
6647 int reada = 0;
6648 int ret = 0;
6649
6650 generation = btrfs_node_ptr_generation(path->nodes[level],
6651 path->slots[level]);
6652 /*
6653 * if the lower level block was created before the snapshot
6654 * was created, we know there is no need to update back refs
6655 * for the subtree
6656 */
6657 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006658 generation <= root->root_key.offset) {
6659 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006660 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006661 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006662
6663 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6664 blocksize = btrfs_level_size(root, level - 1);
6665
6666 next = btrfs_find_tree_block(root, bytenr, blocksize);
6667 if (!next) {
6668 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00006669 if (!next)
6670 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006671 reada = 1;
6672 }
6673 btrfs_tree_lock(next);
6674 btrfs_set_lock_blocking(next);
6675
Yan, Zheng94fcca92009-10-09 09:25:16 -04006676 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6677 &wc->refs[level - 1],
6678 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006679 if (ret < 0) {
6680 btrfs_tree_unlock(next);
6681 return ret;
6682 }
6683
Yan, Zheng94fcca92009-10-09 09:25:16 -04006684 BUG_ON(wc->refs[level - 1] == 0);
6685 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006686
Yan, Zheng94fcca92009-10-09 09:25:16 -04006687 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006688 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006689 if (level == 1 &&
6690 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6691 goto skip;
6692
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006693 if (!wc->update_ref ||
6694 generation <= root->root_key.offset)
6695 goto skip;
6696
6697 btrfs_node_key_to_cpu(path->nodes[level], &key,
6698 path->slots[level]);
6699 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6700 if (ret < 0)
6701 goto skip;
6702
6703 wc->stage = UPDATE_BACKREF;
6704 wc->shared_level = level - 1;
6705 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006706 } else {
6707 if (level == 1 &&
6708 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6709 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006710 }
6711
Chris Masonb9fab912012-05-06 07:23:47 -04006712 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006713 btrfs_tree_unlock(next);
6714 free_extent_buffer(next);
6715 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006716 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006717 }
6718
6719 if (!next) {
6720 if (reada && level == 1)
6721 reada_walk_down(trans, root, wc, path);
6722 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006723 if (!next)
6724 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006725 btrfs_tree_lock(next);
6726 btrfs_set_lock_blocking(next);
6727 }
6728
6729 level--;
6730 BUG_ON(level != btrfs_header_level(next));
6731 path->nodes[level] = next;
6732 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006733 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006734 wc->level = level;
6735 if (wc->level == 1)
6736 wc->reada_slot = 0;
6737 return 0;
6738skip:
6739 wc->refs[level - 1] = 0;
6740 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006741 if (wc->stage == DROP_REFERENCE) {
6742 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6743 parent = path->nodes[level]->start;
6744 } else {
6745 BUG_ON(root->root_key.objectid !=
6746 btrfs_header_owner(path->nodes[level]));
6747 parent = 0;
6748 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006749
Yan, Zheng94fcca92009-10-09 09:25:16 -04006750 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006751 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006752 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006753 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006754 btrfs_tree_unlock(next);
6755 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006756 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006757 return 1;
6758}
6759
6760/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006761 * hepler to process tree block while walking up the tree.
6762 *
6763 * when wc->stage == DROP_REFERENCE, this function drops
6764 * reference count on the block.
6765 *
6766 * when wc->stage == UPDATE_BACKREF, this function changes
6767 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6768 * to UPDATE_BACKREF previously while processing the block.
6769 *
6770 * NOTE: return value 1 means we should stop walking up.
6771 */
6772static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6773 struct btrfs_root *root,
6774 struct btrfs_path *path,
6775 struct walk_control *wc)
6776{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006777 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006778 int level = wc->level;
6779 struct extent_buffer *eb = path->nodes[level];
6780 u64 parent = 0;
6781
6782 if (wc->stage == UPDATE_BACKREF) {
6783 BUG_ON(wc->shared_level < level);
6784 if (level < wc->shared_level)
6785 goto out;
6786
Yan Zheng2c47e6052009-06-27 21:07:35 -04006787 ret = find_next_key(path, level + 1, &wc->update_progress);
6788 if (ret > 0)
6789 wc->update_ref = 0;
6790
6791 wc->stage = DROP_REFERENCE;
6792 wc->shared_level = -1;
6793 path->slots[level] = 0;
6794
6795 /*
6796 * check reference count again if the block isn't locked.
6797 * we should start walking down the tree again if reference
6798 * count is one.
6799 */
6800 if (!path->locks[level]) {
6801 BUG_ON(level == 0);
6802 btrfs_tree_lock(eb);
6803 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006804 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006805
6806 ret = btrfs_lookup_extent_info(trans, root,
6807 eb->start, eb->len,
6808 &wc->refs[level],
6809 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006810 if (ret < 0) {
6811 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006812 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006813 return ret;
6814 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006815 BUG_ON(wc->refs[level] == 0);
6816 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006817 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006818 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006819 return 1;
6820 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006821 }
6822 }
6823
6824 /* wc->stage == DROP_REFERENCE */
6825 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6826
6827 if (wc->refs[level] == 1) {
6828 if (level == 0) {
6829 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006830 ret = btrfs_dec_ref(trans, root, eb, 1,
6831 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006832 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006833 ret = btrfs_dec_ref(trans, root, eb, 0,
6834 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006835 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006836 }
6837 /* make block locked assertion in clean_tree_block happy */
6838 if (!path->locks[level] &&
6839 btrfs_header_generation(eb) == trans->transid) {
6840 btrfs_tree_lock(eb);
6841 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006842 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006843 }
6844 clean_tree_block(trans, root, eb);
6845 }
6846
6847 if (eb == root->node) {
6848 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6849 parent = eb->start;
6850 else
6851 BUG_ON(root->root_key.objectid !=
6852 btrfs_header_owner(eb));
6853 } else {
6854 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6855 parent = path->nodes[level + 1]->start;
6856 else
6857 BUG_ON(root->root_key.objectid !=
6858 btrfs_header_owner(path->nodes[level + 1]));
6859 }
6860
Jan Schmidt5581a512012-05-16 17:04:52 +02006861 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006862out:
6863 wc->refs[level] = 0;
6864 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006865 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006866}
6867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006868static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6869 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006870 struct btrfs_path *path,
6871 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006872{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006873 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006874 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006875 int ret;
6876
Yan Zheng2c47e6052009-06-27 21:07:35 -04006877 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006878 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006879 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006880 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006881
Yan Zheng2c47e6052009-06-27 21:07:35 -04006882 if (level == 0)
6883 break;
6884
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006885 if (path->slots[level] >=
6886 btrfs_header_nritems(path->nodes[level]))
6887 break;
6888
Yan, Zheng94fcca92009-10-09 09:25:16 -04006889 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006890 if (ret > 0) {
6891 path->slots[level]++;
6892 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006893 } else if (ret < 0)
6894 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006895 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006896 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006897 return 0;
6898}
6899
Chris Masond3977122009-01-05 21:25:51 -05006900static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006901 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006902 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006903 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006904{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006905 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006906 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006907
Yan Zheng2c47e6052009-06-27 21:07:35 -04006908 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6909 while (level < max_level && path->nodes[level]) {
6910 wc->level = level;
6911 if (path->slots[level] + 1 <
6912 btrfs_header_nritems(path->nodes[level])) {
6913 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006914 return 0;
6915 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006916 ret = walk_up_proc(trans, root, path, wc);
6917 if (ret > 0)
6918 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006919
Yan Zheng2c47e6052009-06-27 21:07:35 -04006920 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006921 btrfs_tree_unlock_rw(path->nodes[level],
6922 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006923 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006924 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006925 free_extent_buffer(path->nodes[level]);
6926 path->nodes[level] = NULL;
6927 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006928 }
6929 }
6930 return 1;
6931}
6932
Chris Mason9aca1d52007-03-13 11:09:37 -04006933/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006934 * drop a subvolume tree.
6935 *
6936 * this function traverses the tree freeing any blocks that only
6937 * referenced by the tree.
6938 *
6939 * when a shared tree block is found. this function decreases its
6940 * reference count by one. if update_ref is true, this function
6941 * also make sure backrefs for the shared block and all lower level
6942 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006943 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04006944int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006945 struct btrfs_block_rsv *block_rsv, int update_ref,
6946 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05006947{
Chris Mason5caf2a02007-04-02 11:20:42 -04006948 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006949 struct btrfs_trans_handle *trans;
6950 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006951 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006952 struct walk_control *wc;
6953 struct btrfs_key key;
6954 int err = 0;
6955 int ret;
6956 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006957
Chris Mason5caf2a02007-04-02 11:20:42 -04006958 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006959 if (!path) {
6960 err = -ENOMEM;
6961 goto out;
6962 }
Chris Mason20524f02007-03-10 06:35:47 -05006963
Yan Zheng2c47e6052009-06-27 21:07:35 -04006964 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07006965 if (!wc) {
6966 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006967 err = -ENOMEM;
6968 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07006969 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006970
Yan, Zhenga22285a2010-05-16 10:48:46 -04006971 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006972 if (IS_ERR(trans)) {
6973 err = PTR_ERR(trans);
6974 goto out_free;
6975 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006976
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006977 if (block_rsv)
6978 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006979
Chris Mason9f3a7422007-08-07 15:52:19 -04006980 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006981 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006982 path->nodes[level] = btrfs_lock_root_node(root);
6983 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006984 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006985 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006986 memset(&wc->update_progress, 0,
6987 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006988 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006989 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006990 memcpy(&wc->update_progress, &key,
6991 sizeof(wc->update_progress));
6992
Chris Mason6702ed42007-08-07 16:15:09 -04006993 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006994 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006995 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006996 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6997 path->lowest_level = 0;
6998 if (ret < 0) {
6999 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007000 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007001 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007002 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007003
Chris Mason7d9eb122008-07-08 14:19:17 -04007004 /*
7005 * unlock our path, this is safe because only this
7006 * function is allowed to delete this snapshot
7007 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007008 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007009
Yan Zheng2c47e6052009-06-27 21:07:35 -04007010 level = btrfs_header_level(root->node);
7011 while (1) {
7012 btrfs_tree_lock(path->nodes[level]);
7013 btrfs_set_lock_blocking(path->nodes[level]);
7014
7015 ret = btrfs_lookup_extent_info(trans, root,
7016 path->nodes[level]->start,
7017 path->nodes[level]->len,
7018 &wc->refs[level],
7019 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007020 if (ret < 0) {
7021 err = ret;
7022 goto out_end_trans;
7023 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007024 BUG_ON(wc->refs[level] == 0);
7025
7026 if (level == root_item->drop_level)
7027 break;
7028
7029 btrfs_tree_unlock(path->nodes[level]);
7030 WARN_ON(wc->refs[level] != 1);
7031 level--;
7032 }
7033 }
7034
7035 wc->level = level;
7036 wc->shared_level = -1;
7037 wc->stage = DROP_REFERENCE;
7038 wc->update_ref = update_ref;
7039 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007040 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007041 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007042
7043 while (1) {
7044 ret = walk_down_tree(trans, root, path, wc);
7045 if (ret < 0) {
7046 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007047 break;
7048 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007049
7050 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7051 if (ret < 0) {
7052 err = ret;
7053 break;
7054 }
7055
7056 if (ret > 0) {
7057 BUG_ON(wc->stage != DROP_REFERENCE);
7058 break;
7059 }
7060
7061 if (wc->stage == DROP_REFERENCE) {
7062 level = wc->level;
7063 btrfs_node_key(path->nodes[level],
7064 &root_item->drop_progress,
7065 path->slots[level]);
7066 root_item->drop_level = level;
7067 }
7068
7069 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007070 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007071 ret = btrfs_update_root(trans, tree_root,
7072 &root->root_key,
7073 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007074 if (ret) {
7075 btrfs_abort_transaction(trans, tree_root, ret);
7076 err = ret;
7077 goto out_end_trans;
7078 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007079
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007080 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007081 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007082 if (IS_ERR(trans)) {
7083 err = PTR_ERR(trans);
7084 goto out_free;
7085 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007086 if (block_rsv)
7087 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007088 }
Chris Mason20524f02007-03-10 06:35:47 -05007089 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007090 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007091 if (err)
7092 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007093
7094 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007095 if (ret) {
7096 btrfs_abort_transaction(trans, tree_root, ret);
7097 goto out_end_trans;
7098 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007099
Yan, Zheng76dda932009-09-21 16:00:26 -04007100 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7101 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7102 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007103 if (ret < 0) {
7104 btrfs_abort_transaction(trans, tree_root, ret);
7105 err = ret;
7106 goto out_end_trans;
7107 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007108 /* if we fail to delete the orphan item this time
7109 * around, it'll get picked up the next time.
7110 *
7111 * The most common failure here is just -ENOENT.
7112 */
7113 btrfs_del_orphan_item(trans, tree_root,
7114 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007115 }
7116 }
7117
7118 if (root->in_radix) {
7119 btrfs_free_fs_root(tree_root->fs_info, root);
7120 } else {
7121 free_extent_buffer(root->node);
7122 free_extent_buffer(root->commit_root);
7123 kfree(root);
7124 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007125out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007126 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007127out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007128 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007129 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007130out:
7131 if (err)
7132 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007133 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007134}
Chris Mason9078a3e2007-04-26 16:46:15 -04007135
Yan Zheng2c47e6052009-06-27 21:07:35 -04007136/*
7137 * drop subtree rooted at tree block 'node'.
7138 *
7139 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007140 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007141 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007142int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7143 struct btrfs_root *root,
7144 struct extent_buffer *node,
7145 struct extent_buffer *parent)
7146{
7147 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007148 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007149 int level;
7150 int parent_level;
7151 int ret = 0;
7152 int wret;
7153
Yan Zheng2c47e6052009-06-27 21:07:35 -04007154 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7155
Yan Zhengf82d02d2008-10-29 14:49:05 -04007156 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007157 if (!path)
7158 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007159
Yan Zheng2c47e6052009-06-27 21:07:35 -04007160 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007161 if (!wc) {
7162 btrfs_free_path(path);
7163 return -ENOMEM;
7164 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007165
Chris Masonb9447ef82009-03-09 11:45:38 -04007166 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007167 parent_level = btrfs_header_level(parent);
7168 extent_buffer_get(parent);
7169 path->nodes[parent_level] = parent;
7170 path->slots[parent_level] = btrfs_header_nritems(parent);
7171
Chris Masonb9447ef82009-03-09 11:45:38 -04007172 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007173 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007174 path->nodes[level] = node;
7175 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007176 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007177
7178 wc->refs[parent_level] = 1;
7179 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7180 wc->level = level;
7181 wc->shared_level = -1;
7182 wc->stage = DROP_REFERENCE;
7183 wc->update_ref = 0;
7184 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007185 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007186 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007187
7188 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007189 wret = walk_down_tree(trans, root, path, wc);
7190 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007191 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007192 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007193 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007194
Yan Zheng2c47e6052009-06-27 21:07:35 -04007195 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007196 if (wret < 0)
7197 ret = wret;
7198 if (wret != 0)
7199 break;
7200 }
7201
Yan Zheng2c47e6052009-06-27 21:07:35 -04007202 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007203 btrfs_free_path(path);
7204 return ret;
7205}
7206
Chris Masonec44a352008-04-28 15:29:52 -04007207static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7208{
7209 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007210 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007211
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007212 /*
7213 * if restripe for this chunk_type is on pick target profile and
7214 * return, otherwise do the usual balance
7215 */
7216 stripped = get_restripe_target(root->fs_info, flags);
7217 if (stripped)
7218 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007219
Chris Masoncd02dca2010-12-13 14:56:23 -05007220 /*
7221 * we add in the count of missing devices because we want
7222 * to make sure that any RAID levels on a degraded FS
7223 * continue to be honored.
7224 */
7225 num_devices = root->fs_info->fs_devices->rw_devices +
7226 root->fs_info->fs_devices->missing_devices;
7227
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007228 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7229 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7230
Chris Masonec44a352008-04-28 15:29:52 -04007231 if (num_devices == 1) {
7232 stripped |= BTRFS_BLOCK_GROUP_DUP;
7233 stripped = flags & ~stripped;
7234
7235 /* turn raid0 into single device chunks */
7236 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7237 return stripped;
7238
7239 /* turn mirroring into duplication */
7240 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7241 BTRFS_BLOCK_GROUP_RAID10))
7242 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007243 } else {
7244 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007245 if (flags & stripped)
7246 return flags;
7247
7248 stripped |= BTRFS_BLOCK_GROUP_DUP;
7249 stripped = flags & ~stripped;
7250
7251 /* switch duplicated blocks with raid1 */
7252 if (flags & BTRFS_BLOCK_GROUP_DUP)
7253 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7254
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007255 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007256 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007257
Chris Masonec44a352008-04-28 15:29:52 -04007258 return flags;
7259}
7260
Miao Xie199c36e2011-07-15 10:34:36 +00007261static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007262{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007263 struct btrfs_space_info *sinfo = cache->space_info;
7264 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007265 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007266 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007267
Chris Masonc286ac42008-07-22 23:06:41 -04007268
Miao Xie199c36e2011-07-15 10:34:36 +00007269 /*
7270 * We need some metadata space and system metadata space for
7271 * allocating chunks in some corner cases until we force to set
7272 * it to be readonly.
7273 */
7274 if ((sinfo->flags &
7275 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7276 !force)
7277 min_allocable_bytes = 1 * 1024 * 1024;
7278 else
7279 min_allocable_bytes = 0;
7280
Yan, Zhengf0486c62010-05-16 10:46:25 -04007281 spin_lock(&sinfo->lock);
7282 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007283
7284 if (cache->ro) {
7285 ret = 0;
7286 goto out;
7287 }
7288
Yan, Zhengf0486c62010-05-16 10:46:25 -04007289 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7290 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007291
Yan, Zhengf0486c62010-05-16 10:46:25 -04007292 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007293 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7294 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007295 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007296 cache->ro = 1;
7297 ret = 0;
7298 }
WuBo61cfea92011-07-26 03:30:11 +00007299out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007300 spin_unlock(&cache->lock);
7301 spin_unlock(&sinfo->lock);
7302 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007303}
7304
Yan, Zhengf0486c62010-05-16 10:46:25 -04007305int btrfs_set_block_group_ro(struct btrfs_root *root,
7306 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007307
7308{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007309 struct btrfs_trans_handle *trans;
7310 u64 alloc_flags;
7311 int ret;
7312
7313 BUG_ON(cache->ro);
7314
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007315 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007316 if (IS_ERR(trans))
7317 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007318
7319 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007320 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007321 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007322 CHUNK_ALLOC_FORCE);
7323 if (ret < 0)
7324 goto out;
7325 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007326
Miao Xie199c36e2011-07-15 10:34:36 +00007327 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007328 if (!ret)
7329 goto out;
7330 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007331 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007332 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007333 if (ret < 0)
7334 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007335 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007336out:
7337 btrfs_end_transaction(trans, root);
7338 return ret;
7339}
7340
Chris Masonc87f08c2011-02-16 13:57:04 -05007341int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7342 struct btrfs_root *root, u64 type)
7343{
7344 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007345 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007346 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007347}
7348
Miao Xie6d07bce2011-01-05 10:07:31 +00007349/*
7350 * helper to account the unused space of all the readonly block group in the
7351 * list. takes mirrors into account.
7352 */
7353static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7354{
7355 struct btrfs_block_group_cache *block_group;
7356 u64 free_bytes = 0;
7357 int factor;
7358
7359 list_for_each_entry(block_group, groups_list, list) {
7360 spin_lock(&block_group->lock);
7361
7362 if (!block_group->ro) {
7363 spin_unlock(&block_group->lock);
7364 continue;
7365 }
7366
7367 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7368 BTRFS_BLOCK_GROUP_RAID10 |
7369 BTRFS_BLOCK_GROUP_DUP))
7370 factor = 2;
7371 else
7372 factor = 1;
7373
7374 free_bytes += (block_group->key.offset -
7375 btrfs_block_group_used(&block_group->item)) *
7376 factor;
7377
7378 spin_unlock(&block_group->lock);
7379 }
7380
7381 return free_bytes;
7382}
7383
7384/*
7385 * helper to account the unused space of all the readonly block group in the
7386 * space_info. takes mirrors into account.
7387 */
7388u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7389{
7390 int i;
7391 u64 free_bytes = 0;
7392
7393 spin_lock(&sinfo->lock);
7394
7395 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7396 if (!list_empty(&sinfo->block_groups[i]))
7397 free_bytes += __btrfs_get_ro_block_group_free_space(
7398 &sinfo->block_groups[i]);
7399
7400 spin_unlock(&sinfo->lock);
7401
7402 return free_bytes;
7403}
7404
Jeff Mahoney143bede2012-03-01 14:56:26 +01007405void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007406 struct btrfs_block_group_cache *cache)
7407{
7408 struct btrfs_space_info *sinfo = cache->space_info;
7409 u64 num_bytes;
7410
7411 BUG_ON(!cache->ro);
7412
7413 spin_lock(&sinfo->lock);
7414 spin_lock(&cache->lock);
7415 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7416 cache->bytes_super - btrfs_block_group_used(&cache->item);
7417 sinfo->bytes_readonly -= num_bytes;
7418 cache->ro = 0;
7419 spin_unlock(&cache->lock);
7420 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007421}
7422
Josef Bacikba1bf482009-09-11 16:11:19 -04007423/*
7424 * checks to see if its even possible to relocate this block group.
7425 *
7426 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7427 * ok to go ahead and try.
7428 */
7429int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007430{
Zheng Yan1a40e232008-09-26 10:09:34 -04007431 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007432 struct btrfs_space_info *space_info;
7433 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7434 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007435 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007436 u64 dev_min = 1;
7437 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007438 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007439 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007440 int full = 0;
7441 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007442
Josef Bacikba1bf482009-09-11 16:11:19 -04007443 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007444
Josef Bacikba1bf482009-09-11 16:11:19 -04007445 /* odd, couldn't find the block group, leave it alone */
7446 if (!block_group)
7447 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007448
liubocdcb7252011-08-03 10:15:25 +00007449 min_free = btrfs_block_group_used(&block_group->item);
7450
Josef Bacikba1bf482009-09-11 16:11:19 -04007451 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007452 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007453 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007454
Josef Bacikba1bf482009-09-11 16:11:19 -04007455 space_info = block_group->space_info;
7456 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007457
Josef Bacikba1bf482009-09-11 16:11:19 -04007458 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007459
Josef Bacikba1bf482009-09-11 16:11:19 -04007460 /*
7461 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007462 * relocate it unless we're able to allocate a new chunk below.
7463 *
7464 * Otherwise, we need to make sure we have room in the space to handle
7465 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007466 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007467 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007468 (space_info->bytes_used + space_info->bytes_reserved +
7469 space_info->bytes_pinned + space_info->bytes_readonly +
7470 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007471 spin_unlock(&space_info->lock);
7472 goto out;
7473 }
7474 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007475
Josef Bacikba1bf482009-09-11 16:11:19 -04007476 /*
7477 * ok we don't have enough space, but maybe we have free space on our
7478 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007479 * alloc devices and guess if we have enough space. if this block
7480 * group is going to be restriped, run checks against the target
7481 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007482 */
7483 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007484
liubocdcb7252011-08-03 10:15:25 +00007485 /*
7486 * index:
7487 * 0: raid10
7488 * 1: raid1
7489 * 2: dup
7490 * 3: raid0
7491 * 4: single
7492 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007493 target = get_restripe_target(root->fs_info, block_group->flags);
7494 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007495 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007496 } else {
7497 /*
7498 * this is just a balance, so if we were marked as full
7499 * we know there is no space for a new chunk
7500 */
7501 if (full)
7502 goto out;
7503
7504 index = get_block_group_index(block_group);
7505 }
7506
liubocdcb7252011-08-03 10:15:25 +00007507 if (index == 0) {
7508 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007509 /* Divide by 2 */
7510 min_free >>= 1;
liubocdcb7252011-08-03 10:15:25 +00007511 } else if (index == 1) {
7512 dev_min = 2;
7513 } else if (index == 2) {
Josef Bacik6719db62011-08-20 08:29:51 -04007514 /* Multiply by 2 */
7515 min_free <<= 1;
liubocdcb7252011-08-03 10:15:25 +00007516 } else if (index == 3) {
7517 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007518 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007519 }
7520
Josef Bacikba1bf482009-09-11 16:11:19 -04007521 mutex_lock(&root->fs_info->chunk_mutex);
7522 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007523 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007524
Josef Bacikba1bf482009-09-11 16:11:19 -04007525 /*
7526 * check to make sure we can actually find a chunk with enough
7527 * space to fit our block group in.
7528 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007529 if (device->total_bytes > device->bytes_used + min_free &&
7530 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007531 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007532 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007533 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007534 dev_nr++;
7535
7536 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007537 break;
liubocdcb7252011-08-03 10:15:25 +00007538
Josef Bacikba1bf482009-09-11 16:11:19 -04007539 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007540 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007541 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007542 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007543out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007544 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007545 return ret;
7546}
7547
Christoph Hellwigb2950862008-12-02 09:54:17 -05007548static int find_first_block_group(struct btrfs_root *root,
7549 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007550{
Chris Mason925baed2008-06-25 16:01:30 -04007551 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007552 struct btrfs_key found_key;
7553 struct extent_buffer *leaf;
7554 int slot;
7555
7556 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7557 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007558 goto out;
7559
Chris Masond3977122009-01-05 21:25:51 -05007560 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007561 slot = path->slots[0];
7562 leaf = path->nodes[0];
7563 if (slot >= btrfs_header_nritems(leaf)) {
7564 ret = btrfs_next_leaf(root, path);
7565 if (ret == 0)
7566 continue;
7567 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007568 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007569 break;
7570 }
7571 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7572
7573 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007574 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7575 ret = 0;
7576 goto out;
7577 }
Chris Mason0b86a832008-03-24 15:01:56 -04007578 path->slots[0]++;
7579 }
Chris Mason925baed2008-06-25 16:01:30 -04007580out:
Chris Mason0b86a832008-03-24 15:01:56 -04007581 return ret;
7582}
7583
Josef Bacik0af3d002010-06-21 14:48:16 -04007584void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7585{
7586 struct btrfs_block_group_cache *block_group;
7587 u64 last = 0;
7588
7589 while (1) {
7590 struct inode *inode;
7591
7592 block_group = btrfs_lookup_first_block_group(info, last);
7593 while (block_group) {
7594 spin_lock(&block_group->lock);
7595 if (block_group->iref)
7596 break;
7597 spin_unlock(&block_group->lock);
7598 block_group = next_block_group(info->tree_root,
7599 block_group);
7600 }
7601 if (!block_group) {
7602 if (last == 0)
7603 break;
7604 last = 0;
7605 continue;
7606 }
7607
7608 inode = block_group->inode;
7609 block_group->iref = 0;
7610 block_group->inode = NULL;
7611 spin_unlock(&block_group->lock);
7612 iput(inode);
7613 last = block_group->key.objectid + block_group->key.offset;
7614 btrfs_put_block_group(block_group);
7615 }
7616}
7617
Zheng Yan1a40e232008-09-26 10:09:34 -04007618int btrfs_free_block_groups(struct btrfs_fs_info *info)
7619{
7620 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007621 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007622 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007623 struct rb_node *n;
7624
Yan Zheng11833d62009-09-11 16:11:19 -04007625 down_write(&info->extent_commit_sem);
7626 while (!list_empty(&info->caching_block_groups)) {
7627 caching_ctl = list_entry(info->caching_block_groups.next,
7628 struct btrfs_caching_control, list);
7629 list_del(&caching_ctl->list);
7630 put_caching_control(caching_ctl);
7631 }
7632 up_write(&info->extent_commit_sem);
7633
Zheng Yan1a40e232008-09-26 10:09:34 -04007634 spin_lock(&info->block_group_cache_lock);
7635 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7636 block_group = rb_entry(n, struct btrfs_block_group_cache,
7637 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007638 rb_erase(&block_group->cache_node,
7639 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007640 spin_unlock(&info->block_group_cache_lock);
7641
Josef Bacik80eb2342008-10-29 14:49:05 -04007642 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007643 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007644 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007645
Josef Bacik817d52f2009-07-13 21:29:25 -04007646 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007647 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007648
Josef Bacik3c148742011-02-02 15:53:47 +00007649 /*
7650 * We haven't cached this block group, which means we could
7651 * possibly have excluded extents on this block group.
7652 */
7653 if (block_group->cached == BTRFS_CACHE_NO)
7654 free_excluded_extents(info->extent_root, block_group);
7655
Josef Bacik817d52f2009-07-13 21:29:25 -04007656 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007657 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007658
7659 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007660 }
7661 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007662
7663 /* now that all the block groups are freed, go through and
7664 * free all the space_info structs. This is only called during
7665 * the final stages of unmount, and so we know nobody is
7666 * using them. We call synchronize_rcu() once before we start,
7667 * just to be on the safe side.
7668 */
7669 synchronize_rcu();
7670
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007671 release_global_block_rsv(info);
7672
Chris Mason4184ea72009-03-10 12:39:20 -04007673 while(!list_empty(&info->space_info)) {
7674 space_info = list_entry(info->space_info.next,
7675 struct btrfs_space_info,
7676 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007677 if (space_info->bytes_pinned > 0 ||
Josef Bacikfb25e912011-07-26 17:00:46 -04007678 space_info->bytes_reserved > 0 ||
7679 space_info->bytes_may_use > 0) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007680 WARN_ON(1);
7681 dump_space_info(space_info, 0, 0);
7682 }
Chris Mason4184ea72009-03-10 12:39:20 -04007683 list_del(&space_info->list);
7684 kfree(space_info);
7685 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007686 return 0;
7687}
7688
Yan, Zhengb742bb822010-05-16 10:46:24 -04007689static void __link_block_group(struct btrfs_space_info *space_info,
7690 struct btrfs_block_group_cache *cache)
7691{
7692 int index = get_block_group_index(cache);
7693
7694 down_write(&space_info->groups_sem);
7695 list_add_tail(&cache->list, &space_info->block_groups[index]);
7696 up_write(&space_info->groups_sem);
7697}
7698
Chris Mason9078a3e2007-04-26 16:46:15 -04007699int btrfs_read_block_groups(struct btrfs_root *root)
7700{
7701 struct btrfs_path *path;
7702 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007703 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007704 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007705 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007706 struct btrfs_key key;
7707 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007708 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007709 int need_clear = 0;
7710 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007711
Chris Masonbe744172007-05-06 10:15:01 -04007712 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007713 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007714 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007715 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007716 path = btrfs_alloc_path();
7717 if (!path)
7718 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007719 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007720
David Sterba6c417612011-04-13 15:41:04 +02007721 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007722 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02007723 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04007724 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007725 if (btrfs_test_opt(root, CLEAR_CACHE))
7726 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007727
Chris Masond3977122009-01-05 21:25:51 -05007728 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007729 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007730 if (ret > 0)
7731 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007732 if (ret != 0)
7733 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007734 leaf = path->nodes[0];
7735 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007736 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007737 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007738 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007739 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007740 }
Li Zefan34d52cb2011-03-29 13:46:06 +08007741 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7742 GFP_NOFS);
7743 if (!cache->free_space_ctl) {
7744 kfree(cache);
7745 ret = -ENOMEM;
7746 goto error;
7747 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007748
Yan Zhengd2fb3432008-12-11 16:30:39 -05007749 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007750 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007751 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007752 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007753 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007754
Liu Bocf7c1ef2012-07-06 03:31:34 -06007755 if (need_clear) {
7756 /*
7757 * When we mount with old space cache, we need to
7758 * set BTRFS_DC_CLEAR and set dirty flag.
7759 *
7760 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7761 * truncate the old free space cache inode and
7762 * setup a new one.
7763 * b) Setting 'dirty flag' makes sure that we flush
7764 * the new space cache info onto disk.
7765 */
Josef Bacik0af3d002010-06-21 14:48:16 -04007766 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007767 if (btrfs_test_opt(root, SPACE_CACHE))
7768 cache->dirty = 1;
7769 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007770
Chris Mason5f39d392007-10-15 16:14:19 -04007771 read_extent_buffer(leaf, &cache->item,
7772 btrfs_item_ptr_offset(leaf, path->slots[0]),
7773 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007774 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007775
Chris Mason9078a3e2007-04-26 16:46:15 -04007776 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007777 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007778 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007779 cache->sectorsize = root->sectorsize;
7780
Li Zefan34d52cb2011-03-29 13:46:06 +08007781 btrfs_init_free_space_ctl(cache);
7782
Josef Bacik817d52f2009-07-13 21:29:25 -04007783 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007784 * We need to exclude the super stripes now so that the space
7785 * info has super bytes accounted for, otherwise we'll think
7786 * we have more space than we actually do.
7787 */
7788 exclude_super_stripes(root, cache);
7789
7790 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007791 * check for two cases, either we are full, and therefore
7792 * don't need to bother with the caching work since we won't
7793 * find any space, or we are empty, and we can just add all
7794 * the space in and be done with it. This saves us _alot_ of
7795 * time, particularly in the full case.
7796 */
7797 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007798 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007799 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007800 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007801 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007802 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007803 cache->cached = BTRFS_CACHE_FINISHED;
7804 add_new_free_space(cache, root->fs_info,
7805 found_key.objectid,
7806 found_key.objectid +
7807 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007808 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007809 }
Chris Mason96b51792007-10-15 16:15:19 -04007810
Chris Mason6324fbf2008-03-24 15:01:59 -04007811 ret = update_space_info(info, cache->flags, found_key.offset,
7812 btrfs_block_group_used(&cache->item),
7813 &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007814 BUG_ON(ret); /* -ENOMEM */
Chris Mason6324fbf2008-03-24 15:01:59 -04007815 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007816 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007817 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007818 spin_unlock(&cache->space_info->lock);
7819
Yan, Zhengb742bb822010-05-16 10:46:24 -04007820 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007821
Josef Bacik0f9dd462008-09-23 13:14:11 -04007822 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007823 BUG_ON(ret); /* Logic error */
Chris Mason75ccf472008-09-30 19:24:06 -04007824
7825 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007826 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007827 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007828 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04007829
7830 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7831 if (!(get_alloc_profile(root, space_info->flags) &
7832 (BTRFS_BLOCK_GROUP_RAID10 |
7833 BTRFS_BLOCK_GROUP_RAID1 |
7834 BTRFS_BLOCK_GROUP_DUP)))
7835 continue;
7836 /*
7837 * avoid allocating from un-mirrored block group if there are
7838 * mirrored block groups.
7839 */
7840 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007841 set_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007842 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007843 set_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007844 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007845
7846 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007847 ret = 0;
7848error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007849 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007850 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007851}
Chris Mason6324fbf2008-03-24 15:01:59 -04007852
Josef Bacikea658ba2012-09-11 16:57:25 -04007853void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
7854 struct btrfs_root *root)
7855{
7856 struct btrfs_block_group_cache *block_group, *tmp;
7857 struct btrfs_root *extent_root = root->fs_info->extent_root;
7858 struct btrfs_block_group_item item;
7859 struct btrfs_key key;
7860 int ret = 0;
7861
7862 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
7863 new_bg_list) {
7864 list_del_init(&block_group->new_bg_list);
7865
7866 if (ret)
7867 continue;
7868
7869 spin_lock(&block_group->lock);
7870 memcpy(&item, &block_group->item, sizeof(item));
7871 memcpy(&key, &block_group->key, sizeof(key));
7872 spin_unlock(&block_group->lock);
7873
7874 ret = btrfs_insert_item(trans, extent_root, &key, &item,
7875 sizeof(item));
7876 if (ret)
7877 btrfs_abort_transaction(trans, extent_root, ret);
7878 }
7879}
7880
Chris Mason6324fbf2008-03-24 15:01:59 -04007881int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7882 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007883 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007884 u64 size)
7885{
7886 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007887 struct btrfs_root *extent_root;
7888 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007889
7890 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007891
Chris Mason12fcfd22009-03-24 10:24:20 -04007892 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007893
Chris Mason8f18cf12008-04-25 16:53:30 -04007894 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007895 if (!cache)
7896 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08007897 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7898 GFP_NOFS);
7899 if (!cache->free_space_ctl) {
7900 kfree(cache);
7901 return -ENOMEM;
7902 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007903
Chris Masone17cade2008-04-15 15:41:47 -04007904 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007905 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007906 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007907 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007908 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007909
Yan Zhengd2fb3432008-12-11 16:30:39 -05007910 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007911 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007912 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007913 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04007914 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007915
Li Zefan34d52cb2011-03-29 13:46:06 +08007916 btrfs_init_free_space_ctl(cache);
7917
Chris Mason6324fbf2008-03-24 15:01:59 -04007918 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007919 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7920 cache->flags = type;
7921 btrfs_set_block_group_flags(&cache->item, type);
7922
Yan Zheng11833d62009-09-11 16:11:19 -04007923 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007924 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007925 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007926
Josef Bacik817d52f2009-07-13 21:29:25 -04007927 add_new_free_space(cache, root->fs_info, chunk_offset,
7928 chunk_offset + size);
7929
Yan Zheng11833d62009-09-11 16:11:19 -04007930 free_excluded_extents(root, cache);
7931
Chris Mason6324fbf2008-03-24 15:01:59 -04007932 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7933 &cache->space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007934 BUG_ON(ret); /* -ENOMEM */
Li Zefanc7c144d2011-12-07 10:39:22 +08007935 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04007936
7937 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007938 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007939 spin_unlock(&cache->space_info->lock);
7940
Yan, Zhengb742bb822010-05-16 10:46:24 -04007941 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007942
Josef Bacik0f9dd462008-09-23 13:14:11 -04007943 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007944 BUG_ON(ret); /* Logic error */
Chris Masonc286ac42008-07-22 23:06:41 -04007945
Josef Bacikea658ba2012-09-11 16:57:25 -04007946 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04007947
Chris Masond18a2c42008-04-04 15:40:00 -04007948 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007949
Chris Mason6324fbf2008-03-24 15:01:59 -04007950 return 0;
7951}
Zheng Yan1a40e232008-09-26 10:09:34 -04007952
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007953static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
7954{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03007955 u64 extra_flags = chunk_to_extended(flags) &
7956 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007957
7958 if (flags & BTRFS_BLOCK_GROUP_DATA)
7959 fs_info->avail_data_alloc_bits &= ~extra_flags;
7960 if (flags & BTRFS_BLOCK_GROUP_METADATA)
7961 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
7962 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
7963 fs_info->avail_system_alloc_bits &= ~extra_flags;
7964}
7965
Zheng Yan1a40e232008-09-26 10:09:34 -04007966int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7967 struct btrfs_root *root, u64 group_start)
7968{
7969 struct btrfs_path *path;
7970 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007971 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007972 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007973 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007974 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007975 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007976 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04007977 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007978
Zheng Yan1a40e232008-09-26 10:09:34 -04007979 root = root->fs_info->extent_root;
7980
7981 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7982 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007983 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007984
liubo9f7c43c2011-03-07 02:13:33 +00007985 /*
7986 * Free the reserved super bytes from this block group before
7987 * remove it.
7988 */
7989 free_excluded_extents(root, block_group);
7990
Zheng Yan1a40e232008-09-26 10:09:34 -04007991 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007992 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04007993 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7994 BTRFS_BLOCK_GROUP_RAID1 |
7995 BTRFS_BLOCK_GROUP_RAID10))
7996 factor = 2;
7997 else
7998 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007999
Chris Mason44fb5512009-06-04 15:34:51 -04008000 /* make sure this block group isn't part of an allocation cluster */
8001 cluster = &root->fs_info->data_alloc_cluster;
8002 spin_lock(&cluster->refill_lock);
8003 btrfs_return_cluster_to_free_space(block_group, cluster);
8004 spin_unlock(&cluster->refill_lock);
8005
8006 /*
8007 * make sure this block group isn't part of a metadata
8008 * allocation cluster
8009 */
8010 cluster = &root->fs_info->meta_alloc_cluster;
8011 spin_lock(&cluster->refill_lock);
8012 btrfs_return_cluster_to_free_space(block_group, cluster);
8013 spin_unlock(&cluster->refill_lock);
8014
Zheng Yan1a40e232008-09-26 10:09:34 -04008015 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008016 if (!path) {
8017 ret = -ENOMEM;
8018 goto out;
8019 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008020
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008021 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008022 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008023 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008024 if (ret) {
8025 btrfs_add_delayed_iput(inode);
8026 goto out;
8027 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008028 clear_nlink(inode);
8029 /* One for the block groups ref */
8030 spin_lock(&block_group->lock);
8031 if (block_group->iref) {
8032 block_group->iref = 0;
8033 block_group->inode = NULL;
8034 spin_unlock(&block_group->lock);
8035 iput(inode);
8036 } else {
8037 spin_unlock(&block_group->lock);
8038 }
8039 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008040 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008041 }
8042
8043 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8044 key.offset = block_group->key.objectid;
8045 key.type = 0;
8046
8047 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8048 if (ret < 0)
8049 goto out;
8050 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008051 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008052 if (ret == 0) {
8053 ret = btrfs_del_item(trans, tree_root, path);
8054 if (ret)
8055 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008056 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008057 }
8058
Yan Zheng3dfdb932009-01-21 10:49:16 -05008059 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008060 rb_erase(&block_group->cache_node,
8061 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008062 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008063
Josef Bacik80eb2342008-10-29 14:49:05 -04008064 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008065 /*
8066 * we must use list_del_init so people can check to see if they
8067 * are still on the list after taking the semaphore
8068 */
8069 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008070 if (list_empty(&block_group->space_info->block_groups[index]))
8071 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008072 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008073
Josef Bacik817d52f2009-07-13 21:29:25 -04008074 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008075 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008076
8077 btrfs_remove_free_space_cache(block_group);
8078
Yan Zhengc146afa2008-11-12 14:34:12 -05008079 spin_lock(&block_group->space_info->lock);
8080 block_group->space_info->total_bytes -= block_group->key.offset;
8081 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008082 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008083 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008084
Josef Bacik0af3d002010-06-21 14:48:16 -04008085 memcpy(&key, &block_group->key, sizeof(key));
8086
Chris Mason283bb192009-07-24 16:30:55 -04008087 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008088
Chris Masonfa9c0d792009-04-03 09:47:43 -04008089 btrfs_put_block_group(block_group);
8090 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008091
8092 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8093 if (ret > 0)
8094 ret = -EIO;
8095 if (ret < 0)
8096 goto out;
8097
8098 ret = btrfs_del_item(trans, root, path);
8099out:
8100 btrfs_free_path(path);
8101 return ret;
8102}
liuboacce9522011-01-06 19:30:25 +08008103
liuboc59021f2011-03-07 02:13:14 +00008104int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8105{
8106 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008107 struct btrfs_super_block *disk_super;
8108 u64 features;
8109 u64 flags;
8110 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008111 int ret;
8112
David Sterba6c417612011-04-13 15:41:04 +02008113 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008114 if (!btrfs_super_root(disk_super))
8115 return 1;
liuboc59021f2011-03-07 02:13:14 +00008116
liubo1aba86d2011-04-08 08:44:37 +00008117 features = btrfs_super_incompat_flags(disk_super);
8118 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8119 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008120
liubo1aba86d2011-04-08 08:44:37 +00008121 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8122 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008123 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008124 goto out;
liuboc59021f2011-03-07 02:13:14 +00008125
liubo1aba86d2011-04-08 08:44:37 +00008126 if (mixed) {
8127 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8128 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8129 } else {
8130 flags = BTRFS_BLOCK_GROUP_METADATA;
8131 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8132 if (ret)
8133 goto out;
8134
8135 flags = BTRFS_BLOCK_GROUP_DATA;
8136 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8137 }
8138out:
liuboc59021f2011-03-07 02:13:14 +00008139 return ret;
8140}
8141
liuboacce9522011-01-06 19:30:25 +08008142int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8143{
8144 return unpin_extent_range(root, start, end);
8145}
8146
8147int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008148 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008149{
Li Dongyang5378e602011-03-24 10:24:27 +00008150 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008151}
Li Dongyangf7039b12011-03-24 10:24:28 +00008152
8153int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8154{
8155 struct btrfs_fs_info *fs_info = root->fs_info;
8156 struct btrfs_block_group_cache *cache = NULL;
8157 u64 group_trimmed;
8158 u64 start;
8159 u64 end;
8160 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008161 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008162 int ret = 0;
8163
Liu Bo2cac13e2012-02-09 18:17:41 +08008164 /*
8165 * try to trim all FS space, our block group may start from non-zero.
8166 */
8167 if (range->len == total_bytes)
8168 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8169 else
8170 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008171
8172 while (cache) {
8173 if (cache->key.objectid >= (range->start + range->len)) {
8174 btrfs_put_block_group(cache);
8175 break;
8176 }
8177
8178 start = max(range->start, cache->key.objectid);
8179 end = min(range->start + range->len,
8180 cache->key.objectid + cache->key.offset);
8181
8182 if (end - start >= range->minlen) {
8183 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008184 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008185 if (!ret)
8186 wait_block_group_cache_done(cache);
8187 }
8188 ret = btrfs_trim_block_group(cache,
8189 &group_trimmed,
8190 start,
8191 end,
8192 range->minlen);
8193
8194 trimmed += group_trimmed;
8195 if (ret) {
8196 btrfs_put_block_group(cache);
8197 break;
8198 }
8199 }
8200
8201 cache = next_block_group(fs_info->tree_root, cache);
8202 }
8203
8204 range->len = trimmed;
8205 return ret;
8206}