blob: a9dc12d5ac1ba32b1b8821cc508cbd6ffe5d200d [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
Josef Bacikf3465ca2008-11-12 14:19:50 -050075static int update_block_group(struct btrfs_trans_handle *trans,
76 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040077 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040078static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
79 struct btrfs_root *root,
80 u64 bytenr, u64 num_bytes, u64 parent,
81 u64 root_objectid, u64 owner_objectid,
82 u64 owner_offset, int refs_to_drop,
83 struct btrfs_delayed_extent_op *extra_op);
84static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
85 struct extent_buffer *leaf,
86 struct btrfs_extent_item *ei);
87static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
88 struct btrfs_root *root,
89 u64 parent, u64 root_objectid,
90 u64 flags, u64 owner, u64 offset,
91 struct btrfs_key *ins, int ref_mod);
92static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
93 struct btrfs_root *root,
94 u64 parent, u64 root_objectid,
95 u64 flags, struct btrfs_disk_key *key,
96 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050097static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -040098 struct btrfs_root *extent_root, u64 flags,
99 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400100static int find_next_key(struct btrfs_path *path, int level,
101 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400102static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
103 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400104static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
105 u64 num_bytes, int reserve);
Josef Bacik6a632092009-02-20 11:00:09 -0500106
Josef Bacik817d52f2009-07-13 21:29:25 -0400107static noinline int
108block_group_cache_done(struct btrfs_block_group_cache *cache)
109{
110 smp_mb();
111 return cache->cached == BTRFS_CACHE_FINISHED;
112}
113
Josef Bacik0f9dd462008-09-23 13:14:11 -0400114static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
115{
116 return (cache->flags & bits) == bits;
117}
118
David Sterba62a45b62011-04-20 15:52:26 +0200119static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000120{
121 atomic_inc(&cache->count);
122}
123
124void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
125{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400126 if (atomic_dec_and_test(&cache->count)) {
127 WARN_ON(cache->pinned > 0);
128 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800129 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000130 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400131 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000132}
133
Josef Bacik0f9dd462008-09-23 13:14:11 -0400134/*
135 * this adds the block group to the fs_info rb tree for the block group
136 * cache
137 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500138static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400139 struct btrfs_block_group_cache *block_group)
140{
141 struct rb_node **p;
142 struct rb_node *parent = NULL;
143 struct btrfs_block_group_cache *cache;
144
145 spin_lock(&info->block_group_cache_lock);
146 p = &info->block_group_cache_tree.rb_node;
147
148 while (*p) {
149 parent = *p;
150 cache = rb_entry(parent, struct btrfs_block_group_cache,
151 cache_node);
152 if (block_group->key.objectid < cache->key.objectid) {
153 p = &(*p)->rb_left;
154 } else if (block_group->key.objectid > cache->key.objectid) {
155 p = &(*p)->rb_right;
156 } else {
157 spin_unlock(&info->block_group_cache_lock);
158 return -EEXIST;
159 }
160 }
161
162 rb_link_node(&block_group->cache_node, parent, p);
163 rb_insert_color(&block_group->cache_node,
164 &info->block_group_cache_tree);
165 spin_unlock(&info->block_group_cache_lock);
166
167 return 0;
168}
169
170/*
171 * This will return the block group at or after bytenr if contains is 0, else
172 * it will return the block group that contains the bytenr
173 */
174static struct btrfs_block_group_cache *
175block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
176 int contains)
177{
178 struct btrfs_block_group_cache *cache, *ret = NULL;
179 struct rb_node *n;
180 u64 end, start;
181
182 spin_lock(&info->block_group_cache_lock);
183 n = info->block_group_cache_tree.rb_node;
184
185 while (n) {
186 cache = rb_entry(n, struct btrfs_block_group_cache,
187 cache_node);
188 end = cache->key.objectid + cache->key.offset - 1;
189 start = cache->key.objectid;
190
191 if (bytenr < start) {
192 if (!contains && (!ret || start < ret->key.objectid))
193 ret = cache;
194 n = n->rb_left;
195 } else if (bytenr > start) {
196 if (contains && bytenr <= end) {
197 ret = cache;
198 break;
199 }
200 n = n->rb_right;
201 } else {
202 ret = cache;
203 break;
204 }
205 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500206 if (ret)
Josef Bacik11dfe352009-11-13 20:12:59 +0000207 btrfs_get_block_group(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400208 spin_unlock(&info->block_group_cache_lock);
209
210 return ret;
211}
212
Yan Zheng11833d62009-09-11 16:11:19 -0400213static int add_excluded_extent(struct btrfs_root *root,
214 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400215{
Yan Zheng11833d62009-09-11 16:11:19 -0400216 u64 end = start + num_bytes - 1;
217 set_extent_bits(&root->fs_info->freed_extents[0],
218 start, end, EXTENT_UPTODATE, GFP_NOFS);
219 set_extent_bits(&root->fs_info->freed_extents[1],
220 start, end, EXTENT_UPTODATE, GFP_NOFS);
221 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400222}
223
Yan Zheng11833d62009-09-11 16:11:19 -0400224static void free_excluded_extents(struct btrfs_root *root,
225 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400226{
Yan Zheng11833d62009-09-11 16:11:19 -0400227 u64 start, end;
228
229 start = cache->key.objectid;
230 end = start + cache->key.offset - 1;
231
232 clear_extent_bits(&root->fs_info->freed_extents[0],
233 start, end, EXTENT_UPTODATE, GFP_NOFS);
234 clear_extent_bits(&root->fs_info->freed_extents[1],
235 start, end, EXTENT_UPTODATE, GFP_NOFS);
236}
237
238static int exclude_super_stripes(struct btrfs_root *root,
239 struct btrfs_block_group_cache *cache)
240{
Josef Bacik817d52f2009-07-13 21:29:25 -0400241 u64 bytenr;
242 u64 *logical;
243 int stripe_len;
244 int i, nr, ret;
245
Yan, Zheng06b23312009-11-26 09:31:11 +0000246 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
247 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
248 cache->bytes_super += stripe_len;
249 ret = add_excluded_extent(root, cache->key.objectid,
250 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100251 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng06b23312009-11-26 09:31:11 +0000252 }
253
Josef Bacik817d52f2009-07-13 21:29:25 -0400254 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
255 bytenr = btrfs_sb_offset(i);
256 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
257 cache->key.objectid, bytenr,
258 0, &logical, &nr, &stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100259 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -0400260
Josef Bacik817d52f2009-07-13 21:29:25 -0400261 while (nr--) {
Josef Bacik1b2da372009-09-11 16:11:20 -0400262 cache->bytes_super += stripe_len;
Yan Zheng11833d62009-09-11 16:11:19 -0400263 ret = add_excluded_extent(root, logical[nr],
264 stripe_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100265 BUG_ON(ret); /* -ENOMEM */
Josef Bacik817d52f2009-07-13 21:29:25 -0400266 }
Yan Zheng11833d62009-09-11 16:11:19 -0400267
Josef Bacik817d52f2009-07-13 21:29:25 -0400268 kfree(logical);
269 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400270 return 0;
271}
272
Yan Zheng11833d62009-09-11 16:11:19 -0400273static struct btrfs_caching_control *
274get_caching_control(struct btrfs_block_group_cache *cache)
275{
276 struct btrfs_caching_control *ctl;
277
278 spin_lock(&cache->lock);
279 if (cache->cached != BTRFS_CACHE_STARTED) {
280 spin_unlock(&cache->lock);
281 return NULL;
282 }
283
Josef Bacikdde5abe2010-09-16 16:17:03 -0400284 /* We're loading it the fast way, so we don't have a caching_ctl. */
285 if (!cache->caching_ctl) {
286 spin_unlock(&cache->lock);
287 return NULL;
288 }
289
Yan Zheng11833d62009-09-11 16:11:19 -0400290 ctl = cache->caching_ctl;
291 atomic_inc(&ctl->count);
292 spin_unlock(&cache->lock);
293 return ctl;
294}
295
296static void put_caching_control(struct btrfs_caching_control *ctl)
297{
298 if (atomic_dec_and_test(&ctl->count))
299 kfree(ctl);
300}
301
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302/*
303 * this is only called by cache_block_group, since we could have freed extents
304 * we need to check the pinned_extents for any extents that can't be used yet
305 * since their free space will be released as soon as the transaction commits.
306 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400307static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400308 struct btrfs_fs_info *info, u64 start, u64 end)
309{
Josef Bacik817d52f2009-07-13 21:29:25 -0400310 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400311 int ret;
312
313 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400314 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400315 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400316 EXTENT_DIRTY | EXTENT_UPTODATE,
317 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400318 if (ret)
319 break;
320
Yan, Zheng06b23312009-11-26 09:31:11 +0000321 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400322 start = extent_end + 1;
323 } else if (extent_start > start && extent_start < end) {
324 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400325 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500326 ret = btrfs_add_free_space(block_group, start,
327 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100328 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400329 start = extent_end + 1;
330 } else {
331 break;
332 }
333 }
334
335 if (start < end) {
336 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400337 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500338 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100339 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400340 }
341
Josef Bacik817d52f2009-07-13 21:29:25 -0400342 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343}
344
Josef Bacikbab39bf2011-06-30 14:42:28 -0400345static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400346{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400347 struct btrfs_block_group_cache *block_group;
348 struct btrfs_fs_info *fs_info;
349 struct btrfs_caching_control *caching_ctl;
350 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400351 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400352 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400353 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400354 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400355 u64 last = 0;
356 u32 nritems;
357 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400358
Josef Bacikbab39bf2011-06-30 14:42:28 -0400359 caching_ctl = container_of(work, struct btrfs_caching_control, work);
360 block_group = caching_ctl->block_group;
361 fs_info = block_group->fs_info;
362 extent_root = fs_info->extent_root;
363
Chris Masone37c9e62007-05-09 20:13:14 -0400364 path = btrfs_alloc_path();
365 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400366 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400367
Josef Bacik817d52f2009-07-13 21:29:25 -0400368 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400369
Chris Mason5cd57b22008-06-25 16:01:30 -0400370 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400371 * We don't want to deadlock with somebody trying to allocate a new
372 * extent for the extent root while also trying to search the extent
373 * root to add free space. So we skip locking and search the commit
374 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400375 */
376 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400377 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400378 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400379
Yan Zhenge4404d62008-12-12 10:03:26 -0500380 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400381 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400382 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400383again:
Yan Zheng11833d62009-09-11 16:11:19 -0400384 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400385 /* need to make sure the commit_root doesn't disappear */
386 down_read(&fs_info->extent_commit_sem);
387
Yan Zheng11833d62009-09-11 16:11:19 -0400388 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400389 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400390 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500391
Yan Zheng11833d62009-09-11 16:11:19 -0400392 leaf = path->nodes[0];
393 nritems = btrfs_header_nritems(leaf);
394
Chris Masond3977122009-01-05 21:25:51 -0500395 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200396 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400397 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400398 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400399 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400400
Yan Zheng11833d62009-09-11 16:11:19 -0400401 if (path->slots[0] < nritems) {
402 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
403 } else {
404 ret = find_next_key(path, 0, &key);
405 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400406 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400407
Josef Bacik589d8ad2011-05-11 17:30:53 -0400408 if (need_resched() ||
409 btrfs_next_leaf(extent_root, path)) {
410 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400411 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400412 up_read(&fs_info->extent_commit_sem);
413 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400414 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400415 goto again;
416 }
417 leaf = path->nodes[0];
418 nritems = btrfs_header_nritems(leaf);
419 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400420 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400421
Yan Zheng11833d62009-09-11 16:11:19 -0400422 if (key.objectid < block_group->key.objectid) {
423 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400424 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400425 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400426
Chris Masone37c9e62007-05-09 20:13:14 -0400427 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400428 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400429 break;
Yan7d7d6062007-09-14 16:15:28 -0400430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400432 total_found += add_new_free_space(block_group,
433 fs_info, last,
434 key.objectid);
Yan7d7d6062007-09-14 16:15:28 -0400435 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400436
Yan Zheng11833d62009-09-11 16:11:19 -0400437 if (total_found > (1024 * 1024 * 2)) {
438 total_found = 0;
439 wake_up(&caching_ctl->wait);
440 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400441 }
Chris Masone37c9e62007-05-09 20:13:14 -0400442 path->slots[0]++;
443 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400444 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400445
446 total_found += add_new_free_space(block_group, fs_info, last,
447 block_group->key.objectid +
448 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400449 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400450
451 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400452 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400453 block_group->cached = BTRFS_CACHE_FINISHED;
454 spin_unlock(&block_group->lock);
455
Chris Mason54aa1f42007-06-22 14:16:25 -0400456err:
Chris Masone37c9e62007-05-09 20:13:14 -0400457 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400458 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400459
Yan Zheng11833d62009-09-11 16:11:19 -0400460 free_excluded_extents(extent_root, block_group);
461
462 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400463out:
Yan Zheng11833d62009-09-11 16:11:19 -0400464 wake_up(&caching_ctl->wait);
465
466 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000467 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400468}
469
Josef Bacik9d66e232010-08-25 16:54:15 -0400470static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400471 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400472{
Josef Bacik291c7d22011-11-14 13:52:14 -0500473 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400474 struct btrfs_fs_info *fs_info = cache->fs_info;
475 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400476 int ret = 0;
477
Josef Bacik291c7d22011-11-14 13:52:14 -0500478 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100479 if (!caching_ctl)
480 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500481
482 INIT_LIST_HEAD(&caching_ctl->list);
483 mutex_init(&caching_ctl->mutex);
484 init_waitqueue_head(&caching_ctl->wait);
485 caching_ctl->block_group = cache;
486 caching_ctl->progress = cache->key.objectid;
487 atomic_set(&caching_ctl->count, 1);
488 caching_ctl->work.func = caching_thread;
489
490 spin_lock(&cache->lock);
491 /*
492 * This should be a rare occasion, but this could happen I think in the
493 * case where one thread starts to load the space cache info, and then
494 * some other thread starts a transaction commit which tries to do an
495 * allocation while the other thread is still loading the space cache
496 * info. The previous loop should have kept us from choosing this block
497 * group, but if we've moved to the state where we will wait on caching
498 * block groups we need to first check if we're doing a fast load here,
499 * so we can wait for it to finish, otherwise we could end up allocating
500 * from a block group who's cache gets evicted for one reason or
501 * another.
502 */
503 while (cache->cached == BTRFS_CACHE_FAST) {
504 struct btrfs_caching_control *ctl;
505
506 ctl = cache->caching_ctl;
507 atomic_inc(&ctl->count);
508 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
509 spin_unlock(&cache->lock);
510
511 schedule();
512
513 finish_wait(&ctl->wait, &wait);
514 put_caching_control(ctl);
515 spin_lock(&cache->lock);
516 }
517
518 if (cache->cached != BTRFS_CACHE_NO) {
519 spin_unlock(&cache->lock);
520 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400521 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500522 }
523 WARN_ON(cache->caching_ctl);
524 cache->caching_ctl = caching_ctl;
525 cache->cached = BTRFS_CACHE_FAST;
526 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400527
Josef Bacikd53ba472012-04-12 16:03:57 -0400528 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400529 ret = load_free_space_cache(fs_info, cache);
530
531 spin_lock(&cache->lock);
532 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500533 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400534 cache->cached = BTRFS_CACHE_FINISHED;
535 cache->last_byte_to_unpin = (u64)-1;
536 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500537 if (load_cache_only) {
538 cache->caching_ctl = NULL;
539 cache->cached = BTRFS_CACHE_NO;
540 } else {
541 cache->cached = BTRFS_CACHE_STARTED;
542 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400543 }
544 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500545 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000546 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500547 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000548 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400549 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000550 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500551 } else {
552 /*
553 * We are not going to do the fast caching, set cached to the
554 * appropriate value and wakeup any waiters.
555 */
556 spin_lock(&cache->lock);
557 if (load_cache_only) {
558 cache->caching_ctl = NULL;
559 cache->cached = BTRFS_CACHE_NO;
560 } else {
561 cache->cached = BTRFS_CACHE_STARTED;
562 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400563 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500564 wake_up(&caching_ctl->wait);
565 }
566
567 if (load_cache_only) {
568 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400569 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400570 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400571
Yan Zheng11833d62009-09-11 16:11:19 -0400572 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500573 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400574 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
575 up_write(&fs_info->extent_commit_sem);
576
Josef Bacik11dfe352009-11-13 20:12:59 +0000577 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400578
Josef Bacikbab39bf2011-06-30 14:42:28 -0400579 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400580
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400581 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400582}
583
Josef Bacik0f9dd462008-09-23 13:14:11 -0400584/*
585 * return the block group that starts at or after bytenr
586 */
Chris Masond3977122009-01-05 21:25:51 -0500587static struct btrfs_block_group_cache *
588btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400589{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400590 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400591
Josef Bacik0f9dd462008-09-23 13:14:11 -0400592 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400593
Josef Bacik0f9dd462008-09-23 13:14:11 -0400594 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400595}
596
Josef Bacik0f9dd462008-09-23 13:14:11 -0400597/*
Sankar P9f556842009-05-14 13:52:22 -0400598 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400599 */
Chris Masond3977122009-01-05 21:25:51 -0500600struct btrfs_block_group_cache *btrfs_lookup_block_group(
601 struct btrfs_fs_info *info,
602 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400603{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400604 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400605
Josef Bacik0f9dd462008-09-23 13:14:11 -0400606 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400607
Josef Bacik0f9dd462008-09-23 13:14:11 -0400608 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400609}
Chris Mason0b86a832008-03-24 15:01:56 -0400610
Josef Bacik0f9dd462008-09-23 13:14:11 -0400611static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
612 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400613{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400614 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400615 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400616
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200617 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400618
Chris Mason4184ea72009-03-10 12:39:20 -0400619 rcu_read_lock();
620 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400621 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400622 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400623 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400624 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400625 }
Chris Mason4184ea72009-03-10 12:39:20 -0400626 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400627 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400628}
629
Chris Mason4184ea72009-03-10 12:39:20 -0400630/*
631 * after adding space to the filesystem, we need to clear the full flags
632 * on all the space infos.
633 */
634void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
635{
636 struct list_head *head = &info->space_info;
637 struct btrfs_space_info *found;
638
639 rcu_read_lock();
640 list_for_each_entry_rcu(found, head, list)
641 found->full = 0;
642 rcu_read_unlock();
643}
644
Yan Zhengd2fb3432008-12-11 16:30:39 -0500645u64 btrfs_find_block_group(struct btrfs_root *root,
646 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400647{
Chris Mason96b51792007-10-15 16:15:19 -0400648 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400649 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500650 u64 last = max(search_hint, search_start);
651 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400652 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500653 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400654 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400655again:
Zheng Yane8569812008-09-26 10:05:48 -0400656 while (1) {
657 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400658 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400659 break;
Chris Mason96b51792007-10-15 16:15:19 -0400660
Chris Masonc286ac42008-07-22 23:06:41 -0400661 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400662 last = cache->key.objectid + cache->key.offset;
663 used = btrfs_block_group_used(&cache->item);
664
Yan Zhengd2fb3432008-12-11 16:30:39 -0500665 if ((full_search || !cache->ro) &&
666 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400667 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500668 div_factor(cache->key.offset, factor)) {
669 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400670 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400671 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400672 goto found;
673 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400674 }
Chris Masonc286ac42008-07-22 23:06:41 -0400675 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400676 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400677 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400678 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400679 if (!wrapped) {
680 last = search_start;
681 wrapped = 1;
682 goto again;
683 }
684 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400685 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400686 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400687 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400688 goto again;
689 }
Chris Masonbe744172007-05-06 10:15:01 -0400690found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500691 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400692}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400693
Chris Masone02119d2008-09-05 16:13:11 -0400694/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400695int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400696{
697 int ret;
698 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400699 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400700
Zheng Yan31840ae2008-09-23 13:14:14 -0400701 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700702 if (!path)
703 return -ENOMEM;
704
Chris Masone02119d2008-09-05 16:13:11 -0400705 key.objectid = start;
706 key.offset = len;
707 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
708 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
709 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400710 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500711 return ret;
712}
713
Chris Masond8d5f3e2007-12-11 12:42:00 -0500714/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400715 * helper function to lookup reference count and flags of extent.
716 *
717 * the head node for delayed ref is used to store the sum of all the
718 * reference count modifications queued up in the rbtree. the head
719 * node may also store the extent flags to set. This way you can check
720 * to see what the reference count and extent flags would be if all of
721 * the delayed refs are not processed.
722 */
723int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
724 struct btrfs_root *root, u64 bytenr,
725 u64 num_bytes, u64 *refs, u64 *flags)
726{
727 struct btrfs_delayed_ref_head *head;
728 struct btrfs_delayed_ref_root *delayed_refs;
729 struct btrfs_path *path;
730 struct btrfs_extent_item *ei;
731 struct extent_buffer *leaf;
732 struct btrfs_key key;
733 u32 item_size;
734 u64 num_refs;
735 u64 extent_flags;
736 int ret;
737
738 path = btrfs_alloc_path();
739 if (!path)
740 return -ENOMEM;
741
742 key.objectid = bytenr;
743 key.type = BTRFS_EXTENT_ITEM_KEY;
744 key.offset = num_bytes;
745 if (!trans) {
746 path->skip_locking = 1;
747 path->search_commit_root = 1;
748 }
749again:
750 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
751 &key, path, 0, 0);
752 if (ret < 0)
753 goto out_free;
754
755 if (ret == 0) {
756 leaf = path->nodes[0];
757 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
758 if (item_size >= sizeof(*ei)) {
759 ei = btrfs_item_ptr(leaf, path->slots[0],
760 struct btrfs_extent_item);
761 num_refs = btrfs_extent_refs(leaf, ei);
762 extent_flags = btrfs_extent_flags(leaf, ei);
763 } else {
764#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
765 struct btrfs_extent_item_v0 *ei0;
766 BUG_ON(item_size != sizeof(*ei0));
767 ei0 = btrfs_item_ptr(leaf, path->slots[0],
768 struct btrfs_extent_item_v0);
769 num_refs = btrfs_extent_refs_v0(leaf, ei0);
770 /* FIXME: this isn't correct for data */
771 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
772#else
773 BUG();
774#endif
775 }
776 BUG_ON(num_refs == 0);
777 } else {
778 num_refs = 0;
779 extent_flags = 0;
780 ret = 0;
781 }
782
783 if (!trans)
784 goto out;
785
786 delayed_refs = &trans->transaction->delayed_refs;
787 spin_lock(&delayed_refs->lock);
788 head = btrfs_find_delayed_ref_head(trans, bytenr);
789 if (head) {
790 if (!mutex_trylock(&head->mutex)) {
791 atomic_inc(&head->node.refs);
792 spin_unlock(&delayed_refs->lock);
793
David Sterbab3b4aa72011-04-21 01:20:15 +0200794 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400795
David Sterba8cc33e52011-05-02 15:29:25 +0200796 /*
797 * Mutex was contended, block until it's released and try
798 * again
799 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400800 mutex_lock(&head->mutex);
801 mutex_unlock(&head->mutex);
802 btrfs_put_delayed_ref(&head->node);
803 goto again;
804 }
805 if (head->extent_op && head->extent_op->update_flags)
806 extent_flags |= head->extent_op->flags_to_set;
807 else
808 BUG_ON(num_refs == 0);
809
810 num_refs += head->node.ref_mod;
811 mutex_unlock(&head->mutex);
812 }
813 spin_unlock(&delayed_refs->lock);
814out:
815 WARN_ON(num_refs == 0);
816 if (refs)
817 *refs = num_refs;
818 if (flags)
819 *flags = extent_flags;
820out_free:
821 btrfs_free_path(path);
822 return ret;
823}
824
825/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500826 * Back reference rules. Back refs have three main goals:
827 *
828 * 1) differentiate between all holders of references to an extent so that
829 * when a reference is dropped we can make sure it was a valid reference
830 * before freeing the extent.
831 *
832 * 2) Provide enough information to quickly find the holders of an extent
833 * if we notice a given block is corrupted or bad.
834 *
835 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
836 * maintenance. This is actually the same as #2, but with a slightly
837 * different use case.
838 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400839 * There are two kinds of back refs. The implicit back refs is optimized
840 * for pointers in non-shared tree blocks. For a given pointer in a block,
841 * back refs of this kind provide information about the block's owner tree
842 * and the pointer's key. These information allow us to find the block by
843 * b-tree searching. The full back refs is for pointers in tree blocks not
844 * referenced by their owner trees. The location of tree block is recorded
845 * in the back refs. Actually the full back refs is generic, and can be
846 * used in all cases the implicit back refs is used. The major shortcoming
847 * of the full back refs is its overhead. Every time a tree block gets
848 * COWed, we have to update back refs entry for all pointers in it.
849 *
850 * For a newly allocated tree block, we use implicit back refs for
851 * pointers in it. This means most tree related operations only involve
852 * implicit back refs. For a tree block created in old transaction, the
853 * only way to drop a reference to it is COW it. So we can detect the
854 * event that tree block loses its owner tree's reference and do the
855 * back refs conversion.
856 *
857 * When a tree block is COW'd through a tree, there are four cases:
858 *
859 * The reference count of the block is one and the tree is the block's
860 * owner tree. Nothing to do in this case.
861 *
862 * The reference count of the block is one and the tree is not the
863 * block's owner tree. In this case, full back refs is used for pointers
864 * in the block. Remove these full back refs, add implicit back refs for
865 * every pointers in the new block.
866 *
867 * The reference count of the block is greater than one and the tree is
868 * the block's owner tree. In this case, implicit back refs is used for
869 * pointers in the block. Add full back refs for every pointers in the
870 * block, increase lower level extents' reference counts. The original
871 * implicit back refs are entailed to the new block.
872 *
873 * The reference count of the block is greater than one and the tree is
874 * not the block's owner tree. Add implicit back refs for every pointer in
875 * the new block, increase lower level extents' reference count.
876 *
877 * Back Reference Key composing:
878 *
879 * The key objectid corresponds to the first byte in the extent,
880 * The key type is used to differentiate between types of back refs.
881 * There are different meanings of the key offset for different types
882 * of back refs.
883 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500884 * File extents can be referenced by:
885 *
886 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400887 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500888 * - different offsets inside a file (bookend extents in file.c)
889 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400890 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500891 *
892 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500893 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400894 * - original offset in the file
895 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400896 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400897 * The key offset for the implicit back refs is hash of the first
898 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500899 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500901 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500903 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904 * The key offset for the implicit back refs is the first byte of
905 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500906 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907 * When a file extent is allocated, The implicit back refs is used.
908 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500909 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910 * (root_key.objectid, inode objectid, offset in file, 1)
911 *
912 * When a file extent is removed file truncation, we find the
913 * corresponding implicit back refs and check the following fields:
914 *
915 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500916 *
917 * Btree extents can be referenced by:
918 *
919 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500920 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400921 * Both the implicit back refs and the full back refs for tree blocks
922 * only consist of key. The key offset for the implicit back refs is
923 * objectid of block's owner tree. The key offset for the full back refs
924 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500925 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 * When implicit back refs is used, information about the lowest key and
927 * level of the tree block are required. These information are stored in
928 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500929 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400930
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400931#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
932static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
933 struct btrfs_root *root,
934 struct btrfs_path *path,
935 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500936{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400937 struct btrfs_extent_item *item;
938 struct btrfs_extent_item_v0 *ei0;
939 struct btrfs_extent_ref_v0 *ref0;
940 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400941 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942 struct btrfs_key key;
943 struct btrfs_key found_key;
944 u32 new_size = sizeof(*item);
945 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500946 int ret;
947
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400948 leaf = path->nodes[0];
949 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500950
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400951 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
952 ei0 = btrfs_item_ptr(leaf, path->slots[0],
953 struct btrfs_extent_item_v0);
954 refs = btrfs_extent_refs_v0(leaf, ei0);
955
956 if (owner == (u64)-1) {
957 while (1) {
958 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
959 ret = btrfs_next_leaf(root, path);
960 if (ret < 0)
961 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100962 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 leaf = path->nodes[0];
964 }
965 btrfs_item_key_to_cpu(leaf, &found_key,
966 path->slots[0]);
967 BUG_ON(key.objectid != found_key.objectid);
968 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
969 path->slots[0]++;
970 continue;
971 }
972 ref0 = btrfs_item_ptr(leaf, path->slots[0],
973 struct btrfs_extent_ref_v0);
974 owner = btrfs_ref_objectid_v0(leaf, ref0);
975 break;
976 }
977 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200978 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400979
980 if (owner < BTRFS_FIRST_FREE_OBJECTID)
981 new_size += sizeof(*bi);
982
983 new_size -= sizeof(*ei0);
984 ret = btrfs_search_slot(trans, root, &key, path,
985 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400986 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100988 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400989
Jeff Mahoney143bede2012-03-01 14:56:26 +0100990 btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991
992 leaf = path->nodes[0];
993 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
994 btrfs_set_extent_refs(leaf, item, refs);
995 /* FIXME: get real generation */
996 btrfs_set_extent_generation(leaf, item, 0);
997 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
998 btrfs_set_extent_flags(leaf, item,
999 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1000 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1001 bi = (struct btrfs_tree_block_info *)(item + 1);
1002 /* FIXME: get first key of the block */
1003 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1004 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1005 } else {
1006 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1007 }
1008 btrfs_mark_buffer_dirty(leaf);
1009 return 0;
1010}
1011#endif
1012
1013static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1014{
1015 u32 high_crc = ~(u32)0;
1016 u32 low_crc = ~(u32)0;
1017 __le64 lenum;
1018
1019 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001020 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001022 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001023 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001024 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001025
1026 return ((u64)high_crc << 31) ^ (u64)low_crc;
1027}
1028
1029static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1030 struct btrfs_extent_data_ref *ref)
1031{
1032 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1033 btrfs_extent_data_ref_objectid(leaf, ref),
1034 btrfs_extent_data_ref_offset(leaf, ref));
1035}
1036
1037static int match_extent_data_ref(struct extent_buffer *leaf,
1038 struct btrfs_extent_data_ref *ref,
1039 u64 root_objectid, u64 owner, u64 offset)
1040{
1041 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1042 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1043 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1044 return 0;
1045 return 1;
1046}
1047
1048static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1049 struct btrfs_root *root,
1050 struct btrfs_path *path,
1051 u64 bytenr, u64 parent,
1052 u64 root_objectid,
1053 u64 owner, u64 offset)
1054{
1055 struct btrfs_key key;
1056 struct btrfs_extent_data_ref *ref;
1057 struct extent_buffer *leaf;
1058 u32 nritems;
1059 int ret;
1060 int recow;
1061 int err = -ENOENT;
1062
1063 key.objectid = bytenr;
1064 if (parent) {
1065 key.type = BTRFS_SHARED_DATA_REF_KEY;
1066 key.offset = parent;
1067 } else {
1068 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1069 key.offset = hash_extent_data_ref(root_objectid,
1070 owner, offset);
1071 }
1072again:
1073 recow = 0;
1074 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1075 if (ret < 0) {
1076 err = ret;
1077 goto fail;
1078 }
1079
1080 if (parent) {
1081 if (!ret)
1082 return 0;
1083#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1084 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001085 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001086 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1087 if (ret < 0) {
1088 err = ret;
1089 goto fail;
1090 }
1091 if (!ret)
1092 return 0;
1093#endif
1094 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001095 }
1096
1097 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098 nritems = btrfs_header_nritems(leaf);
1099 while (1) {
1100 if (path->slots[0] >= nritems) {
1101 ret = btrfs_next_leaf(root, path);
1102 if (ret < 0)
1103 err = ret;
1104 if (ret)
1105 goto fail;
1106
1107 leaf = path->nodes[0];
1108 nritems = btrfs_header_nritems(leaf);
1109 recow = 1;
1110 }
1111
1112 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1113 if (key.objectid != bytenr ||
1114 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1115 goto fail;
1116
1117 ref = btrfs_item_ptr(leaf, path->slots[0],
1118 struct btrfs_extent_data_ref);
1119
1120 if (match_extent_data_ref(leaf, ref, root_objectid,
1121 owner, offset)) {
1122 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001123 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001124 goto again;
1125 }
1126 err = 0;
1127 break;
1128 }
1129 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001130 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001131fail:
1132 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001133}
1134
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001135static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1136 struct btrfs_root *root,
1137 struct btrfs_path *path,
1138 u64 bytenr, u64 parent,
1139 u64 root_objectid, u64 owner,
1140 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001141{
1142 struct btrfs_key key;
1143 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001144 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001145 u32 num_refs;
1146 int ret;
1147
1148 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001149 if (parent) {
1150 key.type = BTRFS_SHARED_DATA_REF_KEY;
1151 key.offset = parent;
1152 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001153 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001154 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1155 key.offset = hash_extent_data_ref(root_objectid,
1156 owner, offset);
1157 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001158 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001159
1160 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1161 if (ret && ret != -EEXIST)
1162 goto fail;
1163
1164 leaf = path->nodes[0];
1165 if (parent) {
1166 struct btrfs_shared_data_ref *ref;
1167 ref = btrfs_item_ptr(leaf, path->slots[0],
1168 struct btrfs_shared_data_ref);
1169 if (ret == 0) {
1170 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1171 } else {
1172 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1173 num_refs += refs_to_add;
1174 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1175 }
1176 } else {
1177 struct btrfs_extent_data_ref *ref;
1178 while (ret == -EEXIST) {
1179 ref = btrfs_item_ptr(leaf, path->slots[0],
1180 struct btrfs_extent_data_ref);
1181 if (match_extent_data_ref(leaf, ref, root_objectid,
1182 owner, offset))
1183 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001184 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001185 key.offset++;
1186 ret = btrfs_insert_empty_item(trans, root, path, &key,
1187 size);
1188 if (ret && ret != -EEXIST)
1189 goto fail;
1190
1191 leaf = path->nodes[0];
1192 }
1193 ref = btrfs_item_ptr(leaf, path->slots[0],
1194 struct btrfs_extent_data_ref);
1195 if (ret == 0) {
1196 btrfs_set_extent_data_ref_root(leaf, ref,
1197 root_objectid);
1198 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1199 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1200 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1201 } else {
1202 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1203 num_refs += refs_to_add;
1204 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1205 }
1206 }
1207 btrfs_mark_buffer_dirty(leaf);
1208 ret = 0;
1209fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001210 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001211 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001212}
1213
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root,
1216 struct btrfs_path *path,
1217 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001218{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001219 struct btrfs_key key;
1220 struct btrfs_extent_data_ref *ref1 = NULL;
1221 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001222 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001223 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001224 int ret = 0;
1225
1226 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001227 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1228
1229 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1230 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1231 struct btrfs_extent_data_ref);
1232 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1233 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1234 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1235 struct btrfs_shared_data_ref);
1236 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1237#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1238 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1239 struct btrfs_extent_ref_v0 *ref0;
1240 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1241 struct btrfs_extent_ref_v0);
1242 num_refs = btrfs_ref_count_v0(leaf, ref0);
1243#endif
1244 } else {
1245 BUG();
1246 }
1247
Chris Mason56bec292009-03-13 10:10:06 -04001248 BUG_ON(num_refs < refs_to_drop);
1249 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001250
Zheng Yan31840ae2008-09-23 13:14:14 -04001251 if (num_refs == 0) {
1252 ret = btrfs_del_item(trans, root, path);
1253 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001254 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1255 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1256 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1257 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1258#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1259 else {
1260 struct btrfs_extent_ref_v0 *ref0;
1261 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1262 struct btrfs_extent_ref_v0);
1263 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1264 }
1265#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001266 btrfs_mark_buffer_dirty(leaf);
1267 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 return ret;
1269}
1270
1271static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1272 struct btrfs_path *path,
1273 struct btrfs_extent_inline_ref *iref)
1274{
1275 struct btrfs_key key;
1276 struct extent_buffer *leaf;
1277 struct btrfs_extent_data_ref *ref1;
1278 struct btrfs_shared_data_ref *ref2;
1279 u32 num_refs = 0;
1280
1281 leaf = path->nodes[0];
1282 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1283 if (iref) {
1284 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1285 BTRFS_EXTENT_DATA_REF_KEY) {
1286 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1287 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1288 } else {
1289 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1290 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1291 }
1292 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1293 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1294 struct btrfs_extent_data_ref);
1295 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1296 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1297 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1298 struct btrfs_shared_data_ref);
1299 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1300#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1301 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1302 struct btrfs_extent_ref_v0 *ref0;
1303 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1304 struct btrfs_extent_ref_v0);
1305 num_refs = btrfs_ref_count_v0(leaf, ref0);
1306#endif
1307 } else {
1308 WARN_ON(1);
1309 }
1310 return num_refs;
1311}
1312
1313static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1314 struct btrfs_root *root,
1315 struct btrfs_path *path,
1316 u64 bytenr, u64 parent,
1317 u64 root_objectid)
1318{
1319 struct btrfs_key key;
1320 int ret;
1321
1322 key.objectid = bytenr;
1323 if (parent) {
1324 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1325 key.offset = parent;
1326 } else {
1327 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1328 key.offset = root_objectid;
1329 }
1330
1331 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1332 if (ret > 0)
1333 ret = -ENOENT;
1334#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1335 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001336 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001337 key.type = BTRFS_EXTENT_REF_V0_KEY;
1338 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1339 if (ret > 0)
1340 ret = -ENOENT;
1341 }
1342#endif
1343 return ret;
1344}
1345
1346static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1347 struct btrfs_root *root,
1348 struct btrfs_path *path,
1349 u64 bytenr, u64 parent,
1350 u64 root_objectid)
1351{
1352 struct btrfs_key key;
1353 int ret;
1354
1355 key.objectid = bytenr;
1356 if (parent) {
1357 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1358 key.offset = parent;
1359 } else {
1360 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1361 key.offset = root_objectid;
1362 }
1363
1364 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001365 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001366 return ret;
1367}
1368
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001369static inline int extent_ref_type(u64 parent, u64 owner)
1370{
1371 int type;
1372 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1373 if (parent > 0)
1374 type = BTRFS_SHARED_BLOCK_REF_KEY;
1375 else
1376 type = BTRFS_TREE_BLOCK_REF_KEY;
1377 } else {
1378 if (parent > 0)
1379 type = BTRFS_SHARED_DATA_REF_KEY;
1380 else
1381 type = BTRFS_EXTENT_DATA_REF_KEY;
1382 }
1383 return type;
1384}
1385
Yan Zheng2c47e6052009-06-27 21:07:35 -04001386static int find_next_key(struct btrfs_path *path, int level,
1387 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001388
1389{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001390 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001391 if (!path->nodes[level])
1392 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393 if (path->slots[level] + 1 >=
1394 btrfs_header_nritems(path->nodes[level]))
1395 continue;
1396 if (level == 0)
1397 btrfs_item_key_to_cpu(path->nodes[level], key,
1398 path->slots[level] + 1);
1399 else
1400 btrfs_node_key_to_cpu(path->nodes[level], key,
1401 path->slots[level] + 1);
1402 return 0;
1403 }
1404 return 1;
1405}
1406
1407/*
1408 * look for inline back ref. if back ref is found, *ref_ret is set
1409 * to the address of inline back ref, and 0 is returned.
1410 *
1411 * if back ref isn't found, *ref_ret is set to the address where it
1412 * should be inserted, and -ENOENT is returned.
1413 *
1414 * if insert is true and there are too many inline back refs, the path
1415 * points to the extent item, and -EAGAIN is returned.
1416 *
1417 * NOTE: inline back refs are ordered in the same way that back ref
1418 * items in the tree are ordered.
1419 */
1420static noinline_for_stack
1421int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1422 struct btrfs_root *root,
1423 struct btrfs_path *path,
1424 struct btrfs_extent_inline_ref **ref_ret,
1425 u64 bytenr, u64 num_bytes,
1426 u64 parent, u64 root_objectid,
1427 u64 owner, u64 offset, int insert)
1428{
1429 struct btrfs_key key;
1430 struct extent_buffer *leaf;
1431 struct btrfs_extent_item *ei;
1432 struct btrfs_extent_inline_ref *iref;
1433 u64 flags;
1434 u64 item_size;
1435 unsigned long ptr;
1436 unsigned long end;
1437 int extra_size;
1438 int type;
1439 int want;
1440 int ret;
1441 int err = 0;
1442
1443 key.objectid = bytenr;
1444 key.type = BTRFS_EXTENT_ITEM_KEY;
1445 key.offset = num_bytes;
1446
1447 want = extent_ref_type(parent, owner);
1448 if (insert) {
1449 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001450 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001451 } else
1452 extra_size = -1;
1453 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1454 if (ret < 0) {
1455 err = ret;
1456 goto out;
1457 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 if (ret && !insert) {
1459 err = -ENOENT;
1460 goto out;
1461 }
1462 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001463
1464 leaf = path->nodes[0];
1465 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1466#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1467 if (item_size < sizeof(*ei)) {
1468 if (!insert) {
1469 err = -ENOENT;
1470 goto out;
1471 }
1472 ret = convert_extent_item_v0(trans, root, path, owner,
1473 extra_size);
1474 if (ret < 0) {
1475 err = ret;
1476 goto out;
1477 }
1478 leaf = path->nodes[0];
1479 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1480 }
1481#endif
1482 BUG_ON(item_size < sizeof(*ei));
1483
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001484 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1485 flags = btrfs_extent_flags(leaf, ei);
1486
1487 ptr = (unsigned long)(ei + 1);
1488 end = (unsigned long)ei + item_size;
1489
1490 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1491 ptr += sizeof(struct btrfs_tree_block_info);
1492 BUG_ON(ptr > end);
1493 } else {
1494 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1495 }
1496
1497 err = -ENOENT;
1498 while (1) {
1499 if (ptr >= end) {
1500 WARN_ON(ptr > end);
1501 break;
1502 }
1503 iref = (struct btrfs_extent_inline_ref *)ptr;
1504 type = btrfs_extent_inline_ref_type(leaf, iref);
1505 if (want < type)
1506 break;
1507 if (want > type) {
1508 ptr += btrfs_extent_inline_ref_size(type);
1509 continue;
1510 }
1511
1512 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1513 struct btrfs_extent_data_ref *dref;
1514 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1515 if (match_extent_data_ref(leaf, dref, root_objectid,
1516 owner, offset)) {
1517 err = 0;
1518 break;
1519 }
1520 if (hash_extent_data_ref_item(leaf, dref) <
1521 hash_extent_data_ref(root_objectid, owner, offset))
1522 break;
1523 } else {
1524 u64 ref_offset;
1525 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1526 if (parent > 0) {
1527 if (parent == ref_offset) {
1528 err = 0;
1529 break;
1530 }
1531 if (ref_offset < parent)
1532 break;
1533 } else {
1534 if (root_objectid == ref_offset) {
1535 err = 0;
1536 break;
1537 }
1538 if (ref_offset < root_objectid)
1539 break;
1540 }
1541 }
1542 ptr += btrfs_extent_inline_ref_size(type);
1543 }
1544 if (err == -ENOENT && insert) {
1545 if (item_size + extra_size >=
1546 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1547 err = -EAGAIN;
1548 goto out;
1549 }
1550 /*
1551 * To add new inline back ref, we have to make sure
1552 * there is no corresponding back ref item.
1553 * For simplicity, we just do not add new inline back
1554 * ref if there is any kind of item for this block
1555 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001556 if (find_next_key(path, 0, &key) == 0 &&
1557 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001558 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001559 err = -EAGAIN;
1560 goto out;
1561 }
1562 }
1563 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1564out:
Yan Zheng85d41982009-06-11 08:51:10 -04001565 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001566 path->keep_locks = 0;
1567 btrfs_unlock_up_safe(path, 1);
1568 }
1569 return err;
1570}
1571
1572/*
1573 * helper to add new inline back ref
1574 */
1575static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001576void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1577 struct btrfs_root *root,
1578 struct btrfs_path *path,
1579 struct btrfs_extent_inline_ref *iref,
1580 u64 parent, u64 root_objectid,
1581 u64 owner, u64 offset, int refs_to_add,
1582 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001583{
1584 struct extent_buffer *leaf;
1585 struct btrfs_extent_item *ei;
1586 unsigned long ptr;
1587 unsigned long end;
1588 unsigned long item_offset;
1589 u64 refs;
1590 int size;
1591 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001592
1593 leaf = path->nodes[0];
1594 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1595 item_offset = (unsigned long)iref - (unsigned long)ei;
1596
1597 type = extent_ref_type(parent, owner);
1598 size = btrfs_extent_inline_ref_size(type);
1599
Jeff Mahoney143bede2012-03-01 14:56:26 +01001600 btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001601
1602 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1603 refs = btrfs_extent_refs(leaf, ei);
1604 refs += refs_to_add;
1605 btrfs_set_extent_refs(leaf, ei, refs);
1606 if (extent_op)
1607 __run_delayed_extent_op(extent_op, leaf, ei);
1608
1609 ptr = (unsigned long)ei + item_offset;
1610 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1611 if (ptr < end - size)
1612 memmove_extent_buffer(leaf, ptr + size, ptr,
1613 end - size - ptr);
1614
1615 iref = (struct btrfs_extent_inline_ref *)ptr;
1616 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1617 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1618 struct btrfs_extent_data_ref *dref;
1619 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1620 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1621 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1622 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1623 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1624 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1625 struct btrfs_shared_data_ref *sref;
1626 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1627 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1628 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1629 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1630 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1631 } else {
1632 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1633 }
1634 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001635}
1636
1637static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1638 struct btrfs_root *root,
1639 struct btrfs_path *path,
1640 struct btrfs_extent_inline_ref **ref_ret,
1641 u64 bytenr, u64 num_bytes, u64 parent,
1642 u64 root_objectid, u64 owner, u64 offset)
1643{
1644 int ret;
1645
1646 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1647 bytenr, num_bytes, parent,
1648 root_objectid, owner, offset, 0);
1649 if (ret != -ENOENT)
1650 return ret;
1651
David Sterbab3b4aa72011-04-21 01:20:15 +02001652 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001653 *ref_ret = NULL;
1654
1655 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1656 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1657 root_objectid);
1658 } else {
1659 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1660 root_objectid, owner, offset);
1661 }
1662 return ret;
1663}
1664
1665/*
1666 * helper to update/remove inline back ref
1667 */
1668static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001669void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1670 struct btrfs_root *root,
1671 struct btrfs_path *path,
1672 struct btrfs_extent_inline_ref *iref,
1673 int refs_to_mod,
1674 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675{
1676 struct extent_buffer *leaf;
1677 struct btrfs_extent_item *ei;
1678 struct btrfs_extent_data_ref *dref = NULL;
1679 struct btrfs_shared_data_ref *sref = NULL;
1680 unsigned long ptr;
1681 unsigned long end;
1682 u32 item_size;
1683 int size;
1684 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001685 u64 refs;
1686
1687 leaf = path->nodes[0];
1688 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1689 refs = btrfs_extent_refs(leaf, ei);
1690 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1691 refs += refs_to_mod;
1692 btrfs_set_extent_refs(leaf, ei, refs);
1693 if (extent_op)
1694 __run_delayed_extent_op(extent_op, leaf, ei);
1695
1696 type = btrfs_extent_inline_ref_type(leaf, iref);
1697
1698 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1699 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1700 refs = btrfs_extent_data_ref_count(leaf, dref);
1701 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1702 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1703 refs = btrfs_shared_data_ref_count(leaf, sref);
1704 } else {
1705 refs = 1;
1706 BUG_ON(refs_to_mod != -1);
1707 }
1708
1709 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1710 refs += refs_to_mod;
1711
1712 if (refs > 0) {
1713 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1714 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1715 else
1716 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1717 } else {
1718 size = btrfs_extent_inline_ref_size(type);
1719 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1720 ptr = (unsigned long)iref;
1721 end = (unsigned long)ei + item_size;
1722 if (ptr + size < end)
1723 memmove_extent_buffer(leaf, ptr, ptr + size,
1724 end - ptr - size);
1725 item_size -= size;
Jeff Mahoney143bede2012-03-01 14:56:26 +01001726 btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001727 }
1728 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001729}
1730
1731static noinline_for_stack
1732int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1733 struct btrfs_root *root,
1734 struct btrfs_path *path,
1735 u64 bytenr, u64 num_bytes, u64 parent,
1736 u64 root_objectid, u64 owner,
1737 u64 offset, int refs_to_add,
1738 struct btrfs_delayed_extent_op *extent_op)
1739{
1740 struct btrfs_extent_inline_ref *iref;
1741 int ret;
1742
1743 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1744 bytenr, num_bytes, parent,
1745 root_objectid, owner, offset, 1);
1746 if (ret == 0) {
1747 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001748 update_inline_extent_backref(trans, root, path, iref,
1749 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001750 } else if (ret == -ENOENT) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001751 setup_inline_extent_backref(trans, root, path, iref, parent,
1752 root_objectid, owner, offset,
1753 refs_to_add, extent_op);
1754 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001755 }
1756 return ret;
1757}
1758
1759static int insert_extent_backref(struct btrfs_trans_handle *trans,
1760 struct btrfs_root *root,
1761 struct btrfs_path *path,
1762 u64 bytenr, u64 parent, u64 root_objectid,
1763 u64 owner, u64 offset, int refs_to_add)
1764{
1765 int ret;
1766 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1767 BUG_ON(refs_to_add != 1);
1768 ret = insert_tree_block_ref(trans, root, path, bytenr,
1769 parent, root_objectid);
1770 } else {
1771 ret = insert_extent_data_ref(trans, root, path, bytenr,
1772 parent, root_objectid,
1773 owner, offset, refs_to_add);
1774 }
1775 return ret;
1776}
1777
1778static int remove_extent_backref(struct btrfs_trans_handle *trans,
1779 struct btrfs_root *root,
1780 struct btrfs_path *path,
1781 struct btrfs_extent_inline_ref *iref,
1782 int refs_to_drop, int is_data)
1783{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001784 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001785
1786 BUG_ON(!is_data && refs_to_drop != 1);
1787 if (iref) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001788 update_inline_extent_backref(trans, root, path, iref,
1789 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001790 } else if (is_data) {
1791 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1792 } else {
1793 ret = btrfs_del_item(trans, root, path);
1794 }
1795 return ret;
1796}
1797
Li Dongyang5378e602011-03-24 10:24:27 +00001798static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001799 u64 start, u64 len)
1800{
Li Dongyang5378e602011-03-24 10:24:27 +00001801 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001802}
Chris Mason15916de2008-11-19 21:17:22 -05001803
Liu Hui1f3c79a2009-01-05 15:57:51 -05001804static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001805 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001806{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001807 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001808 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001809 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001810
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001811
Liu Hui1f3c79a2009-01-05 15:57:51 -05001812 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001813 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001814 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001815 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001816 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001817 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001818 int i;
1819
Liu Hui1f3c79a2009-01-05 15:57:51 -05001820
Jan Schmidta1d3c472011-08-04 17:15:33 +02001821 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001822 if (!stripe->dev->can_discard)
1823 continue;
1824
Li Dongyang5378e602011-03-24 10:24:27 +00001825 ret = btrfs_issue_discard(stripe->dev->bdev,
1826 stripe->physical,
1827 stripe->length);
1828 if (!ret)
1829 discarded_bytes += stripe->length;
1830 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001831 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001832
1833 /*
1834 * Just in case we get back EOPNOTSUPP for some reason,
1835 * just ignore the return value so we don't screw up
1836 * people calling discard_extent.
1837 */
1838 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001839 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001840 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001841 }
Li Dongyang5378e602011-03-24 10:24:27 +00001842
1843 if (actual_bytes)
1844 *actual_bytes = discarded_bytes;
1845
Liu Hui1f3c79a2009-01-05 15:57:51 -05001846
1847 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001848}
1849
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001850/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001851int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1852 struct btrfs_root *root,
1853 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001854 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001855{
1856 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001857 struct btrfs_fs_info *fs_info = root->fs_info;
1858
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001859 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1860 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001861
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001862 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001863 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1864 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001865 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001866 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001867 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001868 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1869 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001871 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001873 return ret;
1874}
1875
Chris Mason925baed2008-06-25 16:01:30 -04001876static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877 struct btrfs_root *root,
1878 u64 bytenr, u64 num_bytes,
1879 u64 parent, u64 root_objectid,
1880 u64 owner, u64 offset, int refs_to_add,
1881 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001882{
Chris Mason5caf2a02007-04-02 11:20:42 -04001883 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001884 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001885 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001886 u64 refs;
1887 int ret;
1888 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001889
Chris Mason5caf2a02007-04-02 11:20:42 -04001890 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001891 if (!path)
1892 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001893
Chris Mason3c12ac72008-04-21 12:01:38 -04001894 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001895 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001896 /* this will setup the path even if it fails to insert the back ref */
1897 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1898 path, bytenr, num_bytes, parent,
1899 root_objectid, owner, offset,
1900 refs_to_add, extent_op);
1901 if (ret == 0)
1902 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001903
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001904 if (ret != -EAGAIN) {
1905 err = ret;
1906 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001907 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001908
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001909 leaf = path->nodes[0];
1910 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1911 refs = btrfs_extent_refs(leaf, item);
1912 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1913 if (extent_op)
1914 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001915
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001916 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001917 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001918
Chris Mason3c12ac72008-04-21 12:01:38 -04001919 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001920 path->leave_spinning = 1;
1921
Chris Mason56bec292009-03-13 10:10:06 -04001922 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001923 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001924 path, bytenr, parent, root_objectid,
1925 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001926 if (ret)
1927 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001928out:
Chris Mason74493f72007-12-11 09:25:06 -05001929 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001930 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001931}
1932
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001933static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1934 struct btrfs_root *root,
1935 struct btrfs_delayed_ref_node *node,
1936 struct btrfs_delayed_extent_op *extent_op,
1937 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001938{
Chris Mason56bec292009-03-13 10:10:06 -04001939 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001940 struct btrfs_delayed_data_ref *ref;
1941 struct btrfs_key ins;
1942 u64 parent = 0;
1943 u64 ref_root = 0;
1944 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001945
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946 ins.objectid = node->bytenr;
1947 ins.offset = node->num_bytes;
1948 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001949
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001950 ref = btrfs_delayed_node_to_data_ref(node);
1951 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1952 parent = ref->parent;
1953 else
1954 ref_root = ref->root;
1955
1956 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1957 if (extent_op) {
1958 BUG_ON(extent_op->update_key);
1959 flags |= extent_op->flags_to_set;
1960 }
1961 ret = alloc_reserved_file_extent(trans, root,
1962 parent, ref_root, flags,
1963 ref->objectid, ref->offset,
1964 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001965 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1966 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1967 node->num_bytes, parent,
1968 ref_root, ref->objectid,
1969 ref->offset, node->ref_mod,
1970 extent_op);
1971 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1972 ret = __btrfs_free_extent(trans, root, node->bytenr,
1973 node->num_bytes, parent,
1974 ref_root, ref->objectid,
1975 ref->offset, node->ref_mod,
1976 extent_op);
1977 } else {
1978 BUG();
1979 }
Chris Mason56bec292009-03-13 10:10:06 -04001980 return ret;
1981}
1982
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001983static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1984 struct extent_buffer *leaf,
1985 struct btrfs_extent_item *ei)
1986{
1987 u64 flags = btrfs_extent_flags(leaf, ei);
1988 if (extent_op->update_flags) {
1989 flags |= extent_op->flags_to_set;
1990 btrfs_set_extent_flags(leaf, ei, flags);
1991 }
1992
1993 if (extent_op->update_key) {
1994 struct btrfs_tree_block_info *bi;
1995 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1996 bi = (struct btrfs_tree_block_info *)(ei + 1);
1997 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1998 }
1999}
2000
2001static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2002 struct btrfs_root *root,
2003 struct btrfs_delayed_ref_node *node,
2004 struct btrfs_delayed_extent_op *extent_op)
2005{
2006 struct btrfs_key key;
2007 struct btrfs_path *path;
2008 struct btrfs_extent_item *ei;
2009 struct extent_buffer *leaf;
2010 u32 item_size;
2011 int ret;
2012 int err = 0;
2013
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002014 if (trans->aborted)
2015 return 0;
2016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002017 path = btrfs_alloc_path();
2018 if (!path)
2019 return -ENOMEM;
2020
2021 key.objectid = node->bytenr;
2022 key.type = BTRFS_EXTENT_ITEM_KEY;
2023 key.offset = node->num_bytes;
2024
2025 path->reada = 1;
2026 path->leave_spinning = 1;
2027 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2028 path, 0, 1);
2029 if (ret < 0) {
2030 err = ret;
2031 goto out;
2032 }
2033 if (ret > 0) {
2034 err = -EIO;
2035 goto out;
2036 }
2037
2038 leaf = path->nodes[0];
2039 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2040#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2041 if (item_size < sizeof(*ei)) {
2042 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2043 path, (u64)-1, 0);
2044 if (ret < 0) {
2045 err = ret;
2046 goto out;
2047 }
2048 leaf = path->nodes[0];
2049 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2050 }
2051#endif
2052 BUG_ON(item_size < sizeof(*ei));
2053 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2054 __run_delayed_extent_op(extent_op, leaf, ei);
2055
2056 btrfs_mark_buffer_dirty(leaf);
2057out:
2058 btrfs_free_path(path);
2059 return err;
2060}
2061
2062static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2063 struct btrfs_root *root,
2064 struct btrfs_delayed_ref_node *node,
2065 struct btrfs_delayed_extent_op *extent_op,
2066 int insert_reserved)
2067{
2068 int ret = 0;
2069 struct btrfs_delayed_tree_ref *ref;
2070 struct btrfs_key ins;
2071 u64 parent = 0;
2072 u64 ref_root = 0;
2073
2074 ins.objectid = node->bytenr;
2075 ins.offset = node->num_bytes;
2076 ins.type = BTRFS_EXTENT_ITEM_KEY;
2077
2078 ref = btrfs_delayed_node_to_tree_ref(node);
2079 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2080 parent = ref->parent;
2081 else
2082 ref_root = ref->root;
2083
2084 BUG_ON(node->ref_mod != 1);
2085 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2086 BUG_ON(!extent_op || !extent_op->update_flags ||
2087 !extent_op->update_key);
2088 ret = alloc_reserved_tree_block(trans, root,
2089 parent, ref_root,
2090 extent_op->flags_to_set,
2091 &extent_op->key,
2092 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2094 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2095 node->num_bytes, parent, ref_root,
2096 ref->level, 0, 1, extent_op);
2097 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2098 ret = __btrfs_free_extent(trans, root, node->bytenr,
2099 node->num_bytes, parent, ref_root,
2100 ref->level, 0, 1, extent_op);
2101 } else {
2102 BUG();
2103 }
2104 return ret;
2105}
2106
Chris Mason56bec292009-03-13 10:10:06 -04002107/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002108static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2109 struct btrfs_root *root,
2110 struct btrfs_delayed_ref_node *node,
2111 struct btrfs_delayed_extent_op *extent_op,
2112 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002113{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002114 int ret = 0;
2115
2116 if (trans->aborted)
2117 return 0;
2118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002120 struct btrfs_delayed_ref_head *head;
2121 /*
2122 * we've hit the end of the chain and we were supposed
2123 * to insert this extent into the tree. But, it got
2124 * deleted before we ever needed to insert it, so all
2125 * we have to do is clean up the accounting
2126 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002127 BUG_ON(extent_op);
2128 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002129 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002130 btrfs_pin_extent(root, node->bytenr,
2131 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002132 if (head->is_data) {
2133 ret = btrfs_del_csums(trans, root,
2134 node->bytenr,
2135 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002136 }
Chris Mason56bec292009-03-13 10:10:06 -04002137 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002138 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002139 }
Josef Bacikeb099672009-02-12 09:27:38 -05002140
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002141 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2142 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2143 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2144 insert_reserved);
2145 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2146 node->type == BTRFS_SHARED_DATA_REF_KEY)
2147 ret = run_delayed_data_ref(trans, root, node, extent_op,
2148 insert_reserved);
2149 else
2150 BUG();
2151 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002152}
2153
Chris Mason56bec292009-03-13 10:10:06 -04002154static noinline struct btrfs_delayed_ref_node *
2155select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002156{
Chris Mason56bec292009-03-13 10:10:06 -04002157 struct rb_node *node;
2158 struct btrfs_delayed_ref_node *ref;
2159 int action = BTRFS_ADD_DELAYED_REF;
2160again:
2161 /*
2162 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2163 * this prevents ref count from going down to zero when
2164 * there still are pending delayed ref.
2165 */
2166 node = rb_prev(&head->node.rb_node);
2167 while (1) {
2168 if (!node)
2169 break;
2170 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2171 rb_node);
2172 if (ref->bytenr != head->node.bytenr)
2173 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002174 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002175 return ref;
2176 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002177 }
Chris Mason56bec292009-03-13 10:10:06 -04002178 if (action == BTRFS_ADD_DELAYED_REF) {
2179 action = BTRFS_DROP_DELAYED_REF;
2180 goto again;
2181 }
2182 return NULL;
2183}
2184
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002185/*
2186 * Returns 0 on success or if called with an already aborted transaction.
2187 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2188 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002189static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2190 struct btrfs_root *root,
2191 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002192{
Chris Mason56bec292009-03-13 10:10:06 -04002193 struct btrfs_delayed_ref_root *delayed_refs;
2194 struct btrfs_delayed_ref_node *ref;
2195 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002196 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002197 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002198 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002199 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002200 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002201
2202 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002203 while (1) {
2204 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002205 /* pick a new head ref from the cluster list */
2206 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002207 break;
Chris Mason56bec292009-03-13 10:10:06 -04002208
Chris Masonc3e69d52009-03-13 10:17:05 -04002209 locked_ref = list_entry(cluster->next,
2210 struct btrfs_delayed_ref_head, cluster);
2211
2212 /* grab the lock that says we are going to process
2213 * all the refs for this head */
2214 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2215
2216 /*
2217 * we may have dropped the spin lock to get the head
2218 * mutex lock, and that might have given someone else
2219 * time to free the head. If that's true, it has been
2220 * removed from our list and we can move on.
2221 */
2222 if (ret == -EAGAIN) {
2223 locked_ref = NULL;
2224 count++;
2225 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002226 }
2227 }
2228
2229 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002230 * We need to try and merge add/drops of the same ref since we
2231 * can run into issues with relocate dropping the implicit ref
2232 * and then it being added back again before the drop can
2233 * finish. If we merged anything we need to re-loop so we can
2234 * get a good ref.
2235 */
2236 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2237 locked_ref);
2238
2239 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002240 * locked_ref is the head node, so we have to go one
2241 * node back for any delayed ref updates
2242 */
2243 ref = select_delayed_ref(locked_ref);
2244
2245 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002246 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002247 /*
2248 * there are still refs with lower seq numbers in the
2249 * process of being added. Don't run this ref yet.
2250 */
2251 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002252 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002253 locked_ref = NULL;
2254 delayed_refs->num_heads_ready++;
2255 spin_unlock(&delayed_refs->lock);
2256 cond_resched();
2257 spin_lock(&delayed_refs->lock);
2258 continue;
2259 }
2260
2261 /*
Chris Mason56bec292009-03-13 10:10:06 -04002262 * record the must insert reserved flag before we
2263 * drop the spin lock.
2264 */
2265 must_insert_reserved = locked_ref->must_insert_reserved;
2266 locked_ref->must_insert_reserved = 0;
2267
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268 extent_op = locked_ref->extent_op;
2269 locked_ref->extent_op = NULL;
2270
Chris Mason56bec292009-03-13 10:10:06 -04002271 if (!ref) {
2272 /* All delayed refs have been processed, Go ahead
2273 * and send the head node to run_one_delayed_ref,
2274 * so that any accounting fixes can happen
2275 */
2276 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002277
2278 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002279 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002280 extent_op = NULL;
2281 }
2282
2283 if (extent_op) {
2284 spin_unlock(&delayed_refs->lock);
2285
2286 ret = run_delayed_extent_op(trans, root,
2287 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002288 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002289
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002290 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002291 printk(KERN_DEBUG
2292 "btrfs: run_delayed_extent_op "
2293 "returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002294 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002295 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002296 return ret;
2297 }
2298
Chris Mason203bf282012-01-06 15:23:57 -05002299 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002300 }
Chris Mason56bec292009-03-13 10:10:06 -04002301 }
2302
2303 ref->in_tree = 0;
2304 rb_erase(&ref->rb_node, &delayed_refs->root);
2305 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002306 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002307 /*
2308 * when we play the delayed ref, also correct the
2309 * ref_mod on head
2310 */
2311 switch (ref->action) {
2312 case BTRFS_ADD_DELAYED_REF:
2313 case BTRFS_ADD_DELAYED_EXTENT:
2314 locked_ref->node.ref_mod -= ref->ref_mod;
2315 break;
2316 case BTRFS_DROP_DELAYED_REF:
2317 locked_ref->node.ref_mod += ref->ref_mod;
2318 break;
2319 default:
2320 WARN_ON(1);
2321 }
2322 }
Chris Mason56bec292009-03-13 10:10:06 -04002323 spin_unlock(&delayed_refs->lock);
2324
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002325 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002326 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002327
Miao Xie78a61842012-11-21 02:21:28 +00002328 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002329 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002330 btrfs_delayed_ref_unlock(locked_ref);
2331 btrfs_put_delayed_ref(ref);
2332 printk(KERN_DEBUG
2333 "btrfs: run_one_delayed_ref returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002334 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002335 return ret;
2336 }
2337
Miao Xie093486c2012-12-19 08:10:10 +00002338 /*
2339 * If this node is a head, that means all the refs in this head
2340 * have been dealt with, and we will pick the next head to deal
2341 * with, so we must unlock the head and drop it from the cluster
2342 * list before we release it.
2343 */
2344 if (btrfs_delayed_ref_is_head(ref)) {
2345 list_del_init(&locked_ref->cluster);
2346 btrfs_delayed_ref_unlock(locked_ref);
2347 locked_ref = NULL;
2348 }
2349 btrfs_put_delayed_ref(ref);
2350 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002351next:
Chris Mason1887be62009-03-13 10:11:24 -04002352 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002353 spin_lock(&delayed_refs->lock);
2354 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002355 return count;
2356}
2357
Arne Jansen709c0482011-09-12 12:22:57 +02002358#ifdef SCRAMBLE_DELAYED_REFS
2359/*
2360 * Normally delayed refs get processed in ascending bytenr order. This
2361 * correlates in most cases to the order added. To expose dependencies on this
2362 * order, we start to process the tree in the middle instead of the beginning
2363 */
2364static u64 find_middle(struct rb_root *root)
2365{
2366 struct rb_node *n = root->rb_node;
2367 struct btrfs_delayed_ref_node *entry;
2368 int alt = 1;
2369 u64 middle;
2370 u64 first = 0, last = 0;
2371
2372 n = rb_first(root);
2373 if (n) {
2374 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2375 first = entry->bytenr;
2376 }
2377 n = rb_last(root);
2378 if (n) {
2379 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2380 last = entry->bytenr;
2381 }
2382 n = root->rb_node;
2383
2384 while (n) {
2385 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2386 WARN_ON(!entry->in_tree);
2387
2388 middle = entry->bytenr;
2389
2390 if (alt)
2391 n = n->rb_left;
2392 else
2393 n = n->rb_right;
2394
2395 alt = 1 - alt;
2396 }
2397 return middle;
2398}
2399#endif
2400
Arne Jansenbed92ea2012-06-28 18:03:02 +02002401int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2402 struct btrfs_fs_info *fs_info)
2403{
2404 struct qgroup_update *qgroup_update;
2405 int ret = 0;
2406
2407 if (list_empty(&trans->qgroup_ref_list) !=
2408 !trans->delayed_ref_elem.seq) {
2409 /* list without seq or seq without list */
2410 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2411 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2412 trans->delayed_ref_elem.seq);
2413 BUG();
2414 }
2415
2416 if (!trans->delayed_ref_elem.seq)
2417 return 0;
2418
2419 while (!list_empty(&trans->qgroup_ref_list)) {
2420 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2421 struct qgroup_update, list);
2422 list_del(&qgroup_update->list);
2423 if (!ret)
2424 ret = btrfs_qgroup_account_ref(
2425 trans, fs_info, qgroup_update->node,
2426 qgroup_update->extent_op);
2427 kfree(qgroup_update);
2428 }
2429
2430 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2431
2432 return ret;
2433}
2434
Chris Masonc3e69d52009-03-13 10:17:05 -04002435/*
2436 * this starts processing the delayed reference count updates and
2437 * extent insertions we have queued up so far. count can be
2438 * 0, which means to process everything in the tree at the start
2439 * of the run (but not newly added entries), or it can be some target
2440 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002441 *
2442 * Returns 0 on success or if called with an aborted transaction
2443 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002444 */
2445int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2446 struct btrfs_root *root, unsigned long count)
2447{
2448 struct rb_node *node;
2449 struct btrfs_delayed_ref_root *delayed_refs;
2450 struct btrfs_delayed_ref_node *ref;
2451 struct list_head cluster;
2452 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002453 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002454 int run_all = count == (unsigned long)-1;
2455 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002456 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002457
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002458 /* We'll clean this up in btrfs_cleanup_transaction */
2459 if (trans->aborted)
2460 return 0;
2461
Chris Masonc3e69d52009-03-13 10:17:05 -04002462 if (root == root->fs_info->extent_root)
2463 root = root->fs_info->tree_root;
2464
Jan Schmidtedf39272012-06-28 18:04:55 +02002465 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2466
Chris Masonc3e69d52009-03-13 10:17:05 -04002467 delayed_refs = &trans->transaction->delayed_refs;
2468 INIT_LIST_HEAD(&cluster);
2469again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002470 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002471 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002472
Arne Jansen709c0482011-09-12 12:22:57 +02002473#ifdef SCRAMBLE_DELAYED_REFS
2474 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2475#endif
2476
Chris Masonc3e69d52009-03-13 10:17:05 -04002477 if (count == 0) {
2478 count = delayed_refs->num_entries * 2;
2479 run_most = 1;
2480 }
2481 while (1) {
2482 if (!(run_all || run_most) &&
2483 delayed_refs->num_heads_ready < 64)
2484 break;
2485
2486 /*
2487 * go find something we can process in the rbtree. We start at
2488 * the beginning of the tree, and then build a cluster
2489 * of refs to process starting at the first one we are able to
2490 * lock
2491 */
Jan Schmidta1686502011-12-12 16:10:07 +01002492 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002493 ret = btrfs_find_ref_cluster(trans, &cluster,
2494 delayed_refs->run_delayed_start);
2495 if (ret)
2496 break;
2497
2498 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002499 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002500 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002501 spin_unlock(&delayed_refs->lock);
2502 btrfs_abort_transaction(trans, root, ret);
2503 return ret;
2504 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002505
2506 count -= min_t(unsigned long, ret, count);
2507
2508 if (count == 0)
2509 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002510
Arne Jansen1fa11e22012-08-06 14:18:51 -06002511 if (delayed_start >= delayed_refs->run_delayed_start) {
2512 if (loops == 0) {
2513 /*
2514 * btrfs_find_ref_cluster looped. let's do one
2515 * more cycle. if we don't run any delayed ref
2516 * during that cycle (because we can't because
2517 * all of them are blocked), bail out.
2518 */
2519 loops = 1;
2520 } else {
2521 /*
2522 * no runnable refs left, stop trying
2523 */
2524 BUG_ON(run_all);
2525 break;
2526 }
2527 }
2528 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002529 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002530 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002531 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002532 }
2533
Chris Mason56bec292009-03-13 10:10:06 -04002534 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002535 if (!list_empty(&trans->new_bgs)) {
2536 spin_unlock(&delayed_refs->lock);
2537 btrfs_create_pending_block_groups(trans, root);
2538 spin_lock(&delayed_refs->lock);
2539 }
2540
Chris Mason56bec292009-03-13 10:10:06 -04002541 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002542 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002543 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002544 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002545
2546 while (node) {
2547 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2548 rb_node);
2549 if (btrfs_delayed_ref_is_head(ref)) {
2550 struct btrfs_delayed_ref_head *head;
2551
2552 head = btrfs_delayed_node_to_head(ref);
2553 atomic_inc(&ref->refs);
2554
2555 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002556 /*
2557 * Mutex was contended, block until it's
2558 * released and try again
2559 */
Chris Mason56bec292009-03-13 10:10:06 -04002560 mutex_lock(&head->mutex);
2561 mutex_unlock(&head->mutex);
2562
2563 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002564 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002565 goto again;
2566 }
2567 node = rb_next(node);
2568 }
2569 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002570 schedule_timeout(1);
2571 goto again;
2572 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002573out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002574 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002575 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002576 return 0;
2577}
2578
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002579int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2580 struct btrfs_root *root,
2581 u64 bytenr, u64 num_bytes, u64 flags,
2582 int is_data)
2583{
2584 struct btrfs_delayed_extent_op *extent_op;
2585 int ret;
2586
Miao Xie78a61842012-11-21 02:21:28 +00002587 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002588 if (!extent_op)
2589 return -ENOMEM;
2590
2591 extent_op->flags_to_set = flags;
2592 extent_op->update_flags = 1;
2593 extent_op->update_key = 0;
2594 extent_op->is_data = is_data ? 1 : 0;
2595
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002596 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2597 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002598 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002599 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002600 return ret;
2601}
2602
2603static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2604 struct btrfs_root *root,
2605 struct btrfs_path *path,
2606 u64 objectid, u64 offset, u64 bytenr)
2607{
2608 struct btrfs_delayed_ref_head *head;
2609 struct btrfs_delayed_ref_node *ref;
2610 struct btrfs_delayed_data_ref *data_ref;
2611 struct btrfs_delayed_ref_root *delayed_refs;
2612 struct rb_node *node;
2613 int ret = 0;
2614
2615 ret = -ENOENT;
2616 delayed_refs = &trans->transaction->delayed_refs;
2617 spin_lock(&delayed_refs->lock);
2618 head = btrfs_find_delayed_ref_head(trans, bytenr);
2619 if (!head)
2620 goto out;
2621
2622 if (!mutex_trylock(&head->mutex)) {
2623 atomic_inc(&head->node.refs);
2624 spin_unlock(&delayed_refs->lock);
2625
David Sterbab3b4aa72011-04-21 01:20:15 +02002626 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002627
David Sterba8cc33e52011-05-02 15:29:25 +02002628 /*
2629 * Mutex was contended, block until it's released and let
2630 * caller try again
2631 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002632 mutex_lock(&head->mutex);
2633 mutex_unlock(&head->mutex);
2634 btrfs_put_delayed_ref(&head->node);
2635 return -EAGAIN;
2636 }
2637
2638 node = rb_prev(&head->node.rb_node);
2639 if (!node)
2640 goto out_unlock;
2641
2642 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2643
2644 if (ref->bytenr != bytenr)
2645 goto out_unlock;
2646
2647 ret = 1;
2648 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2649 goto out_unlock;
2650
2651 data_ref = btrfs_delayed_node_to_data_ref(ref);
2652
2653 node = rb_prev(node);
2654 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002655 int seq = ref->seq;
2656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002657 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002658 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002659 goto out_unlock;
2660 }
2661
2662 if (data_ref->root != root->root_key.objectid ||
2663 data_ref->objectid != objectid || data_ref->offset != offset)
2664 goto out_unlock;
2665
2666 ret = 0;
2667out_unlock:
2668 mutex_unlock(&head->mutex);
2669out:
2670 spin_unlock(&delayed_refs->lock);
2671 return ret;
2672}
2673
2674static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2675 struct btrfs_root *root,
2676 struct btrfs_path *path,
2677 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002678{
2679 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002680 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002681 struct btrfs_extent_data_ref *ref;
2682 struct btrfs_extent_inline_ref *iref;
2683 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002684 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002685 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002686 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002687
Chris Masonbe20aa92007-12-17 20:14:01 -05002688 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002689 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002690 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002691
Chris Masonbe20aa92007-12-17 20:14:01 -05002692 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2693 if (ret < 0)
2694 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002695 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002696
2697 ret = -ENOENT;
2698 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002699 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002700
Zheng Yan31840ae2008-09-23 13:14:14 -04002701 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002702 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002703 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002704
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002705 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002706 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002707
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002708 ret = 1;
2709 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2710#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2711 if (item_size < sizeof(*ei)) {
2712 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2713 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002714 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002715#endif
2716 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2717
2718 if (item_size != sizeof(*ei) +
2719 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2720 goto out;
2721
2722 if (btrfs_extent_generation(leaf, ei) <=
2723 btrfs_root_last_snapshot(&root->root_item))
2724 goto out;
2725
2726 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2727 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2728 BTRFS_EXTENT_DATA_REF_KEY)
2729 goto out;
2730
2731 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2732 if (btrfs_extent_refs(leaf, ei) !=
2733 btrfs_extent_data_ref_count(leaf, ref) ||
2734 btrfs_extent_data_ref_root(leaf, ref) !=
2735 root->root_key.objectid ||
2736 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2737 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2738 goto out;
2739
Yan Zhengf321e492008-07-30 09:26:11 -04002740 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002741out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002742 return ret;
2743}
2744
2745int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2746 struct btrfs_root *root,
2747 u64 objectid, u64 offset, u64 bytenr)
2748{
2749 struct btrfs_path *path;
2750 int ret;
2751 int ret2;
2752
2753 path = btrfs_alloc_path();
2754 if (!path)
2755 return -ENOENT;
2756
2757 do {
2758 ret = check_committed_ref(trans, root, path, objectid,
2759 offset, bytenr);
2760 if (ret && ret != -ENOENT)
2761 goto out;
2762
2763 ret2 = check_delayed_ref(trans, root, path, objectid,
2764 offset, bytenr);
2765 } while (ret2 == -EAGAIN);
2766
2767 if (ret2 && ret2 != -ENOENT) {
2768 ret = ret2;
2769 goto out;
2770 }
2771
2772 if (ret != -ENOENT || ret2 != -ENOENT)
2773 ret = 0;
2774out:
Yan Zhengf321e492008-07-30 09:26:11 -04002775 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002776 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2777 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002778 return ret;
2779}
2780
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002781static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002782 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002783 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002784 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002785{
2786 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002787 u64 num_bytes;
2788 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002789 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002790 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002791 struct btrfs_key key;
2792 struct btrfs_file_extent_item *fi;
2793 int i;
2794 int level;
2795 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002796 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002797 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002798
2799 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002800 nritems = btrfs_header_nritems(buf);
2801 level = btrfs_header_level(buf);
2802
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002803 if (!root->ref_cows && level == 0)
2804 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002805
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002806 if (inc)
2807 process_func = btrfs_inc_extent_ref;
2808 else
2809 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002810
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002811 if (full_backref)
2812 parent = buf->start;
2813 else
2814 parent = 0;
2815
Zheng Yan31840ae2008-09-23 13:14:14 -04002816 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002817 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002818 btrfs_item_key_to_cpu(buf, &key, i);
2819 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002820 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002821 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002822 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002823 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002824 BTRFS_FILE_EXTENT_INLINE)
2825 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002826 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2827 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002828 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002829
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002830 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2831 key.offset -= btrfs_file_extent_offset(buf, fi);
2832 ret = process_func(trans, root, bytenr, num_bytes,
2833 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002834 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002835 if (ret)
2836 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002837 } else {
2838 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002839 num_bytes = btrfs_level_size(root, level - 1);
2840 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002841 parent, ref_root, level - 1, 0,
2842 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002843 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002844 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002845 }
2846 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002847 return 0;
2848fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002849 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002850}
2851
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002852int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002853 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002854{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002855 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002856}
Zheng Yan31840ae2008-09-23 13:14:14 -04002857
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002858int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002859 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002860{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002861 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002862}
2863
Chris Mason9078a3e2007-04-26 16:46:15 -04002864static int write_one_cache_group(struct btrfs_trans_handle *trans,
2865 struct btrfs_root *root,
2866 struct btrfs_path *path,
2867 struct btrfs_block_group_cache *cache)
2868{
2869 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002870 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002871 unsigned long bi;
2872 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002873
Chris Mason9078a3e2007-04-26 16:46:15 -04002874 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002875 if (ret < 0)
2876 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002877 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04002878
2879 leaf = path->nodes[0];
2880 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2881 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2882 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002883 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002884fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002885 if (ret) {
2886 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04002887 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002888 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002889 return 0;
2890
2891}
2892
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002893static struct btrfs_block_group_cache *
2894next_block_group(struct btrfs_root *root,
2895 struct btrfs_block_group_cache *cache)
2896{
2897 struct rb_node *node;
2898 spin_lock(&root->fs_info->block_group_cache_lock);
2899 node = rb_next(&cache->cache_node);
2900 btrfs_put_block_group(cache);
2901 if (node) {
2902 cache = rb_entry(node, struct btrfs_block_group_cache,
2903 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002904 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002905 } else
2906 cache = NULL;
2907 spin_unlock(&root->fs_info->block_group_cache_lock);
2908 return cache;
2909}
2910
Josef Bacik0af3d002010-06-21 14:48:16 -04002911static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2912 struct btrfs_trans_handle *trans,
2913 struct btrfs_path *path)
2914{
2915 struct btrfs_root *root = block_group->fs_info->tree_root;
2916 struct inode *inode = NULL;
2917 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002918 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002919 int num_pages = 0;
2920 int retries = 0;
2921 int ret = 0;
2922
2923 /*
2924 * If this block group is smaller than 100 megs don't bother caching the
2925 * block group.
2926 */
2927 if (block_group->key.offset < (100 * 1024 * 1024)) {
2928 spin_lock(&block_group->lock);
2929 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2930 spin_unlock(&block_group->lock);
2931 return 0;
2932 }
2933
2934again:
2935 inode = lookup_free_space_inode(root, block_group, path);
2936 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2937 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002938 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002939 goto out;
2940 }
2941
2942 if (IS_ERR(inode)) {
2943 BUG_ON(retries);
2944 retries++;
2945
2946 if (block_group->ro)
2947 goto out_free;
2948
2949 ret = create_free_space_inode(root, trans, block_group, path);
2950 if (ret)
2951 goto out_free;
2952 goto again;
2953 }
2954
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002955 /* We've already setup this transaction, go ahead and exit */
2956 if (block_group->cache_generation == trans->transid &&
2957 i_size_read(inode)) {
2958 dcs = BTRFS_DC_SETUP;
2959 goto out_put;
2960 }
2961
Josef Bacik0af3d002010-06-21 14:48:16 -04002962 /*
2963 * We want to set the generation to 0, that way if anything goes wrong
2964 * from here on out we know not to trust this cache when we load up next
2965 * time.
2966 */
2967 BTRFS_I(inode)->generation = 0;
2968 ret = btrfs_update_inode(trans, root, inode);
2969 WARN_ON(ret);
2970
2971 if (i_size_read(inode) > 0) {
2972 ret = btrfs_truncate_free_space_cache(root, trans, path,
2973 inode);
2974 if (ret)
2975 goto out_put;
2976 }
2977
2978 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06002979 if (block_group->cached != BTRFS_CACHE_FINISHED ||
2980 !btrfs_test_opt(root, SPACE_CACHE)) {
2981 /*
2982 * don't bother trying to write stuff out _if_
2983 * a) we're not cached,
2984 * b) we're with nospace_cache mount option.
2985 */
Josef Bacik2b209822010-12-03 13:17:53 -05002986 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002987 spin_unlock(&block_group->lock);
2988 goto out_put;
2989 }
2990 spin_unlock(&block_group->lock);
2991
Josef Bacik6fc823b2012-08-06 13:46:38 -06002992 /*
2993 * Try to preallocate enough space based on how big the block group is.
2994 * Keep in mind this has to include any pinned space which could end up
2995 * taking up quite a bit since it's not folded into the other space
2996 * cache.
2997 */
2998 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04002999 if (!num_pages)
3000 num_pages = 1;
3001
Josef Bacik0af3d002010-06-21 14:48:16 -04003002 num_pages *= 16;
3003 num_pages *= PAGE_CACHE_SIZE;
3004
3005 ret = btrfs_check_data_free_space(inode, num_pages);
3006 if (ret)
3007 goto out_put;
3008
3009 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3010 num_pages, num_pages,
3011 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003012 if (!ret)
3013 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003014 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003015
Josef Bacik0af3d002010-06-21 14:48:16 -04003016out_put:
3017 iput(inode);
3018out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003019 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003020out:
3021 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003022 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003023 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003024 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003025 spin_unlock(&block_group->lock);
3026
3027 return ret;
3028}
3029
Chris Mason96b51792007-10-15 16:15:19 -04003030int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3031 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003032{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003033 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003034 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003035 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003036 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003037
3038 path = btrfs_alloc_path();
3039 if (!path)
3040 return -ENOMEM;
3041
Josef Bacik0af3d002010-06-21 14:48:16 -04003042again:
3043 while (1) {
3044 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3045 while (cache) {
3046 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3047 break;
3048 cache = next_block_group(root, cache);
3049 }
3050 if (!cache) {
3051 if (last == 0)
3052 break;
3053 last = 0;
3054 continue;
3055 }
3056 err = cache_save_setup(cache, trans, path);
3057 last = cache->key.objectid + cache->key.offset;
3058 btrfs_put_block_group(cache);
3059 }
3060
Chris Masond3977122009-01-05 21:25:51 -05003061 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003062 if (last == 0) {
3063 err = btrfs_run_delayed_refs(trans, root,
3064 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003065 if (err) /* File system offline */
3066 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003067 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003068
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003069 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3070 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003071 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3072 btrfs_put_block_group(cache);
3073 goto again;
3074 }
3075
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003076 if (cache->dirty)
3077 break;
3078 cache = next_block_group(root, cache);
3079 }
3080 if (!cache) {
3081 if (last == 0)
3082 break;
3083 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003084 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003085 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003086
Josef Bacik0cb59c92010-07-02 12:14:14 -04003087 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3088 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003089 cache->dirty = 0;
3090 last = cache->key.objectid + cache->key.offset;
3091
3092 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003093 if (err) /* File system offline */
3094 goto out;
3095
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003096 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003097 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003098
Josef Bacik0cb59c92010-07-02 12:14:14 -04003099 while (1) {
3100 /*
3101 * I don't think this is needed since we're just marking our
3102 * preallocated extent as written, but just in case it can't
3103 * hurt.
3104 */
3105 if (last == 0) {
3106 err = btrfs_run_delayed_refs(trans, root,
3107 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003108 if (err) /* File system offline */
3109 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003110 }
3111
3112 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3113 while (cache) {
3114 /*
3115 * Really this shouldn't happen, but it could if we
3116 * couldn't write the entire preallocated extent and
3117 * splitting the extent resulted in a new block.
3118 */
3119 if (cache->dirty) {
3120 btrfs_put_block_group(cache);
3121 goto again;
3122 }
3123 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3124 break;
3125 cache = next_block_group(root, cache);
3126 }
3127 if (!cache) {
3128 if (last == 0)
3129 break;
3130 last = 0;
3131 continue;
3132 }
3133
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003134 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003135
3136 /*
3137 * If we didn't have an error then the cache state is still
3138 * NEED_WRITE, so we can set it to WRITTEN.
3139 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003140 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003141 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3142 last = cache->key.objectid + cache->key.offset;
3143 btrfs_put_block_group(cache);
3144 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003145out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003146
Chris Mason9078a3e2007-04-26 16:46:15 -04003147 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003148 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003149}
3150
Yan Zhengd2fb3432008-12-11 16:30:39 -05003151int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3152{
3153 struct btrfs_block_group_cache *block_group;
3154 int readonly = 0;
3155
3156 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3157 if (!block_group || block_group->ro)
3158 readonly = 1;
3159 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003160 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003161 return readonly;
3162}
3163
Chris Mason593060d2008-03-25 16:50:33 -04003164static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3165 u64 total_bytes, u64 bytes_used,
3166 struct btrfs_space_info **space_info)
3167{
3168 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003169 int i;
3170 int factor;
3171
3172 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3173 BTRFS_BLOCK_GROUP_RAID10))
3174 factor = 2;
3175 else
3176 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003177
3178 found = __find_space_info(info, flags);
3179 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003180 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003181 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003182 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003183 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003184 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003185 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003186 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003187 *space_info = found;
3188 return 0;
3189 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003190 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003191 if (!found)
3192 return -ENOMEM;
3193
Yan, Zhengb742bb82010-05-16 10:46:24 -04003194 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3195 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003196 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003197 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003198 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003199 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003200 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003201 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003202 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003203 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003204 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003205 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003206 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003207 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003208 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003209 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003210 found->flush = 0;
3211 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003212 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003213 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003214 if (flags & BTRFS_BLOCK_GROUP_DATA)
3215 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003216 return 0;
3217}
3218
Chris Mason8790d502008-04-03 16:29:03 -04003219static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3220{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003221 u64 extra_flags = chunk_to_extended(flags) &
3222 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003223
3224 if (flags & BTRFS_BLOCK_GROUP_DATA)
3225 fs_info->avail_data_alloc_bits |= extra_flags;
3226 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3227 fs_info->avail_metadata_alloc_bits |= extra_flags;
3228 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3229 fs_info->avail_system_alloc_bits |= extra_flags;
Chris Mason8790d502008-04-03 16:29:03 -04003230}
Chris Mason593060d2008-03-25 16:50:33 -04003231
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003232/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003233 * returns target flags in extended format or 0 if restripe for this
3234 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003235 *
3236 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003237 */
3238static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3239{
3240 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3241 u64 target = 0;
3242
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003243 if (!bctl)
3244 return 0;
3245
3246 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3247 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3248 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3249 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3250 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3251 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3252 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3253 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3254 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3255 }
3256
3257 return target;
3258}
3259
3260/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003261 * @flags: available profiles in extended format (see ctree.h)
3262 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003263 * Returns reduced profile in chunk format. If profile changing is in
3264 * progress (either running or paused) picks the target profile (if it's
3265 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003266 */
Yan Zheng2b820322008-11-17 21:11:30 -05003267u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003268{
Chris Masoncd02dca2010-12-13 14:56:23 -05003269 /*
3270 * we add in the count of missing devices because we want
3271 * to make sure that any RAID levels on a degraded FS
3272 * continue to be honored.
3273 */
3274 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3275 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003276 u64 target;
Chris Masona061fc82008-05-07 11:43:44 -04003277
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003278 /*
3279 * see if restripe for this chunk_type is in progress, if so
3280 * try to reduce to the target profile
3281 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003282 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003283 target = get_restripe_target(root->fs_info, flags);
3284 if (target) {
3285 /* pick target profile only if it's already available */
3286 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003287 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003288 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003289 }
3290 }
3291 spin_unlock(&root->fs_info->balance_lock);
3292
Chris Masona061fc82008-05-07 11:43:44 -04003293 if (num_devices == 1)
3294 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3295 if (num_devices < 4)
3296 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3297
Chris Masonec44a352008-04-28 15:29:52 -04003298 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3299 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003300 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003301 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003302 }
Chris Masonec44a352008-04-28 15:29:52 -04003303
3304 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003305 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003306 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003307 }
Chris Masonec44a352008-04-28 15:29:52 -04003308
3309 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3310 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3311 (flags & BTRFS_BLOCK_GROUP_RAID10) |
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003312 (flags & BTRFS_BLOCK_GROUP_DUP))) {
Chris Masonec44a352008-04-28 15:29:52 -04003313 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003314 }
3315
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003316 return extended_to_chunk(flags);
Chris Masonec44a352008-04-28 15:29:52 -04003317}
3318
Yan, Zhengb742bb82010-05-16 10:46:24 -04003319static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003320{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003321 if (flags & BTRFS_BLOCK_GROUP_DATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003322 flags |= root->fs_info->avail_data_alloc_bits;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003323 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003324 flags |= root->fs_info->avail_system_alloc_bits;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003325 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003326 flags |= root->fs_info->avail_metadata_alloc_bits;
3327
Yan, Zhengb742bb82010-05-16 10:46:24 -04003328 return btrfs_reduce_alloc_profile(root, flags);
3329}
Josef Bacik6a632092009-02-20 11:00:09 -05003330
Miao Xie6d07bce2011-01-05 10:07:31 +00003331u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003332{
3333 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003334
Yan, Zhengb742bb82010-05-16 10:46:24 -04003335 if (data)
3336 flags = BTRFS_BLOCK_GROUP_DATA;
3337 else if (root == root->fs_info->chunk_root)
3338 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3339 else
3340 flags = BTRFS_BLOCK_GROUP_METADATA;
3341
3342 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003343}
3344
Josef Bacik6a632092009-02-20 11:00:09 -05003345/*
3346 * This will check the space that the inode allocates from to make sure we have
3347 * enough space for bytes.
3348 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003349int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003350{
3351 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003352 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003353 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003354 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003355 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003356
3357 /* make sure bytes are sectorsize aligned */
3358 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3359
Li Zefan82d59022011-04-20 10:33:24 +08003360 if (root == root->fs_info->tree_root ||
3361 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003362 alloc_chunk = 0;
3363 committed = 1;
3364 }
3365
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003366 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003367 if (!data_sinfo)
3368 goto alloc;
3369
Josef Bacik6a632092009-02-20 11:00:09 -05003370again:
3371 /* make sure we have enough space to handle the data first */
3372 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003373 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3374 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3375 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003376
3377 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003378 struct btrfs_trans_handle *trans;
3379
Josef Bacik6a632092009-02-20 11:00:09 -05003380 /*
3381 * if we don't have enough free bytes in this space then we need
3382 * to alloc a new chunk.
3383 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003384 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003385 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003386
Chris Mason0e4f8f82011-04-15 16:05:44 -04003387 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003388 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003389alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003390 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003391 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003392 if (IS_ERR(trans))
3393 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003394
3395 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003396 alloc_target,
3397 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003398 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003399 if (ret < 0) {
3400 if (ret != -ENOSPC)
3401 return ret;
3402 else
3403 goto commit_trans;
3404 }
Chris Mason33b4d472009-09-22 14:45:50 -04003405
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003406 if (!data_sinfo)
3407 data_sinfo = fs_info->data_sinfo;
3408
Josef Bacik6a632092009-02-20 11:00:09 -05003409 goto again;
3410 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003411
3412 /*
3413 * If we have less pinned bytes than we want to allocate then
3414 * don't bother committing the transaction, it won't help us.
3415 */
3416 if (data_sinfo->bytes_pinned < bytes)
3417 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003418 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003419
3420 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003421commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003422 if (!committed &&
3423 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003424 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003425 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003426 if (IS_ERR(trans))
3427 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003428 ret = btrfs_commit_transaction(trans, root);
3429 if (ret)
3430 return ret;
3431 goto again;
3432 }
3433
Josef Bacik6a632092009-02-20 11:00:09 -05003434 return -ENOSPC;
3435 }
3436 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003437 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003438 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003439 spin_unlock(&data_sinfo->lock);
3440
Josef Bacik9ed74f22009-09-11 16:12:44 -04003441 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003442}
3443
3444/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003445 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003446 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003447void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003448{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003449 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003450 struct btrfs_space_info *data_sinfo;
3451
3452 /* make sure bytes are sectorsize aligned */
3453 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3454
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003455 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003456 spin_lock(&data_sinfo->lock);
3457 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003458 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003459 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003460 spin_unlock(&data_sinfo->lock);
3461}
3462
Josef Bacik97e728d2009-04-21 17:40:57 -04003463static void force_metadata_allocation(struct btrfs_fs_info *info)
3464{
3465 struct list_head *head = &info->space_info;
3466 struct btrfs_space_info *found;
3467
3468 rcu_read_lock();
3469 list_for_each_entry_rcu(found, head, list) {
3470 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003471 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003472 }
3473 rcu_read_unlock();
3474}
3475
Chris Masone5bc2452010-10-26 13:37:56 -04003476static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003477 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003478{
Josef Bacikfb25e912011-07-26 17:00:46 -04003479 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003480 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003481 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003482 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003483
Chris Mason0e4f8f82011-04-15 16:05:44 -04003484 if (force == CHUNK_ALLOC_FORCE)
3485 return 1;
3486
3487 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003488 * We need to take into account the global rsv because for all intents
3489 * and purposes it's used space. Don't worry about locking the
3490 * global_rsv, it doesn't change except when the transaction commits.
3491 */
Josef Bacik54338b52012-08-14 16:20:52 -04003492 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3493 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003494
3495 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003496 * in limited mode, we want to have some free space up to
3497 * about 1% of the FS size.
3498 */
3499 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003500 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003501 thresh = max_t(u64, 64 * 1024 * 1024,
3502 div_factor_fine(thresh, 1));
3503
3504 if (num_bytes - num_allocated < thresh)
3505 return 1;
3506 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003507
Josef Bacik698d0082012-09-12 14:08:47 -04003508 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003509 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003510 return 1;
3511}
3512
Liu Bo15d1ff82012-03-29 09:57:44 -04003513static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3514{
3515 u64 num_dev;
3516
3517 if (type & BTRFS_BLOCK_GROUP_RAID10 ||
3518 type & BTRFS_BLOCK_GROUP_RAID0)
3519 num_dev = root->fs_info->fs_devices->rw_devices;
3520 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3521 num_dev = 2;
3522 else
3523 num_dev = 1; /* DUP or single */
3524
3525 /* metadata for updaing devices and chunk tree */
3526 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3527}
3528
3529static void check_system_chunk(struct btrfs_trans_handle *trans,
3530 struct btrfs_root *root, u64 type)
3531{
3532 struct btrfs_space_info *info;
3533 u64 left;
3534 u64 thresh;
3535
3536 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3537 spin_lock(&info->lock);
3538 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3539 info->bytes_reserved - info->bytes_readonly;
3540 spin_unlock(&info->lock);
3541
3542 thresh = get_system_chunk_thresh(root, type);
3543 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3544 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3545 left, thresh, type);
3546 dump_space_info(info, 0, 0);
3547 }
3548
3549 if (left < thresh) {
3550 u64 flags;
3551
3552 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3553 btrfs_alloc_chunk(trans, root, flags);
3554 }
3555}
3556
Chris Mason6324fbf2008-03-24 15:01:59 -04003557static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003558 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003559{
3560 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003561 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003562 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003563 int ret = 0;
3564
Josef Bacikc6b305a2012-12-18 09:16:16 -05003565 /* Don't re-enter if we're already allocating a chunk */
3566 if (trans->allocating_chunk)
3567 return -ENOSPC;
3568
Chris Mason6324fbf2008-03-24 15:01:59 -04003569 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003570 if (!space_info) {
3571 ret = update_space_info(extent_root->fs_info, flags,
3572 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003573 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003574 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003575 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003576
Josef Bacik6d741192011-04-11 20:20:11 -04003577again:
Josef Bacik25179202008-10-29 14:49:05 -04003578 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003579 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003580 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003581 if (space_info->full) {
3582 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003583 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003584 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003585
Josef Bacik698d0082012-09-12 14:08:47 -04003586 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003587 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003588 return 0;
3589 } else if (space_info->chunk_alloc) {
3590 wait_for_alloc = 1;
3591 } else {
3592 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003593 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003594
Josef Bacik25179202008-10-29 14:49:05 -04003595 spin_unlock(&space_info->lock);
3596
Josef Bacik6d741192011-04-11 20:20:11 -04003597 mutex_lock(&fs_info->chunk_mutex);
3598
3599 /*
3600 * The chunk_mutex is held throughout the entirety of a chunk
3601 * allocation, so once we've acquired the chunk_mutex we know that the
3602 * other guy is done and we need to recheck and see if we should
3603 * allocate.
3604 */
3605 if (wait_for_alloc) {
3606 mutex_unlock(&fs_info->chunk_mutex);
3607 wait_for_alloc = 0;
3608 goto again;
3609 }
3610
Josef Bacikc6b305a2012-12-18 09:16:16 -05003611 trans->allocating_chunk = true;
3612
Josef Bacik97e728d2009-04-21 17:40:57 -04003613 /*
Josef Bacik67377732010-09-16 16:19:09 -04003614 * If we have mixed data/metadata chunks we want to make sure we keep
3615 * allocating mixed chunks instead of individual chunks.
3616 */
3617 if (btrfs_mixed_space_info(space_info))
3618 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3619
3620 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003621 * if we're doing a data chunk, go ahead and make sure that
3622 * we keep a reasonable number of metadata chunks allocated in the
3623 * FS as well.
3624 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003625 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003626 fs_info->data_chunk_allocations++;
3627 if (!(fs_info->data_chunk_allocations %
3628 fs_info->metadata_ratio))
3629 force_metadata_allocation(fs_info);
3630 }
3631
Liu Bo15d1ff82012-03-29 09:57:44 -04003632 /*
3633 * Check if we have enough space in SYSTEM chunk because we may need
3634 * to update devices.
3635 */
3636 check_system_chunk(trans, extent_root, flags);
3637
Yan Zheng2b820322008-11-17 21:11:30 -05003638 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003639 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003640 if (ret < 0 && ret != -ENOSPC)
3641 goto out;
3642
Josef Bacik9ed74f22009-09-11 16:12:44 -04003643 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003644 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003645 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003646 else
3647 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003648
Chris Mason0e4f8f82011-04-15 16:05:44 -04003649 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003650 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003651 spin_unlock(&space_info->lock);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003652out:
Dan Carpentera25c75d2012-04-18 09:59:29 +03003653 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003654 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003655}
3656
Josef Bacika80c8dc2012-09-06 16:59:33 -04003657static int can_overcommit(struct btrfs_root *root,
3658 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003659 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003660{
3661 u64 profile = btrfs_get_alloc_profile(root, 0);
3662 u64 avail;
3663 u64 used;
3664
3665 used = space_info->bytes_used + space_info->bytes_reserved +
3666 space_info->bytes_pinned + space_info->bytes_readonly +
3667 space_info->bytes_may_use;
3668
3669 spin_lock(&root->fs_info->free_chunk_lock);
3670 avail = root->fs_info->free_chunk_space;
3671 spin_unlock(&root->fs_info->free_chunk_lock);
3672
3673 /*
3674 * If we have dup, raid1 or raid10 then only half of the free
3675 * space is actually useable.
3676 */
3677 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3678 BTRFS_BLOCK_GROUP_RAID1 |
3679 BTRFS_BLOCK_GROUP_RAID10))
3680 avail >>= 1;
3681
3682 /*
Miao Xie561c2942012-10-16 11:32:18 +00003683 * If we aren't flushing all things, let us overcommit up to
3684 * 1/2th of the space. If we can flush, don't let us overcommit
3685 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003686 */
Miao Xie08e007d2012-10-16 11:33:38 +00003687 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003688 avail >>= 3;
3689 else
3690 avail >>= 1;
3691
3692 if (used + bytes < space_info->total_bytes + avail)
3693 return 1;
3694 return 0;
3695}
3696
Miao Xieda633a42012-12-20 11:19:09 +00003697static inline int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
3698 unsigned long nr_pages,
3699 enum wb_reason reason)
Josef Bacik1135d6d2012-12-14 13:46:43 -05003700{
Miao Xieda633a42012-12-20 11:19:09 +00003701 /* the flusher is dealing with the dirty inodes now. */
3702 if (writeback_in_progress(sb->s_bdi))
3703 return 1;
3704
3705 if (down_read_trylock(&sb->s_umount)) {
Josef Bacik1135d6d2012-12-14 13:46:43 -05003706 writeback_inodes_sb_nr(sb, nr_pages, reason);
3707 up_read(&sb->s_umount);
3708 return 1;
3709 }
3710
3711 return 0;
3712}
3713
Miao Xieda633a42012-12-20 11:19:09 +00003714void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3715 unsigned long nr_pages)
3716{
3717 struct super_block *sb = root->fs_info->sb;
3718 int started;
3719
3720 /* If we can not start writeback, just sync all the delalloc file. */
3721 started = writeback_inodes_sb_nr_if_idle_safe(sb, nr_pages,
3722 WB_REASON_FS_FREE_SPACE);
3723 if (!started) {
3724 /*
3725 * We needn't worry the filesystem going from r/w to r/o though
3726 * we don't acquire ->s_umount mutex, because the filesystem
3727 * should guarantee the delalloc inodes list be empty after
3728 * the filesystem is readonly(all dirty pages are written to
3729 * the disk).
3730 */
3731 btrfs_start_delalloc_inodes(root, 0);
3732 btrfs_wait_ordered_extents(root, 0);
3733 }
3734}
3735
Yan, Zheng5da9d012010-05-16 10:46:25 -04003736/*
3737 * shrink metadata reservation for delalloc
3738 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003739static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3740 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003741{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003742 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003743 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003744 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003745 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003746 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003747 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003748 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003749 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003750 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003751
Josef Bacik663350a2011-11-03 22:54:25 -04003752 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003753 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003754 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003755
3756 smp_mb();
Josef Bacikf4c738c2012-07-02 17:10:51 -04003757 delalloc_bytes = root->fs_info->delalloc_bytes;
3758 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003759 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003760 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003761 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003762 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003763 }
3764
Josef Bacikf4c738c2012-07-02 17:10:51 -04003765 while (delalloc_bytes && loops < 3) {
3766 max_reclaim = min(delalloc_bytes, to_reclaim);
3767 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003768 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003769 /*
3770 * We need to wait for the async pages to actually start before
3771 * we do anything.
3772 */
3773 wait_event(root->fs_info->async_submit_wait,
3774 !atomic_read(&root->fs_info->async_delalloc_pages));
3775
Miao Xie08e007d2012-10-16 11:33:38 +00003776 if (!trans)
3777 flush = BTRFS_RESERVE_FLUSH_ALL;
3778 else
3779 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003780 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003781 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003782 spin_unlock(&space_info->lock);
3783 break;
3784 }
Josef Bacik0019f102010-10-15 15:18:40 -04003785 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003786
Chris Mason36e39c42011-03-12 07:08:42 -05003787 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003788 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003789 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003790 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003791 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003792 if (time_left)
3793 break;
3794 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003795 smp_mb();
3796 delalloc_bytes = root->fs_info->delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003797 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003798}
3799
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003800/**
Josef Bacik663350a2011-11-03 22:54:25 -04003801 * maybe_commit_transaction - possibly commit the transaction if its ok to
3802 * @root - the root we're allocating for
3803 * @bytes - the number of bytes we want to reserve
3804 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003805 *
Josef Bacik663350a2011-11-03 22:54:25 -04003806 * This will check to make sure that committing the transaction will actually
3807 * get us somewhere and then commit the transaction if it does. Otherwise it
3808 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003809 */
Josef Bacik663350a2011-11-03 22:54:25 -04003810static int may_commit_transaction(struct btrfs_root *root,
3811 struct btrfs_space_info *space_info,
3812 u64 bytes, int force)
3813{
3814 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3815 struct btrfs_trans_handle *trans;
3816
3817 trans = (struct btrfs_trans_handle *)current->journal_info;
3818 if (trans)
3819 return -EAGAIN;
3820
3821 if (force)
3822 goto commit;
3823
3824 /* See if there is enough pinned space to make this reservation */
3825 spin_lock(&space_info->lock);
3826 if (space_info->bytes_pinned >= bytes) {
3827 spin_unlock(&space_info->lock);
3828 goto commit;
3829 }
3830 spin_unlock(&space_info->lock);
3831
3832 /*
3833 * See if there is some space in the delayed insertion reservation for
3834 * this reservation.
3835 */
3836 if (space_info != delayed_rsv->space_info)
3837 return -ENOSPC;
3838
Liu Bod9b02182012-02-16 18:34:39 +08003839 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003840 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003841 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04003842 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003843 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003844 return -ENOSPC;
3845 }
3846 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003847 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003848
3849commit:
3850 trans = btrfs_join_transaction(root);
3851 if (IS_ERR(trans))
3852 return -ENOSPC;
3853
3854 return btrfs_commit_transaction(trans, root);
3855}
3856
Josef Bacik96c3f432012-06-21 14:05:49 -04003857enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04003858 FLUSH_DELAYED_ITEMS_NR = 1,
3859 FLUSH_DELAYED_ITEMS = 2,
3860 FLUSH_DELALLOC = 3,
3861 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04003862 ALLOC_CHUNK = 5,
3863 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04003864};
3865
3866static int flush_space(struct btrfs_root *root,
3867 struct btrfs_space_info *space_info, u64 num_bytes,
3868 u64 orig_bytes, int state)
3869{
3870 struct btrfs_trans_handle *trans;
3871 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003872 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04003873
3874 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04003875 case FLUSH_DELAYED_ITEMS_NR:
3876 case FLUSH_DELAYED_ITEMS:
3877 if (state == FLUSH_DELAYED_ITEMS_NR) {
3878 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3879
3880 nr = (int)div64_u64(num_bytes, bytes);
3881 if (!nr)
3882 nr = 1;
3883 nr *= 2;
3884 } else {
3885 nr = -1;
3886 }
3887 trans = btrfs_join_transaction(root);
3888 if (IS_ERR(trans)) {
3889 ret = PTR_ERR(trans);
3890 break;
3891 }
3892 ret = btrfs_run_delayed_items_nr(trans, root, nr);
3893 btrfs_end_transaction(trans, root);
3894 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003895 case FLUSH_DELALLOC:
3896 case FLUSH_DELALLOC_WAIT:
3897 shrink_delalloc(root, num_bytes, orig_bytes,
3898 state == FLUSH_DELALLOC_WAIT);
3899 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04003900 case ALLOC_CHUNK:
3901 trans = btrfs_join_transaction(root);
3902 if (IS_ERR(trans)) {
3903 ret = PTR_ERR(trans);
3904 break;
3905 }
3906 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04003907 btrfs_get_alloc_profile(root, 0),
3908 CHUNK_ALLOC_NO_FORCE);
3909 btrfs_end_transaction(trans, root);
3910 if (ret == -ENOSPC)
3911 ret = 0;
3912 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04003913 case COMMIT_TRANS:
3914 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3915 break;
3916 default:
3917 ret = -ENOSPC;
3918 break;
3919 }
3920
3921 return ret;
3922}
Josef Bacik663350a2011-11-03 22:54:25 -04003923/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003924 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3925 * @root - the root we're allocating for
3926 * @block_rsv - the block_rsv we're allocating for
3927 * @orig_bytes - the number of bytes we want
3928 * @flush - wether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003929 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003930 * This will reserve orgi_bytes number of bytes from the space info associated
3931 * with the block_rsv. If there is not enough space it will make an attempt to
3932 * flush out space to make room. It will do this by flushing delalloc if
3933 * possible or committing the transaction. If flush is 0 then no attempts to
3934 * regain reservations will be made and this will fail if there is not enough
3935 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003936 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003937static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003938 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00003939 u64 orig_bytes,
3940 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003941{
3942 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04003943 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003944 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003945 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003946 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003947 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003948
3949again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003950 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003951 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003952 /*
Miao Xie08e007d2012-10-16 11:33:38 +00003953 * We only want to wait if somebody other than us is flushing and we
3954 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003955 */
Miao Xie08e007d2012-10-16 11:33:38 +00003956 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
3957 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003958 spin_unlock(&space_info->lock);
3959 /*
3960 * If we have a trans handle we can't wait because the flusher
3961 * may have to commit the transaction, which would mean we would
3962 * deadlock since we are waiting for the flusher to finish, but
3963 * hold the current transaction open.
3964 */
Josef Bacik663350a2011-11-03 22:54:25 -04003965 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003966 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02003967 ret = wait_event_killable(space_info->wait, !space_info->flush);
3968 /* Must have been killed, return */
3969 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003970 return -EINTR;
3971
3972 spin_lock(&space_info->lock);
3973 }
3974
3975 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04003976 used = space_info->bytes_used + space_info->bytes_reserved +
3977 space_info->bytes_pinned + space_info->bytes_readonly +
3978 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003979
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003980 /*
3981 * The idea here is that we've not already over-reserved the block group
3982 * then we can go ahead and save our reservation first and then start
3983 * flushing if we need to. Otherwise if we've already overcommitted
3984 * lets start flushing stuff first and then come back and try to make
3985 * our reservation.
3986 */
Josef Bacik2bf64752011-09-26 17:12:22 -04003987 if (used <= space_info->total_bytes) {
3988 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04003989 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003990 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04003991 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003992 ret = 0;
3993 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003994 /*
3995 * Ok set num_bytes to orig_bytes since we aren't
3996 * overocmmitted, this way we only try and reclaim what
3997 * we need.
3998 */
3999 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004000 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004001 } else {
4002 /*
4003 * Ok we're over committed, set num_bytes to the overcommitted
4004 * amount plus the amount of bytes that we need for this
4005 * reservation.
4006 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004007 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004008 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004009 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004010
Josef Bacik44734ed2012-09-28 16:04:19 -04004011 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4012 space_info->bytes_may_use += orig_bytes;
4013 trace_btrfs_space_reservation(root->fs_info, "space_info",
4014 space_info->flags, orig_bytes,
4015 1);
4016 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004017 }
4018
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004019 /*
4020 * Couldn't make our reservation, save our place so while we're trying
4021 * to reclaim space we can actually use it instead of somebody else
4022 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004023 *
4024 * We make the other tasks wait for the flush only when we can flush
4025 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004026 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004027 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004028 flushing = true;
4029 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004030 }
4031
Yan, Zhengf0486c62010-05-16 10:46:25 -04004032 spin_unlock(&space_info->lock);
4033
Miao Xie08e007d2012-10-16 11:33:38 +00004034 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004035 goto out;
4036
Josef Bacik96c3f432012-06-21 14:05:49 -04004037 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4038 flush_state);
4039 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004040
4041 /*
4042 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4043 * would happen. So skip delalloc flush.
4044 */
4045 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4046 (flush_state == FLUSH_DELALLOC ||
4047 flush_state == FLUSH_DELALLOC_WAIT))
4048 flush_state = ALLOC_CHUNK;
4049
Josef Bacik96c3f432012-06-21 14:05:49 -04004050 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004051 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004052 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4053 flush_state < COMMIT_TRANS)
4054 goto again;
4055 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4056 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004057 goto again;
4058
4059out:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004060 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004061 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004062 space_info->flush = 0;
4063 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004064 spin_unlock(&space_info->lock);
4065 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004066 return ret;
4067}
4068
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004069static struct btrfs_block_rsv *get_block_rsv(
4070 const struct btrfs_trans_handle *trans,
4071 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004072{
Josef Bacik4c13d752011-08-30 11:31:29 -04004073 struct btrfs_block_rsv *block_rsv = NULL;
4074
Josef Bacik0e721102012-06-26 16:13:18 -04004075 if (root->ref_cows)
4076 block_rsv = trans->block_rsv;
4077
4078 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004079 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004080
4081 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004082 block_rsv = root->block_rsv;
4083
4084 if (!block_rsv)
4085 block_rsv = &root->fs_info->empty_block_rsv;
4086
4087 return block_rsv;
4088}
4089
4090static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4091 u64 num_bytes)
4092{
4093 int ret = -ENOSPC;
4094 spin_lock(&block_rsv->lock);
4095 if (block_rsv->reserved >= num_bytes) {
4096 block_rsv->reserved -= num_bytes;
4097 if (block_rsv->reserved < block_rsv->size)
4098 block_rsv->full = 0;
4099 ret = 0;
4100 }
4101 spin_unlock(&block_rsv->lock);
4102 return ret;
4103}
4104
4105static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4106 u64 num_bytes, int update_size)
4107{
4108 spin_lock(&block_rsv->lock);
4109 block_rsv->reserved += num_bytes;
4110 if (update_size)
4111 block_rsv->size += num_bytes;
4112 else if (block_rsv->reserved >= block_rsv->size)
4113 block_rsv->full = 1;
4114 spin_unlock(&block_rsv->lock);
4115}
4116
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004117static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4118 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004119 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004120{
4121 struct btrfs_space_info *space_info = block_rsv->space_info;
4122
4123 spin_lock(&block_rsv->lock);
4124 if (num_bytes == (u64)-1)
4125 num_bytes = block_rsv->size;
4126 block_rsv->size -= num_bytes;
4127 if (block_rsv->reserved >= block_rsv->size) {
4128 num_bytes = block_rsv->reserved - block_rsv->size;
4129 block_rsv->reserved = block_rsv->size;
4130 block_rsv->full = 1;
4131 } else {
4132 num_bytes = 0;
4133 }
4134 spin_unlock(&block_rsv->lock);
4135
4136 if (num_bytes > 0) {
4137 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004138 spin_lock(&dest->lock);
4139 if (!dest->full) {
4140 u64 bytes_to_add;
4141
4142 bytes_to_add = dest->size - dest->reserved;
4143 bytes_to_add = min(num_bytes, bytes_to_add);
4144 dest->reserved += bytes_to_add;
4145 if (dest->reserved >= dest->size)
4146 dest->full = 1;
4147 num_bytes -= bytes_to_add;
4148 }
4149 spin_unlock(&dest->lock);
4150 }
4151 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004152 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004153 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004154 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004155 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004156 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004157 spin_unlock(&space_info->lock);
4158 }
4159 }
4160}
4161
4162static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4163 struct btrfs_block_rsv *dst, u64 num_bytes)
4164{
4165 int ret;
4166
4167 ret = block_rsv_use_bytes(src, num_bytes);
4168 if (ret)
4169 return ret;
4170
4171 block_rsv_add_bytes(dst, num_bytes, 1);
4172 return 0;
4173}
4174
Miao Xie66d8f3d2012-09-06 04:02:28 -06004175void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004176{
4177 memset(rsv, 0, sizeof(*rsv));
4178 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004179 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004180}
4181
Miao Xie66d8f3d2012-09-06 04:02:28 -06004182struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4183 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004184{
4185 struct btrfs_block_rsv *block_rsv;
4186 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004187
4188 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4189 if (!block_rsv)
4190 return NULL;
4191
Miao Xie66d8f3d2012-09-06 04:02:28 -06004192 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004193 block_rsv->space_info = __find_space_info(fs_info,
4194 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004195 return block_rsv;
4196}
4197
4198void btrfs_free_block_rsv(struct btrfs_root *root,
4199 struct btrfs_block_rsv *rsv)
4200{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004201 if (!rsv)
4202 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004203 btrfs_block_rsv_release(root, rsv, (u64)-1);
4204 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004205}
4206
Miao Xie08e007d2012-10-16 11:33:38 +00004207int btrfs_block_rsv_add(struct btrfs_root *root,
4208 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4209 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004210{
4211 int ret;
4212
4213 if (num_bytes == 0)
4214 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004215
Miao Xie61b520a2011-11-10 20:45:05 -05004216 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004217 if (!ret) {
4218 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4219 return 0;
4220 }
4221
Yan, Zhengf0486c62010-05-16 10:46:25 -04004222 return ret;
4223}
4224
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004225int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004226 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004227{
4228 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004229 int ret = -ENOSPC;
4230
4231 if (!block_rsv)
4232 return 0;
4233
4234 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004235 num_bytes = div_factor(block_rsv->size, min_factor);
4236 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004237 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004238 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004239
Josef Bacik36ba0222011-10-18 12:15:48 -04004240 return ret;
4241}
4242
Miao Xie08e007d2012-10-16 11:33:38 +00004243int btrfs_block_rsv_refill(struct btrfs_root *root,
4244 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4245 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004246{
4247 u64 num_bytes = 0;
4248 int ret = -ENOSPC;
4249
4250 if (!block_rsv)
4251 return 0;
4252
4253 spin_lock(&block_rsv->lock);
4254 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004255 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004256 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004257 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004258 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004259 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004260
Yan, Zhengf0486c62010-05-16 10:46:25 -04004261 if (!ret)
4262 return 0;
4263
Miao Xieaa38a712011-11-18 17:43:00 +08004264 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004265 if (!ret) {
4266 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004267 return 0;
4268 }
4269
Josef Bacik13553e52011-08-08 13:33:21 -04004270 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004271}
4272
4273int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4274 struct btrfs_block_rsv *dst_rsv,
4275 u64 num_bytes)
4276{
4277 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4278}
4279
4280void btrfs_block_rsv_release(struct btrfs_root *root,
4281 struct btrfs_block_rsv *block_rsv,
4282 u64 num_bytes)
4283{
4284 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4285 if (global_rsv->full || global_rsv == block_rsv ||
4286 block_rsv->space_info != global_rsv->space_info)
4287 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004288 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4289 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004290}
4291
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004292/*
4293 * helper to calculate size of global block reservation.
4294 * the desired value is sum of space used by extent tree,
4295 * checksum tree and root tree
4296 */
4297static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4298{
4299 struct btrfs_space_info *sinfo;
4300 u64 num_bytes;
4301 u64 meta_used;
4302 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004303 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004304
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004305 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4306 spin_lock(&sinfo->lock);
4307 data_used = sinfo->bytes_used;
4308 spin_unlock(&sinfo->lock);
4309
4310 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4311 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004312 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4313 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004314 meta_used = sinfo->bytes_used;
4315 spin_unlock(&sinfo->lock);
4316
4317 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4318 csum_size * 2;
4319 num_bytes += div64_u64(data_used + meta_used, 50);
4320
4321 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004322 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004323
4324 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4325}
4326
4327static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4328{
4329 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4330 struct btrfs_space_info *sinfo = block_rsv->space_info;
4331 u64 num_bytes;
4332
4333 num_bytes = calc_global_metadata_size(fs_info);
4334
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004335 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004336 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004337
4338 block_rsv->size = num_bytes;
4339
4340 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004341 sinfo->bytes_reserved + sinfo->bytes_readonly +
4342 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004343
4344 if (sinfo->total_bytes > num_bytes) {
4345 num_bytes = sinfo->total_bytes - num_bytes;
4346 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004347 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004348 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004349 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004350 }
4351
4352 if (block_rsv->reserved >= block_rsv->size) {
4353 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004354 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004355 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004356 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004357 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004358 block_rsv->reserved = block_rsv->size;
4359 block_rsv->full = 1;
4360 }
David Sterba182608c2011-05-05 13:13:16 +02004361
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004362 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004363 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004364}
4365
Yan, Zhengf0486c62010-05-16 10:46:25 -04004366static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4367{
4368 struct btrfs_space_info *space_info;
4369
4370 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4371 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004372
4373 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004374 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004375 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004376 fs_info->trans_block_rsv.space_info = space_info;
4377 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004378 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004379
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004380 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4381 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4382 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4383 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004384 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004385
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004386 update_global_block_rsv(fs_info);
4387}
4388
4389static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4390{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004391 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4392 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004393 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4394 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4395 WARN_ON(fs_info->trans_block_rsv.size > 0);
4396 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4397 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4398 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004399 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4400 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004401}
4402
Yan, Zhenga22285a2010-05-16 10:48:46 -04004403void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4404 struct btrfs_root *root)
4405{
Josef Bacik0e721102012-06-26 16:13:18 -04004406 if (!trans->block_rsv)
4407 return;
4408
Yan, Zhenga22285a2010-05-16 10:48:46 -04004409 if (!trans->bytes_reserved)
4410 return;
4411
Chris Masone77266e2012-02-24 10:39:05 -05004412 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004413 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004414 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004415 trans->bytes_reserved = 0;
4416}
4417
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004418/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004419int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4420 struct inode *inode)
4421{
4422 struct btrfs_root *root = BTRFS_I(inode)->root;
4423 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4424 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4425
4426 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004427 * We need to hold space in order to delete our orphan item once we've
4428 * added it, so this takes the reservation so we can release it later
4429 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004430 */
Chris Masonff5714c2011-05-28 07:00:39 -04004431 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004432 trace_btrfs_space_reservation(root->fs_info, "orphan",
4433 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004434 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4435}
4436
4437void btrfs_orphan_release_metadata(struct inode *inode)
4438{
4439 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004440 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004441 trace_btrfs_space_reservation(root->fs_info, "orphan",
4442 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004443 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4444}
4445
Yan, Zhenga22285a2010-05-16 10:48:46 -04004446int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4447 struct btrfs_pending_snapshot *pending)
4448{
4449 struct btrfs_root *root = pending->root;
4450 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4451 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4452 /*
Miao Xie48c03c42012-09-06 04:03:56 -06004453 * two for root back/forward refs, two for directory entries,
4454 * one for root of the snapshot and one for parent inode.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004455 */
Miao Xie48c03c42012-09-06 04:03:56 -06004456 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004457 dst_rsv->space_info = src_rsv->space_info;
4458 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4459}
4460
Josef Bacik7709cde2011-08-04 10:25:02 -04004461/**
4462 * drop_outstanding_extent - drop an outstanding extent
4463 * @inode: the inode we're dropping the extent for
4464 *
4465 * This is called when we are freeing up an outstanding extent, either called
4466 * after an error or after an extent is written. This will return the number of
4467 * reserved extents that need to be freed. This must be called with
4468 * BTRFS_I(inode)->lock held.
4469 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004470static unsigned drop_outstanding_extent(struct inode *inode)
4471{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004472 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004473 unsigned dropped_extents = 0;
4474
Josef Bacik9e0baf62011-07-15 15:16:44 +00004475 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4476 BTRFS_I(inode)->outstanding_extents--;
4477
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004478 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004479 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4480 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004481 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004482
Josef Bacik9e0baf62011-07-15 15:16:44 +00004483 /*
4484 * If we have more or the same amount of outsanding extents than we have
4485 * reserved then we need to leave the reserved extents count alone.
4486 */
4487 if (BTRFS_I(inode)->outstanding_extents >=
4488 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004489 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004490
4491 dropped_extents = BTRFS_I(inode)->reserved_extents -
4492 BTRFS_I(inode)->outstanding_extents;
4493 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004494 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004495}
4496
Josef Bacik7709cde2011-08-04 10:25:02 -04004497/**
4498 * calc_csum_metadata_size - return the amount of metada space that must be
4499 * reserved/free'd for the given bytes.
4500 * @inode: the inode we're manipulating
4501 * @num_bytes: the number of bytes in question
4502 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4503 *
4504 * This adjusts the number of csum_bytes in the inode and then returns the
4505 * correct amount of metadata that must either be reserved or freed. We
4506 * calculate how many checksums we can fit into one leaf and then divide the
4507 * number of bytes that will need to be checksumed by this value to figure out
4508 * how many checksums will be required. If we are adding bytes then the number
4509 * may go up and we will return the number of additional bytes that must be
4510 * reserved. If it is going down we will return the number of bytes that must
4511 * be freed.
4512 *
4513 * This must be called with BTRFS_I(inode)->lock held.
4514 */
4515static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4516 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004517{
Josef Bacik7709cde2011-08-04 10:25:02 -04004518 struct btrfs_root *root = BTRFS_I(inode)->root;
4519 u64 csum_size;
4520 int num_csums_per_leaf;
4521 int num_csums;
4522 int old_csums;
4523
4524 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4525 BTRFS_I(inode)->csum_bytes == 0)
4526 return 0;
4527
4528 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4529 if (reserve)
4530 BTRFS_I(inode)->csum_bytes += num_bytes;
4531 else
4532 BTRFS_I(inode)->csum_bytes -= num_bytes;
4533 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4534 num_csums_per_leaf = (int)div64_u64(csum_size,
4535 sizeof(struct btrfs_csum_item) +
4536 sizeof(struct btrfs_disk_key));
4537 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4538 num_csums = num_csums + num_csums_per_leaf - 1;
4539 num_csums = num_csums / num_csums_per_leaf;
4540
4541 old_csums = old_csums + num_csums_per_leaf - 1;
4542 old_csums = old_csums / num_csums_per_leaf;
4543
4544 /* No change, no need to reserve more */
4545 if (old_csums == num_csums)
4546 return 0;
4547
4548 if (reserve)
4549 return btrfs_calc_trans_metadata_size(root,
4550 num_csums - old_csums);
4551
4552 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004553}
4554
4555int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4556{
4557 struct btrfs_root *root = BTRFS_I(inode)->root;
4558 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004559 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004560 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004561 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004562 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004563 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004564 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004565 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004566
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004567 /* If we are a free space inode we need to not flush since we will be in
4568 * the middle of a transaction commit. We also don't need the delalloc
4569 * mutex since we won't race with anybody. We need this mostly to make
4570 * lockdep shut its filthy mouth.
4571 */
4572 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004573 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004574 delalloc_lock = false;
4575 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004576
Miao Xie08e007d2012-10-16 11:33:38 +00004577 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4578 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004579 schedule_timeout(1);
4580
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004581 if (delalloc_lock)
4582 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4583
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004584 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004585
Josef Bacik9e0baf62011-07-15 15:16:44 +00004586 spin_lock(&BTRFS_I(inode)->lock);
4587 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004588
Josef Bacik9e0baf62011-07-15 15:16:44 +00004589 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004590 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004591 nr_extents = BTRFS_I(inode)->outstanding_extents -
4592 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004593
4594 /*
4595 * Add an item to reserve for updating the inode when we complete the
4596 * delalloc io.
4597 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004598 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4599 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004600 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004601 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004602 }
4603
4604 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004605 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004606 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004607 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004608
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004609 if (root->fs_info->quota_enabled)
Arne Jansenc5567232011-09-14 15:44:05 +02004610 ret = btrfs_qgroup_reserve(root, num_bytes +
4611 nr_extents * root->leafsize);
Arne Jansenc5567232011-09-14 15:44:05 +02004612
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004613 /*
4614 * ret != 0 here means the qgroup reservation failed, we go straight to
4615 * the shared error handling then.
4616 */
4617 if (ret == 0)
4618 ret = reserve_metadata_bytes(root, block_rsv,
4619 to_reserve, flush);
4620
Josef Bacik9e0baf62011-07-15 15:16:44 +00004621 if (ret) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004622 u64 to_free = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004623 unsigned dropped;
Josef Bacik7ed49f12011-08-19 15:45:52 -04004624
Josef Bacik7709cde2011-08-04 10:25:02 -04004625 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004626 dropped = drop_outstanding_extent(inode);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004627 /*
Josef Bacik660d3f62011-12-09 11:18:51 -05004628 * If the inodes csum_bytes is the same as the original
4629 * csum_bytes then we know we haven't raced with any free()ers
4630 * so we can just reduce our inodes csum bytes and carry on.
4631 * Otherwise we have to do the normal free thing to account for
4632 * the case that the free side didn't free up its reserve
4633 * because of this outstanding reservation.
Josef Bacik7ed49f12011-08-19 15:45:52 -04004634 */
Josef Bacik660d3f62011-12-09 11:18:51 -05004635 if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4636 calc_csum_metadata_size(inode, num_bytes, 0);
4637 else
4638 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4639 spin_unlock(&BTRFS_I(inode)->lock);
4640 if (dropped)
4641 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4642
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004643 if (to_free) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004644 btrfs_block_rsv_release(root, block_rsv, to_free);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004645 trace_btrfs_space_reservation(root->fs_info,
4646 "delalloc",
4647 btrfs_ino(inode),
4648 to_free, 0);
4649 }
Miao Xie4b5829a2012-12-05 10:53:25 +00004650 if (root->fs_info->quota_enabled) {
4651 btrfs_qgroup_free(root, num_bytes +
4652 nr_extents * root->leafsize);
4653 }
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004654 if (delalloc_lock)
4655 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004656 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004657 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004658
Josef Bacik660d3f62011-12-09 11:18:51 -05004659 spin_lock(&BTRFS_I(inode)->lock);
4660 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004661 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4662 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004663 nr_extents--;
4664 }
4665 BTRFS_I(inode)->reserved_extents += nr_extents;
4666 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004667
4668 if (delalloc_lock)
4669 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004670
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004671 if (to_reserve)
4672 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4673 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004674 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4675
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004676 return 0;
4677}
4678
Josef Bacik7709cde2011-08-04 10:25:02 -04004679/**
4680 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4681 * @inode: the inode to release the reservation for
4682 * @num_bytes: the number of bytes we're releasing
4683 *
4684 * This will release the metadata reservation for an inode. This can be called
4685 * once we complete IO for a given set of bytes to release their metadata
4686 * reservations.
4687 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004688void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4689{
4690 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004691 u64 to_free = 0;
4692 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004693
4694 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004695 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004696 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004697
Josef Bacik7709cde2011-08-04 10:25:02 -04004698 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4699 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004700 if (dropped > 0)
4701 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004702
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004703 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4704 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004705 if (root->fs_info->quota_enabled) {
4706 btrfs_qgroup_free(root, num_bytes +
4707 dropped * root->leafsize);
4708 }
4709
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004710 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4711 to_free);
4712}
4713
Josef Bacik7709cde2011-08-04 10:25:02 -04004714/**
4715 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4716 * @inode: inode we're writing to
4717 * @num_bytes: the number of bytes we want to allocate
4718 *
4719 * This will do the following things
4720 *
4721 * o reserve space in the data space info for num_bytes
4722 * o reserve space in the metadata space info based on number of outstanding
4723 * extents and how much csums will be needed
4724 * o add to the inodes ->delalloc_bytes
4725 * o add it to the fs_info's delalloc inodes list.
4726 *
4727 * This will return 0 for success and -ENOSPC if there is no space left.
4728 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004729int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4730{
4731 int ret;
4732
4733 ret = btrfs_check_data_free_space(inode, num_bytes);
4734 if (ret)
4735 return ret;
4736
4737 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4738 if (ret) {
4739 btrfs_free_reserved_data_space(inode, num_bytes);
4740 return ret;
4741 }
4742
4743 return 0;
4744}
4745
Josef Bacik7709cde2011-08-04 10:25:02 -04004746/**
4747 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4748 * @inode: inode we're releasing space for
4749 * @num_bytes: the number of bytes we want to free up
4750 *
4751 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4752 * called in the case that we don't need the metadata AND data reservations
4753 * anymore. So if there is an error or we insert an inline extent.
4754 *
4755 * This function will release the metadata space that was not used and will
4756 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4757 * list if there are no delalloc bytes left.
4758 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004759void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4760{
4761 btrfs_delalloc_release_metadata(inode, num_bytes);
4762 btrfs_free_reserved_data_space(inode, num_bytes);
4763}
4764
Chris Mason9078a3e2007-04-26 16:46:15 -04004765static int update_block_group(struct btrfs_trans_handle *trans,
4766 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004767 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004768{
Josef Bacik0af3d002010-06-21 14:48:16 -04004769 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004770 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004771 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004772 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004773 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004774 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004775
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004776 /* block accounting for super block */
4777 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02004778 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004779 if (alloc)
4780 old_val += num_bytes;
4781 else
4782 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02004783 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004784 spin_unlock(&info->delalloc_lock);
4785
Chris Masond3977122009-01-05 21:25:51 -05004786 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004787 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004788 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004789 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004790 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4791 BTRFS_BLOCK_GROUP_RAID1 |
4792 BTRFS_BLOCK_GROUP_RAID10))
4793 factor = 2;
4794 else
4795 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004796 /*
4797 * If this block group has free space cache written out, we
4798 * need to make sure to load it if we are removing space. This
4799 * is because we need the unpinning stage to actually add the
4800 * space back to the block group, otherwise we will leak space.
4801 */
4802 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00004803 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004804
Chris Masondb945352007-10-15 16:15:53 -04004805 byte_in_group = bytenr - cache->key.objectid;
4806 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004807
Josef Bacik25179202008-10-29 14:49:05 -04004808 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004809 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004810
Josef Bacik73bc1872011-10-03 14:07:49 -04004811 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04004812 cache->disk_cache_state < BTRFS_DC_CLEAR)
4813 cache->disk_cache_state = BTRFS_DC_CLEAR;
4814
Josef Bacik0f9dd462008-09-23 13:14:11 -04004815 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004816 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004817 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004818 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004819 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004820 btrfs_set_block_group_used(&cache->item, old_val);
4821 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004822 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004823 cache->space_info->bytes_used += num_bytes;
4824 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004825 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004826 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004827 } else {
Chris Masondb945352007-10-15 16:15:53 -04004828 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004829 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004830 cache->pinned += num_bytes;
4831 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004832 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004833 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004834 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004835 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004836
Yan, Zhengf0486c62010-05-16 10:46:25 -04004837 set_extent_dirty(info->pinned_extents,
4838 bytenr, bytenr + num_bytes - 1,
4839 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004840 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004841 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004842 total -= num_bytes;
4843 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004844 }
4845 return 0;
4846}
Chris Mason6324fbf2008-03-24 15:01:59 -04004847
Chris Masona061fc82008-05-07 11:43:44 -04004848static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4849{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004850 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004851 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004852
4853 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4854 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004855 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004856
Yan Zhengd2fb3432008-12-11 16:30:39 -05004857 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004858 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004859
4860 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004861}
4862
Yan, Zhengf0486c62010-05-16 10:46:25 -04004863static int pin_down_extent(struct btrfs_root *root,
4864 struct btrfs_block_group_cache *cache,
4865 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004866{
Yan Zheng11833d62009-09-11 16:11:19 -04004867 spin_lock(&cache->space_info->lock);
4868 spin_lock(&cache->lock);
4869 cache->pinned += num_bytes;
4870 cache->space_info->bytes_pinned += num_bytes;
4871 if (reserved) {
4872 cache->reserved -= num_bytes;
4873 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004874 }
Yan Zheng11833d62009-09-11 16:11:19 -04004875 spin_unlock(&cache->lock);
4876 spin_unlock(&cache->space_info->lock);
4877
Yan, Zhengf0486c62010-05-16 10:46:25 -04004878 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4879 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004880 return 0;
4881}
Chris Mason9078a3e2007-04-26 16:46:15 -04004882
Yan, Zhengf0486c62010-05-16 10:46:25 -04004883/*
4884 * this function must be called within transaction
4885 */
4886int btrfs_pin_extent(struct btrfs_root *root,
4887 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004888{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004889 struct btrfs_block_group_cache *cache;
4890
4891 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004892 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04004893
4894 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4895
4896 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004897 return 0;
4898}
Zheng Yane8569812008-09-26 10:05:48 -04004899
Yan, Zhengf0486c62010-05-16 10:46:25 -04004900/*
Chris Masone688b7252011-10-31 20:52:39 -04004901 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04004902 */
Chris Masone688b7252011-10-31 20:52:39 -04004903int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
4904 struct btrfs_root *root,
4905 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004906{
Chris Masone688b7252011-10-31 20:52:39 -04004907 struct btrfs_block_group_cache *cache;
4908
4909 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004910 BUG_ON(!cache); /* Logic error */
Chris Masone688b7252011-10-31 20:52:39 -04004911
4912 /*
4913 * pull in the free space cache (if any) so that our pin
4914 * removes the free space from the cache. We have load_only set
4915 * to one because the slow code to read in the free extents does check
4916 * the pinned extents.
4917 */
Liu Bof6373bf2012-12-27 09:01:18 +00004918 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04004919
4920 pin_down_extent(root, cache, bytenr, num_bytes, 0);
4921
4922 /* remove us from the free space cache (if we're there at all) */
4923 btrfs_remove_free_space(cache, bytenr, num_bytes);
4924 btrfs_put_block_group(cache);
4925 return 0;
4926}
4927
Josef Bacikfb25e912011-07-26 17:00:46 -04004928/**
4929 * btrfs_update_reserved_bytes - update the block_group and space info counters
4930 * @cache: The cache we are manipulating
4931 * @num_bytes: The number of bytes in question
4932 * @reserve: One of the reservation enums
4933 *
4934 * This is called by the allocator when it reserves space, or by somebody who is
4935 * freeing space that was never actually used on disk. For example if you
4936 * reserve some space for a new leaf in transaction A and before transaction A
4937 * commits you free that leaf, you call this with reserve set to 0 in order to
4938 * clear the reservation.
4939 *
4940 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4941 * ENOSPC accounting. For data we handle the reservation through clearing the
4942 * delalloc bits in the io_tree. We have to do this since we could end up
4943 * allocating less disk space for the amount of data we have reserved in the
4944 * case of compression.
4945 *
4946 * If this is a reservation and the block group has become read only we cannot
4947 * make the reservation and return -EAGAIN, otherwise this function always
4948 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04004949 */
Josef Bacikfb25e912011-07-26 17:00:46 -04004950static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4951 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004952{
Josef Bacikfb25e912011-07-26 17:00:46 -04004953 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004954 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004955
Josef Bacikfb25e912011-07-26 17:00:46 -04004956 spin_lock(&space_info->lock);
4957 spin_lock(&cache->lock);
4958 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004959 if (cache->ro) {
4960 ret = -EAGAIN;
4961 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04004962 cache->reserved += num_bytes;
4963 space_info->bytes_reserved += num_bytes;
4964 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004965 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004966 "space_info", space_info->flags,
4967 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04004968 space_info->bytes_may_use -= num_bytes;
4969 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004970 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004971 } else {
4972 if (cache->ro)
4973 space_info->bytes_readonly += num_bytes;
4974 cache->reserved -= num_bytes;
4975 space_info->bytes_reserved -= num_bytes;
4976 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004977 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004978 spin_unlock(&cache->lock);
4979 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004980 return ret;
4981}
4982
Jeff Mahoney143bede2012-03-01 14:56:26 +01004983void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004984 struct btrfs_root *root)
4985{
4986 struct btrfs_fs_info *fs_info = root->fs_info;
4987 struct btrfs_caching_control *next;
4988 struct btrfs_caching_control *caching_ctl;
4989 struct btrfs_block_group_cache *cache;
4990
4991 down_write(&fs_info->extent_commit_sem);
4992
4993 list_for_each_entry_safe(caching_ctl, next,
4994 &fs_info->caching_block_groups, list) {
4995 cache = caching_ctl->block_group;
4996 if (block_group_cache_done(cache)) {
4997 cache->last_byte_to_unpin = (u64)-1;
4998 list_del_init(&caching_ctl->list);
4999 put_caching_control(caching_ctl);
5000 } else {
5001 cache->last_byte_to_unpin = caching_ctl->progress;
5002 }
5003 }
5004
5005 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5006 fs_info->pinned_extents = &fs_info->freed_extents[1];
5007 else
5008 fs_info->pinned_extents = &fs_info->freed_extents[0];
5009
5010 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005011
5012 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005013}
5014
5015static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5016{
5017 struct btrfs_fs_info *fs_info = root->fs_info;
5018 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005019 struct btrfs_space_info *space_info;
5020 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005021 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005022 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005023
5024 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005025 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005026 if (!cache ||
5027 start >= cache->key.objectid + cache->key.offset) {
5028 if (cache)
5029 btrfs_put_block_group(cache);
5030 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005031 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005032 }
5033
5034 len = cache->key.objectid + cache->key.offset - start;
5035 len = min(len, end + 1 - start);
5036
5037 if (start < cache->last_byte_to_unpin) {
5038 len = min(len, cache->last_byte_to_unpin - start);
5039 btrfs_add_free_space(cache, start, len);
5040 }
Josef Bacik25179202008-10-29 14:49:05 -04005041
Yan, Zhengf0486c62010-05-16 10:46:25 -04005042 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005043 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005044
Josef Bacik7b398f82012-10-22 15:52:28 -04005045 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005046 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005047 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005048 space_info->bytes_pinned -= len;
5049 if (cache->ro) {
5050 space_info->bytes_readonly += len;
5051 readonly = true;
5052 }
Josef Bacik25179202008-10-29 14:49:05 -04005053 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005054 if (!readonly && global_rsv->space_info == space_info) {
5055 spin_lock(&global_rsv->lock);
5056 if (!global_rsv->full) {
5057 len = min(len, global_rsv->size -
5058 global_rsv->reserved);
5059 global_rsv->reserved += len;
5060 space_info->bytes_may_use += len;
5061 if (global_rsv->reserved >= global_rsv->size)
5062 global_rsv->full = 1;
5063 }
5064 spin_unlock(&global_rsv->lock);
5065 }
5066 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005067 }
5068
5069 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005070 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005071 return 0;
5072}
5073
5074int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005075 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005076{
Yan Zheng11833d62009-09-11 16:11:19 -04005077 struct btrfs_fs_info *fs_info = root->fs_info;
5078 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005079 u64 start;
5080 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005081 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005082
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005083 if (trans->aborted)
5084 return 0;
5085
Yan Zheng11833d62009-09-11 16:11:19 -04005086 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5087 unpin = &fs_info->freed_extents[1];
5088 else
5089 unpin = &fs_info->freed_extents[0];
5090
Chris Masond3977122009-01-05 21:25:51 -05005091 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005092 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005093 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005094 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005095 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005096
Li Dongyang5378e602011-03-24 10:24:27 +00005097 if (btrfs_test_opt(root, DISCARD))
5098 ret = btrfs_discard_extent(root, start,
5099 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005100
Chris Mason1a5bc162007-10-15 16:15:26 -04005101 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005102 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005103 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005104 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005105
Chris Masone20d96d2007-03-22 12:13:20 -04005106 return 0;
5107}
5108
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005109static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5110 struct btrfs_root *root,
5111 u64 bytenr, u64 num_bytes, u64 parent,
5112 u64 root_objectid, u64 owner_objectid,
5113 u64 owner_offset, int refs_to_drop,
5114 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005115{
Chris Masone2fa7222007-03-12 16:22:34 -04005116 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005117 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005118 struct btrfs_fs_info *info = root->fs_info;
5119 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005120 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005121 struct btrfs_extent_item *ei;
5122 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005123 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005124 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005125 int extent_slot = 0;
5126 int found_extent = 0;
5127 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005128 u32 item_size;
5129 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05005130
Chris Mason5caf2a02007-04-02 11:20:42 -04005131 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005132 if (!path)
5133 return -ENOMEM;
5134
Chris Mason3c12ac72008-04-21 12:01:38 -04005135 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005136 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005137
5138 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5139 BUG_ON(!is_data && refs_to_drop != 1);
5140
5141 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5142 bytenr, num_bytes, parent,
5143 root_objectid, owner_objectid,
5144 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005145 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005146 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005147 while (extent_slot >= 0) {
5148 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005149 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005150 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005151 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005152 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5153 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005154 found_extent = 1;
5155 break;
5156 }
5157 if (path->slots[0] - extent_slot > 5)
5158 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005159 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005160 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005161#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5162 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5163 if (found_extent && item_size < sizeof(*ei))
5164 found_extent = 0;
5165#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005166 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005167 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005168 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005169 NULL, refs_to_drop,
5170 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005171 if (ret) {
5172 btrfs_abort_transaction(trans, extent_root, ret);
5173 goto out;
5174 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005175 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005176 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005177
5178 key.objectid = bytenr;
5179 key.type = BTRFS_EXTENT_ITEM_KEY;
5180 key.offset = num_bytes;
5181
Zheng Yan31840ae2008-09-23 13:14:14 -04005182 ret = btrfs_search_slot(trans, extent_root,
5183 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005184 if (ret) {
5185 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05005186 ", was looking for %llu\n", ret,
5187 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005188 if (ret > 0)
5189 btrfs_print_leaf(extent_root,
5190 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005191 }
David Sterba005d6422012-09-18 07:52:32 -06005192 if (ret < 0) {
5193 btrfs_abort_transaction(trans, extent_root, ret);
5194 goto out;
5195 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005196 extent_slot = path->slots[0];
5197 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005198 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005199 btrfs_print_leaf(extent_root, path->nodes[0]);
5200 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05005201 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005202 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05005203 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04005204 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05005205 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005206 (unsigned long long)owner_objectid,
5207 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005208 } else {
David Sterba005d6422012-09-18 07:52:32 -06005209 btrfs_abort_transaction(trans, extent_root, ret);
5210 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005211 }
Chris Mason5f39d392007-10-15 16:14:19 -04005212
5213 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005214 item_size = btrfs_item_size_nr(leaf, extent_slot);
5215#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5216 if (item_size < sizeof(*ei)) {
5217 BUG_ON(found_extent || extent_slot != path->slots[0]);
5218 ret = convert_extent_item_v0(trans, extent_root, path,
5219 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005220 if (ret < 0) {
5221 btrfs_abort_transaction(trans, extent_root, ret);
5222 goto out;
5223 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005224
David Sterbab3b4aa72011-04-21 01:20:15 +02005225 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005226 path->leave_spinning = 1;
5227
5228 key.objectid = bytenr;
5229 key.type = BTRFS_EXTENT_ITEM_KEY;
5230 key.offset = num_bytes;
5231
5232 ret = btrfs_search_slot(trans, extent_root, &key, path,
5233 -1, 1);
5234 if (ret) {
5235 printk(KERN_ERR "umm, got %d back from search"
5236 ", was looking for %llu\n", ret,
5237 (unsigned long long)bytenr);
5238 btrfs_print_leaf(extent_root, path->nodes[0]);
5239 }
David Sterba005d6422012-09-18 07:52:32 -06005240 if (ret < 0) {
5241 btrfs_abort_transaction(trans, extent_root, ret);
5242 goto out;
5243 }
5244
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005245 extent_slot = path->slots[0];
5246 leaf = path->nodes[0];
5247 item_size = btrfs_item_size_nr(leaf, extent_slot);
5248 }
5249#endif
5250 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005251 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005252 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005253 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5254 struct btrfs_tree_block_info *bi;
5255 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5256 bi = (struct btrfs_tree_block_info *)(ei + 1);
5257 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005258 }
5259
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005260 refs = btrfs_extent_refs(leaf, ei);
5261 BUG_ON(refs < refs_to_drop);
5262 refs -= refs_to_drop;
5263
5264 if (refs > 0) {
5265 if (extent_op)
5266 __run_delayed_extent_op(extent_op, leaf, ei);
5267 /*
5268 * In the case of inline back ref, reference count will
5269 * be updated by remove_extent_backref
5270 */
5271 if (iref) {
5272 BUG_ON(!found_extent);
5273 } else {
5274 btrfs_set_extent_refs(leaf, ei, refs);
5275 btrfs_mark_buffer_dirty(leaf);
5276 }
5277 if (found_extent) {
5278 ret = remove_extent_backref(trans, extent_root, path,
5279 iref, refs_to_drop,
5280 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005281 if (ret) {
5282 btrfs_abort_transaction(trans, extent_root, ret);
5283 goto out;
5284 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005285 }
5286 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005287 if (found_extent) {
5288 BUG_ON(is_data && refs_to_drop !=
5289 extent_data_ref_count(root, path, iref));
5290 if (iref) {
5291 BUG_ON(path->slots[0] != extent_slot);
5292 } else {
5293 BUG_ON(path->slots[0] != extent_slot + 1);
5294 path->slots[0] = extent_slot;
5295 num_to_del = 2;
5296 }
Chris Mason78fae272007-03-25 11:35:08 -04005297 }
Chris Masonb9473432009-03-13 11:00:37 -04005298
Chris Mason952fcca2008-02-18 16:33:44 -05005299 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5300 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005301 if (ret) {
5302 btrfs_abort_transaction(trans, extent_root, ret);
5303 goto out;
5304 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005305 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005306
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005307 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005308 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005309 if (ret) {
5310 btrfs_abort_transaction(trans, extent_root, ret);
5311 goto out;
5312 }
Chris Mason459931e2008-12-10 09:10:46 -05005313 }
5314
Yan, Zhengf0486c62010-05-16 10:46:25 -04005315 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005316 if (ret) {
5317 btrfs_abort_transaction(trans, extent_root, ret);
5318 goto out;
5319 }
Chris Masona28ec192007-03-06 20:08:01 -05005320 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005321out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005322 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005323 return ret;
5324}
5325
5326/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005327 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005328 * delayed ref for that extent as well. This searches the delayed ref tree for
5329 * a given extent, and if there are no other delayed refs to be processed, it
5330 * removes it from the tree.
5331 */
5332static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5333 struct btrfs_root *root, u64 bytenr)
5334{
5335 struct btrfs_delayed_ref_head *head;
5336 struct btrfs_delayed_ref_root *delayed_refs;
5337 struct btrfs_delayed_ref_node *ref;
5338 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005339 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005340
5341 delayed_refs = &trans->transaction->delayed_refs;
5342 spin_lock(&delayed_refs->lock);
5343 head = btrfs_find_delayed_ref_head(trans, bytenr);
5344 if (!head)
5345 goto out;
5346
5347 node = rb_prev(&head->node.rb_node);
5348 if (!node)
5349 goto out;
5350
5351 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5352
5353 /* there are still entries for this ref, we can't drop it */
5354 if (ref->bytenr == bytenr)
5355 goto out;
5356
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005357 if (head->extent_op) {
5358 if (!head->must_insert_reserved)
5359 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005360 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005361 head->extent_op = NULL;
5362 }
5363
Chris Mason1887be62009-03-13 10:11:24 -04005364 /*
5365 * waiting for the lock here would deadlock. If someone else has it
5366 * locked they are already in the process of dropping it anyway
5367 */
5368 if (!mutex_trylock(&head->mutex))
5369 goto out;
5370
5371 /*
5372 * at this point we have a head with no other entries. Go
5373 * ahead and process it.
5374 */
5375 head->node.in_tree = 0;
5376 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005377
Chris Mason1887be62009-03-13 10:11:24 -04005378 delayed_refs->num_entries--;
5379
5380 /*
5381 * we don't take a ref on the node because we're removing it from the
5382 * tree, so we just steal the ref the tree was holding.
5383 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005384 delayed_refs->num_heads--;
5385 if (list_empty(&head->cluster))
5386 delayed_refs->num_heads_ready--;
5387
5388 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005389 spin_unlock(&delayed_refs->lock);
5390
Yan, Zhengf0486c62010-05-16 10:46:25 -04005391 BUG_ON(head->extent_op);
5392 if (head->must_insert_reserved)
5393 ret = 1;
5394
5395 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005396 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005397 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005398out:
5399 spin_unlock(&delayed_refs->lock);
5400 return 0;
5401}
5402
Yan, Zhengf0486c62010-05-16 10:46:25 -04005403void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5404 struct btrfs_root *root,
5405 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005406 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005407{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005408 struct btrfs_block_group_cache *cache = NULL;
5409 int ret;
5410
5411 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005412 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5413 buf->start, buf->len,
5414 parent, root->root_key.objectid,
5415 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005416 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005417 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005418 }
5419
5420 if (!last_ref)
5421 return;
5422
Yan, Zhengf0486c62010-05-16 10:46:25 -04005423 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005424
5425 if (btrfs_header_generation(buf) == trans->transid) {
5426 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5427 ret = check_ref_cleanup(trans, root, buf->start);
5428 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005429 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005430 }
5431
5432 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5433 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005434 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005435 }
5436
5437 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5438
5439 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005440 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005441 }
5442out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005443 /*
5444 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5445 * anymore.
5446 */
5447 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005448 btrfs_put_block_group(cache);
5449}
5450
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005451/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005452int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5453 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5454 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005455{
5456 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005457 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005458
Chris Mason56bec292009-03-13 10:10:06 -04005459 /*
5460 * tree log blocks never actually go into the extent allocation
5461 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005462 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005463 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5464 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005465 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005466 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005467 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005468 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005469 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5470 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005472 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005473 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005474 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5475 num_bytes,
5476 parent, root_objectid, owner,
5477 offset, BTRFS_DROP_DELAYED_REF,
5478 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005479 }
Chris Mason925baed2008-06-25 16:01:30 -04005480 return ret;
5481}
5482
Chris Mason87ee04e2007-11-30 11:30:34 -05005483static u64 stripe_align(struct btrfs_root *root, u64 val)
5484{
5485 u64 mask = ((u64)root->stripesize - 1);
5486 u64 ret = (val + mask) & ~mask;
5487 return ret;
5488}
5489
Chris Masonfec577f2007-02-26 10:40:21 -05005490/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005491 * when we wait for progress in the block group caching, its because
5492 * our allocation attempt failed at least once. So, we must sleep
5493 * and let some progress happen before we try again.
5494 *
5495 * This function will sleep at least once waiting for new free space to
5496 * show up, and then it will check the block group free space numbers
5497 * for our min num_bytes. Another option is to have it go ahead
5498 * and look in the rbtree for a free extent of a given size, but this
5499 * is a good start.
5500 */
5501static noinline int
5502wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5503 u64 num_bytes)
5504{
Yan Zheng11833d62009-09-11 16:11:19 -04005505 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005506 DEFINE_WAIT(wait);
5507
Yan Zheng11833d62009-09-11 16:11:19 -04005508 caching_ctl = get_caching_control(cache);
5509 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005510 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005511
Yan Zheng11833d62009-09-11 16:11:19 -04005512 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005513 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005514
5515 put_caching_control(caching_ctl);
5516 return 0;
5517}
5518
5519static noinline int
5520wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5521{
5522 struct btrfs_caching_control *caching_ctl;
5523 DEFINE_WAIT(wait);
5524
5525 caching_ctl = get_caching_control(cache);
5526 if (!caching_ctl)
5527 return 0;
5528
5529 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5530
5531 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005532 return 0;
5533}
5534
Liu Bo31e50222012-11-21 14:18:10 +00005535int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005536{
5537 int index;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005538
5539 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005540 index = 0;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005541 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005542 index = 1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005543 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005544 index = 2;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005545 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005546 index = 3;
5547 else
5548 index = 4;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005549
Yan, Zhengb742bb82010-05-16 10:46:24 -04005550 return index;
5551}
5552
Ilya Dryomov7738a532012-03-27 17:09:17 +03005553static int get_block_group_index(struct btrfs_block_group_cache *cache)
5554{
Liu Bo31e50222012-11-21 14:18:10 +00005555 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005556}
5557
Josef Bacik817d52f2009-07-13 21:29:25 -04005558enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005559 LOOP_CACHING_NOWAIT = 0,
5560 LOOP_CACHING_WAIT = 1,
5561 LOOP_ALLOC_CHUNK = 2,
5562 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005563};
5564
5565/*
Chris Masonfec577f2007-02-26 10:40:21 -05005566 * walks the btree of allocated extents and find a hole of a given size.
5567 * The key ins is changed to record the hole:
5568 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005569 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005570 * ins->offset == number of blocks
5571 * Any available blocks before search_start are skipped.
5572 */
Chris Masond3977122009-01-05 21:25:51 -05005573static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005574 struct btrfs_root *orig_root,
5575 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005576 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005577 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005578{
Josef Bacik80eb2342008-10-29 14:49:05 -04005579 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005580 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005581 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005582 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005583 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005584 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005585 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005586 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005587 int loop = 0;
Liu Boac5c9302012-12-27 09:01:24 +00005588 int index = __get_raid_index(data);
Josef Bacikfb25e912011-07-26 17:00:46 -04005589 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5590 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005591 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005592 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005593 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005594 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005595 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005596
Chris Masondb945352007-10-15 16:15:53 -04005597 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005598 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005599 ins->objectid = 0;
5600 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005601
Josef Bacik3f7de032011-11-10 08:29:20 -05005602 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5603
Josef Bacik2552d172009-04-03 10:14:19 -04005604 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005605 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00005606 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005607 return -ENOSPC;
5608 }
Josef Bacik2552d172009-04-03 10:14:19 -04005609
Josef Bacik67377732010-09-16 16:19:09 -04005610 /*
5611 * If the space info is for both data and metadata it means we have a
5612 * small filesystem and we can't use the clustering stuff.
5613 */
5614 if (btrfs_mixed_space_info(space_info))
5615 use_cluster = false;
5616
Josef Bacik67377732010-09-16 16:19:09 -04005617 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005618 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005619 if (!btrfs_test_opt(root, SSD))
5620 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005621 }
5622
Josef Bacik67377732010-09-16 16:19:09 -04005623 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5624 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005625 last_ptr = &root->fs_info->data_alloc_cluster;
5626 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005627
Chris Mason239b14b2008-03-24 15:02:07 -04005628 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005629 spin_lock(&last_ptr->lock);
5630 if (last_ptr->block_group)
5631 hint_byte = last_ptr->window_start;
5632 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005633 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005634
Chris Masona061fc82008-05-07 11:43:44 -04005635 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005636 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005637
Josef Bacik817d52f2009-07-13 21:29:25 -04005638 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005639 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005640
Josef Bacik2552d172009-04-03 10:14:19 -04005641 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005642 block_group = btrfs_lookup_block_group(root->fs_info,
5643 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005644 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005645 /*
5646 * we don't want to use the block group if it doesn't match our
5647 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005648 *
5649 * However if we are re-searching with an ideal block group
5650 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005651 */
5652 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005653 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005654 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005655 if (list_empty(&block_group->list) ||
5656 block_group->ro) {
5657 /*
5658 * someone is removing this block group,
5659 * we can't jump into the have_block_group
5660 * target because our list pointers are not
5661 * valid
5662 */
5663 btrfs_put_block_group(block_group);
5664 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005665 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005666 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005667 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005668 }
Josef Bacik2552d172009-04-03 10:14:19 -04005669 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005670 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005671 }
Chris Mason42e70e72008-11-07 18:17:11 -05005672 }
Josef Bacik2552d172009-04-03 10:14:19 -04005673search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005674 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005675 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005676 list_for_each_entry(block_group, &space_info->block_groups[index],
5677 list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005678 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005679 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005680
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005681 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005682 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005683 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005684
Chris Mason83a50de2010-12-13 15:06:46 -05005685 /*
5686 * this can happen if we end up cycling through all the
5687 * raid types, but we want to make sure we only allocate
5688 * for the proper type.
5689 */
5690 if (!block_group_bits(block_group, data)) {
5691 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5692 BTRFS_BLOCK_GROUP_RAID1 |
5693 BTRFS_BLOCK_GROUP_RAID10;
5694
5695 /*
5696 * if they asked for extra copies and this block group
5697 * doesn't provide them, bail. This does allow us to
5698 * fill raid0 from raid1.
5699 */
5700 if ((data & extra) && !(block_group->flags & extra))
5701 goto loop;
5702 }
5703
Josef Bacik2552d172009-04-03 10:14:19 -04005704have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05005705 cached = block_group_cache_done(block_group);
5706 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05005707 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00005708 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005709 BUG_ON(ret < 0);
5710 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005711 }
5712
Josef Bacikea6a4782008-11-20 12:16:16 -05005713 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005714 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005715
Josef Bacik0a243252009-09-11 16:11:20 -04005716 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005717 * Ok we want to try and use the cluster allocator, so
5718 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005719 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005720 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005721 /*
5722 * the refill lock keeps out other
5723 * people trying to start a new cluster
5724 */
5725 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005726 used_block_group = last_ptr->block_group;
5727 if (used_block_group != block_group &&
5728 (!used_block_group ||
5729 used_block_group->ro ||
5730 !block_group_bits(used_block_group, data))) {
5731 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04005732 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005733 }
Chris Mason44fb5512009-06-04 15:34:51 -04005734
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005735 if (used_block_group != block_group)
5736 btrfs_get_block_group(used_block_group);
5737
5738 offset = btrfs_alloc_from_cluster(used_block_group,
5739 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005740 if (offset) {
5741 /* we have a block, we're done */
5742 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005743 trace_btrfs_reserve_extent_cluster(root,
5744 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005745 goto checks;
5746 }
5747
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005748 WARN_ON(last_ptr->block_group != used_block_group);
5749 if (used_block_group != block_group) {
5750 btrfs_put_block_group(used_block_group);
5751 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005752 }
Chris Mason44fb5512009-06-04 15:34:51 -04005753refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005754 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005755 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5756 * set up a new clusters, so lets just skip it
5757 * and let the allocator find whatever block
5758 * it can find. If we reach this point, we
5759 * will have tried the cluster allocator
5760 * plenty of times and not have found
5761 * anything, so we are likely way too
5762 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005763 * anything.
5764 *
5765 * However, if the cluster is taken from the
5766 * current block group, release the cluster
5767 * first, so that we stand a better chance of
5768 * succeeding in the unclustered
5769 * allocation. */
5770 if (loop >= LOOP_NO_EMPTY_SIZE &&
5771 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005772 spin_unlock(&last_ptr->refill_lock);
5773 goto unclustered_alloc;
5774 }
5775
Chris Masonfa9c0d792009-04-03 09:47:43 -04005776 /*
5777 * this cluster didn't work out, free it and
5778 * start over
5779 */
5780 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5781
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005782 if (loop >= LOOP_NO_EMPTY_SIZE) {
5783 spin_unlock(&last_ptr->refill_lock);
5784 goto unclustered_alloc;
5785 }
5786
Chris Masonfa9c0d792009-04-03 09:47:43 -04005787 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005788 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005789 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05005790 search_start, num_bytes,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005791 empty_cluster + empty_size);
5792 if (ret == 0) {
5793 /*
5794 * now pull our allocation out of this
5795 * cluster
5796 */
5797 offset = btrfs_alloc_from_cluster(block_group,
5798 last_ptr, num_bytes,
5799 search_start);
5800 if (offset) {
5801 /* we found one, proceed */
5802 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005803 trace_btrfs_reserve_extent_cluster(root,
5804 block_group, search_start,
5805 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005806 goto checks;
5807 }
Josef Bacik0a243252009-09-11 16:11:20 -04005808 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5809 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005810 spin_unlock(&last_ptr->refill_lock);
5811
Josef Bacik0a243252009-09-11 16:11:20 -04005812 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005813 wait_block_group_cache_progress(block_group,
5814 num_bytes + empty_cluster + empty_size);
5815 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005816 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005817
Chris Masonfa9c0d792009-04-03 09:47:43 -04005818 /*
5819 * at this point we either didn't find a cluster
5820 * or we weren't able to allocate a block from our
5821 * cluster. Free the cluster we've been trying
5822 * to use, and go to the next block group
5823 */
Josef Bacik0a243252009-09-11 16:11:20 -04005824 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005825 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005826 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005827 }
5828
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005829unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005830 spin_lock(&block_group->free_space_ctl->tree_lock);
5831 if (cached &&
5832 block_group->free_space_ctl->free_space <
5833 num_bytes + empty_cluster + empty_size) {
5834 spin_unlock(&block_group->free_space_ctl->tree_lock);
5835 goto loop;
5836 }
5837 spin_unlock(&block_group->free_space_ctl->tree_lock);
5838
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005839 offset = btrfs_find_space_for_alloc(block_group, search_start,
5840 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005841 /*
5842 * If we didn't find a chunk, and we haven't failed on this
5843 * block group before, and this block group is in the middle of
5844 * caching and we are ok with waiting, then go ahead and wait
5845 * for progress to be made, and set failed_alloc to true.
5846 *
5847 * If failed_alloc is true then we've already waited on this
5848 * block group once and should move on to the next block group.
5849 */
5850 if (!offset && !failed_alloc && !cached &&
5851 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005852 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005853 num_bytes + empty_size);
5854 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005855 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005856 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08005857 if (!cached)
5858 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005859 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005860 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005861checks:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005862 search_start = stripe_align(root, offset);
Chris Masone37c9e62007-05-09 20:13:14 -04005863
Josef Bacik2552d172009-04-03 10:14:19 -04005864 /* move on to the next group */
5865 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005866 used_block_group->key.objectid + used_block_group->key.offset) {
5867 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005868 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005869 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005870
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005871 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005872 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005873 search_start - offset);
5874 BUG_ON(offset > search_start);
5875
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005876 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04005877 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005878 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005879 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005880 goto loop;
5881 }
Yan Zheng11833d62009-09-11 16:11:19 -04005882
Josef Bacik2552d172009-04-03 10:14:19 -04005883 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005884 ins->objectid = search_start;
5885 ins->offset = num_bytes;
5886
Josef Bacik3f7de032011-11-10 08:29:20 -05005887 trace_btrfs_reserve_extent(orig_root, block_group,
5888 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005889 if (used_block_group != block_group)
5890 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005891 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005892 break;
5893loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005894 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005895 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005896 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005897 if (used_block_group != block_group)
5898 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005899 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005900 }
5901 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005902
Miao Xie60d2adb2011-09-09 17:34:35 +08005903 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
5904 goto search;
5905
Yan, Zhengb742bb82010-05-16 10:46:24 -04005906 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5907 goto search;
5908
Josef Bacik285ff5a2012-01-13 15:27:45 -05005909 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005910 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5911 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005912 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5913 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5914 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5915 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005916 */
Josef Bacik723bda22011-05-27 16:11:38 -04005917 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005918 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04005919 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005920 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04005921 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04005922 CHUNK_ALLOC_FORCE);
5923 /*
5924 * Do not bail out on ENOSPC since we
5925 * can do more things.
5926 */
5927 if (ret < 0 && ret != -ENOSPC) {
5928 btrfs_abort_transaction(trans,
5929 root, ret);
5930 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04005931 }
Josef Bacik723bda22011-05-27 16:11:38 -04005932 }
5933
5934 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005935 empty_size = 0;
5936 empty_cluster = 0;
5937 }
Chris Mason42e70e72008-11-07 18:17:11 -05005938
Josef Bacik723bda22011-05-27 16:11:38 -04005939 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005940 } else if (!ins->objectid) {
5941 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005942 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005943 ret = 0;
5944 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005945out:
Chris Mason0b86a832008-03-24 15:01:56 -04005946
Chris Mason0f70abe2007-02-28 16:46:22 -05005947 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005948}
Chris Masonec44a352008-04-28 15:29:52 -04005949
Josef Bacik9ed74f22009-09-11 16:12:44 -04005950static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5951 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005952{
5953 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005954 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005955
Josef Bacik9ed74f22009-09-11 16:12:44 -04005956 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04005957 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
5958 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05005959 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005960 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005961 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005962 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005963 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5964 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005965 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005966 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005967 (unsigned long long)info->bytes_pinned,
5968 (unsigned long long)info->bytes_reserved,
5969 (unsigned long long)info->bytes_may_use,
5970 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005971 spin_unlock(&info->lock);
5972
5973 if (!dump_block_groups)
5974 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005975
Josef Bacik80eb2342008-10-29 14:49:05 -04005976 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005977again:
5978 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005979 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06005980 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05005981 (unsigned long long)cache->key.objectid,
5982 (unsigned long long)cache->key.offset,
5983 (unsigned long long)btrfs_block_group_used(&cache->item),
5984 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06005985 (unsigned long long)cache->reserved,
5986 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04005987 btrfs_dump_free_space(cache, bytes);
5988 spin_unlock(&cache->lock);
5989 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005990 if (++index < BTRFS_NR_RAID_TYPES)
5991 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005992 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005993}
Zheng Yane8569812008-09-26 10:05:48 -04005994
Yan Zheng11833d62009-09-11 16:11:19 -04005995int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5996 struct btrfs_root *root,
5997 u64 num_bytes, u64 min_alloc_size,
5998 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005999 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05006000{
Miao Xie9e622d62012-01-26 15:01:12 -05006001 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006002 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006003
Josef Bacik6a632092009-02-20 11:00:09 -05006004 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04006005again:
Chris Masondb945352007-10-15 16:15:53 -04006006 WARN_ON(num_bytes < root->sectorsize);
6007 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006008 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04006009
Miao Xie9e622d62012-01-26 15:01:12 -05006010 if (ret == -ENOSPC) {
6011 if (!final_tried) {
6012 num_bytes = num_bytes >> 1;
6013 num_bytes = num_bytes & ~(root->sectorsize - 1);
6014 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006015 if (num_bytes == min_alloc_size)
6016 final_tried = true;
6017 goto again;
6018 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6019 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006020
Miao Xie9e622d62012-01-26 15:01:12 -05006021 sinfo = __find_space_info(root->fs_info, data);
6022 printk(KERN_ERR "btrfs allocation failed flags %llu, "
6023 "wanted %llu\n", (unsigned long long)data,
6024 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006025 if (sinfo)
6026 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006027 }
Chris Mason925baed2008-06-25 16:01:30 -04006028 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006029
liubo1abe9b82011-03-24 11:18:59 +00006030 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6031
Josef Bacik0f9dd462008-09-23 13:14:11 -04006032 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006033}
6034
Chris Masone688b7252011-10-31 20:52:39 -04006035static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6036 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006037{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006038 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006039 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006040
Josef Bacik0f9dd462008-09-23 13:14:11 -04006041 cache = btrfs_lookup_block_group(root->fs_info, start);
6042 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05006043 printk(KERN_ERR "Unable to find block group for %llu\n",
6044 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006045 return -ENOSPC;
6046 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006047
Li Dongyang5378e602011-03-24 10:24:27 +00006048 if (btrfs_test_opt(root, DISCARD))
6049 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006050
Chris Masone688b7252011-10-31 20:52:39 -04006051 if (pin)
6052 pin_down_extent(root, cache, start, len, 1);
6053 else {
6054 btrfs_add_free_space(cache, start, len);
6055 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6056 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006057 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006058
liubo1abe9b82011-03-24 11:18:59 +00006059 trace_btrfs_reserved_extent_free(root, start, len);
6060
Chris Masone6dcd2d2008-07-17 12:53:50 -04006061 return ret;
6062}
6063
Chris Masone688b7252011-10-31 20:52:39 -04006064int btrfs_free_reserved_extent(struct btrfs_root *root,
6065 u64 start, u64 len)
6066{
6067 return __btrfs_free_reserved_extent(root, start, len, 0);
6068}
6069
6070int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6071 u64 start, u64 len)
6072{
6073 return __btrfs_free_reserved_extent(root, start, len, 1);
6074}
6075
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006076static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6077 struct btrfs_root *root,
6078 u64 parent, u64 root_objectid,
6079 u64 flags, u64 owner, u64 offset,
6080 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006081{
6082 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006083 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006084 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006085 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006086 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006087 struct extent_buffer *leaf;
6088 int type;
6089 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006090
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006091 if (parent > 0)
6092 type = BTRFS_SHARED_DATA_REF_KEY;
6093 else
6094 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006095
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006096 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006097
6098 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006099 if (!path)
6100 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006101
Chris Masonb9473432009-03-13 11:00:37 -04006102 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006103 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6104 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006105 if (ret) {
6106 btrfs_free_path(path);
6107 return ret;
6108 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006109
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006110 leaf = path->nodes[0];
6111 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006112 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006113 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6114 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6115 btrfs_set_extent_flags(leaf, extent_item,
6116 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006117
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006118 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6119 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6120 if (parent > 0) {
6121 struct btrfs_shared_data_ref *ref;
6122 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6123 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6124 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6125 } else {
6126 struct btrfs_extent_data_ref *ref;
6127 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6128 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6129 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6130 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6131 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6132 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006133
6134 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006135 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006136
Yan, Zhengf0486c62010-05-16 10:46:25 -04006137 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006138 if (ret) { /* -ENOENT, logic error */
Chris Masond3977122009-01-05 21:25:51 -05006139 printk(KERN_ERR "btrfs update block group failed for %llu "
6140 "%llu\n", (unsigned long long)ins->objectid,
6141 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006142 BUG();
6143 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006144 return ret;
6145}
6146
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006147static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6148 struct btrfs_root *root,
6149 u64 parent, u64 root_objectid,
6150 u64 flags, struct btrfs_disk_key *key,
6151 int level, struct btrfs_key *ins)
6152{
6153 int ret;
6154 struct btrfs_fs_info *fs_info = root->fs_info;
6155 struct btrfs_extent_item *extent_item;
6156 struct btrfs_tree_block_info *block_info;
6157 struct btrfs_extent_inline_ref *iref;
6158 struct btrfs_path *path;
6159 struct extent_buffer *leaf;
6160 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
6161
6162 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006163 if (!path)
6164 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006165
6166 path->leave_spinning = 1;
6167 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6168 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006169 if (ret) {
6170 btrfs_free_path(path);
6171 return ret;
6172 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006173
6174 leaf = path->nodes[0];
6175 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6176 struct btrfs_extent_item);
6177 btrfs_set_extent_refs(leaf, extent_item, 1);
6178 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6179 btrfs_set_extent_flags(leaf, extent_item,
6180 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6181 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6182
6183 btrfs_set_tree_block_key(leaf, block_info, key);
6184 btrfs_set_tree_block_level(leaf, block_info, level);
6185
6186 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6187 if (parent > 0) {
6188 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6189 btrfs_set_extent_inline_ref_type(leaf, iref,
6190 BTRFS_SHARED_BLOCK_REF_KEY);
6191 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6192 } else {
6193 btrfs_set_extent_inline_ref_type(leaf, iref,
6194 BTRFS_TREE_BLOCK_REF_KEY);
6195 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6196 }
6197
6198 btrfs_mark_buffer_dirty(leaf);
6199 btrfs_free_path(path);
6200
Yan, Zhengf0486c62010-05-16 10:46:25 -04006201 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006202 if (ret) { /* -ENOENT, logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006203 printk(KERN_ERR "btrfs update block group failed for %llu "
6204 "%llu\n", (unsigned long long)ins->objectid,
6205 (unsigned long long)ins->offset);
6206 BUG();
6207 }
6208 return ret;
6209}
6210
6211int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6212 struct btrfs_root *root,
6213 u64 root_objectid, u64 owner,
6214 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006215{
6216 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006217
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006218 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006219
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006220 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6221 ins->offset, 0,
6222 root_objectid, owner, offset,
6223 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006224 return ret;
6225}
Chris Masone02119d2008-09-05 16:13:11 -04006226
6227/*
6228 * this is used by the tree logging recovery code. It records that
6229 * an extent has been allocated and makes sure to clear the free
6230 * space cache bits as well
6231 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006232int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6233 struct btrfs_root *root,
6234 u64 root_objectid, u64 owner, u64 offset,
6235 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006236{
6237 int ret;
6238 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006239 struct btrfs_caching_control *caching_ctl;
6240 u64 start = ins->objectid;
6241 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006242
Chris Masone02119d2008-09-05 16:13:11 -04006243 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Liu Bof6373bf2012-12-27 09:01:18 +00006244 cache_block_group(block_group, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006245 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006246
Yan Zheng11833d62009-09-11 16:11:19 -04006247 if (!caching_ctl) {
6248 BUG_ON(!block_group_cache_done(block_group));
6249 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006250 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006251 } else {
6252 mutex_lock(&caching_ctl->mutex);
6253
6254 if (start >= caching_ctl->progress) {
6255 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006256 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006257 } else if (start + num_bytes <= caching_ctl->progress) {
6258 ret = btrfs_remove_free_space(block_group,
6259 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006260 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006261 } else {
6262 num_bytes = caching_ctl->progress - start;
6263 ret = btrfs_remove_free_space(block_group,
6264 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006265 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006266
6267 start = caching_ctl->progress;
6268 num_bytes = ins->objectid + ins->offset -
6269 caching_ctl->progress;
6270 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006271 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006272 }
6273
6274 mutex_unlock(&caching_ctl->mutex);
6275 put_caching_control(caching_ctl);
6276 }
6277
Josef Bacikfb25e912011-07-26 17:00:46 -04006278 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6279 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d792009-04-03 09:47:43 -04006281 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006282 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6283 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006284 return ret;
6285}
6286
Chris Mason65b51a02008-08-01 15:11:20 -04006287struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6288 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006289 u64 bytenr, u32 blocksize,
6290 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006291{
6292 struct extent_buffer *buf;
6293
6294 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6295 if (!buf)
6296 return ERR_PTR(-ENOMEM);
6297 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006298 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006299 btrfs_tree_lock(buf);
6300 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006301 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006302
6303 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006304 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006305
Chris Masond0c803c2008-09-11 16:17:57 -04006306 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006307 /*
6308 * we allow two log transactions at a time, use different
6309 * EXENT bit to differentiate dirty pages.
6310 */
6311 if (root->log_transid % 2 == 0)
6312 set_extent_dirty(&root->dirty_log_pages, buf->start,
6313 buf->start + buf->len - 1, GFP_NOFS);
6314 else
6315 set_extent_new(&root->dirty_log_pages, buf->start,
6316 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006317 } else {
6318 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6319 buf->start + buf->len - 1, GFP_NOFS);
6320 }
Chris Mason65b51a02008-08-01 15:11:20 -04006321 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006322 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006323 return buf;
6324}
6325
Yan, Zhengf0486c62010-05-16 10:46:25 -04006326static struct btrfs_block_rsv *
6327use_block_rsv(struct btrfs_trans_handle *trans,
6328 struct btrfs_root *root, u32 blocksize)
6329{
6330 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006331 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006332 int ret;
6333
6334 block_rsv = get_block_rsv(trans, root);
6335
6336 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006337 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6338 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006339 /*
6340 * If we couldn't reserve metadata bytes try and use some from
6341 * the global reserve.
6342 */
6343 if (ret && block_rsv != global_rsv) {
6344 ret = block_rsv_use_bytes(global_rsv, blocksize);
6345 if (!ret)
6346 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006347 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006348 } else if (ret) {
6349 return ERR_PTR(ret);
6350 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006351 return block_rsv;
6352 }
6353
6354 ret = block_rsv_use_bytes(block_rsv, blocksize);
6355 if (!ret)
6356 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006357 if (ret && !block_rsv->failfast) {
David Sterbadff51cd2011-06-14 12:52:17 +02006358 static DEFINE_RATELIMIT_STATE(_rs,
6359 DEFAULT_RATELIMIT_INTERVAL,
6360 /*DEFAULT_RATELIMIT_BURST*/ 2);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006361 if (__ratelimit(&_rs))
6362 WARN(1, KERN_DEBUG "btrfs: block rsv returned %d\n",
6363 ret);
Miao Xie08e007d2012-10-16 11:33:38 +00006364 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6365 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006366 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006367 return block_rsv;
6368 } else if (ret && block_rsv != global_rsv) {
6369 ret = block_rsv_use_bytes(global_rsv, blocksize);
6370 if (!ret)
6371 return global_rsv;
6372 }
6373 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006374
Yan, Zhengf0486c62010-05-16 10:46:25 -04006375 return ERR_PTR(-ENOSPC);
6376}
6377
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006378static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6379 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006380{
6381 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006382 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006383}
6384
Chris Masonfec577f2007-02-26 10:40:21 -05006385/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006386 * finds a free extent and does all the dirty work required for allocation
6387 * returns the key for the extent through ins, and a tree buffer for
6388 * the first block of the extent through buf.
6389 *
Chris Masonfec577f2007-02-26 10:40:21 -05006390 * returns the tree buffer or NULL.
6391 */
Chris Mason5f39d392007-10-15 16:14:19 -04006392struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006393 struct btrfs_root *root, u32 blocksize,
6394 u64 parent, u64 root_objectid,
6395 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006396 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006397{
Chris Masone2fa7222007-03-12 16:22:34 -04006398 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006399 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006400 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006401 u64 flags = 0;
6402 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006403
Yan, Zhengf0486c62010-05-16 10:46:25 -04006404
6405 block_rsv = use_block_rsv(trans, root, blocksize);
6406 if (IS_ERR(block_rsv))
6407 return ERR_CAST(block_rsv);
6408
6409 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006410 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006411 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006412 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006413 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006414 }
Chris Mason55c69072008-01-09 15:55:33 -05006415
Chris Mason4008c042009-02-12 14:09:45 -05006416 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6417 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006418 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006419
6420 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6421 if (parent == 0)
6422 parent = ins.objectid;
6423 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6424 } else
6425 BUG_ON(parent > 0);
6426
6427 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6428 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006429 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006430 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006431 if (key)
6432 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6433 else
6434 memset(&extent_op->key, 0, sizeof(extent_op->key));
6435 extent_op->flags_to_set = flags;
6436 extent_op->update_key = 1;
6437 extent_op->update_flags = 1;
6438 extent_op->is_data = 0;
6439
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006440 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6441 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006442 ins.offset, parent, root_objectid,
6443 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006444 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006445 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006446 }
Chris Masonfec577f2007-02-26 10:40:21 -05006447 return buf;
6448}
Chris Masona28ec192007-03-06 20:08:01 -05006449
Yan Zheng2c47e6052009-06-27 21:07:35 -04006450struct walk_control {
6451 u64 refs[BTRFS_MAX_LEVEL];
6452 u64 flags[BTRFS_MAX_LEVEL];
6453 struct btrfs_key update_progress;
6454 int stage;
6455 int level;
6456 int shared_level;
6457 int update_ref;
6458 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006459 int reada_slot;
6460 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006461 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006462};
6463
6464#define DROP_REFERENCE 1
6465#define UPDATE_BACKREF 2
6466
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006467static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6468 struct btrfs_root *root,
6469 struct walk_control *wc,
6470 struct btrfs_path *path)
6471{
6472 u64 bytenr;
6473 u64 generation;
6474 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006475 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006476 u32 nritems;
6477 u32 blocksize;
6478 struct btrfs_key key;
6479 struct extent_buffer *eb;
6480 int ret;
6481 int slot;
6482 int nread = 0;
6483
6484 if (path->slots[wc->level] < wc->reada_slot) {
6485 wc->reada_count = wc->reada_count * 2 / 3;
6486 wc->reada_count = max(wc->reada_count, 2);
6487 } else {
6488 wc->reada_count = wc->reada_count * 3 / 2;
6489 wc->reada_count = min_t(int, wc->reada_count,
6490 BTRFS_NODEPTRS_PER_BLOCK(root));
6491 }
6492
6493 eb = path->nodes[wc->level];
6494 nritems = btrfs_header_nritems(eb);
6495 blocksize = btrfs_level_size(root, wc->level - 1);
6496
6497 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6498 if (nread >= wc->reada_count)
6499 break;
6500
6501 cond_resched();
6502 bytenr = btrfs_node_blockptr(eb, slot);
6503 generation = btrfs_node_ptr_generation(eb, slot);
6504
6505 if (slot == path->slots[wc->level])
6506 goto reada;
6507
6508 if (wc->stage == UPDATE_BACKREF &&
6509 generation <= root->root_key.offset)
6510 continue;
6511
Yan, Zheng94fcca92009-10-09 09:25:16 -04006512 /* We don't lock the tree block, it's OK to be racy here */
6513 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6514 &refs, &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006515 /* We don't care about errors in readahead. */
6516 if (ret < 0)
6517 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006518 BUG_ON(refs == 0);
6519
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006520 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006521 if (refs == 1)
6522 goto reada;
6523
Yan, Zheng94fcca92009-10-09 09:25:16 -04006524 if (wc->level == 1 &&
6525 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6526 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006527 if (!wc->update_ref ||
6528 generation <= root->root_key.offset)
6529 continue;
6530 btrfs_node_key_to_cpu(eb, &key, slot);
6531 ret = btrfs_comp_cpu_keys(&key,
6532 &wc->update_progress);
6533 if (ret < 0)
6534 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006535 } else {
6536 if (wc->level == 1 &&
6537 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6538 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006539 }
6540reada:
6541 ret = readahead_tree_block(root, bytenr, blocksize,
6542 generation);
6543 if (ret)
6544 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006545 nread++;
6546 }
6547 wc->reada_slot = slot;
6548}
6549
Chris Mason9aca1d52007-03-13 11:09:37 -04006550/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006551 * hepler to process tree block while walking down the tree.
6552 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006553 * when wc->stage == UPDATE_BACKREF, this function updates
6554 * back refs for pointers in the block.
6555 *
6556 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006557 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006558static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6559 struct btrfs_root *root,
6560 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006561 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006562{
6563 int level = wc->level;
6564 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006565 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6566 int ret;
6567
6568 if (wc->stage == UPDATE_BACKREF &&
6569 btrfs_header_owner(eb) != root->root_key.objectid)
6570 return 1;
6571
6572 /*
6573 * when reference count of tree block is 1, it won't increase
6574 * again. once full backref flag is set, we never clear it.
6575 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006576 if (lookup_info &&
6577 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6578 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006579 BUG_ON(!path->locks[level]);
6580 ret = btrfs_lookup_extent_info(trans, root,
6581 eb->start, eb->len,
6582 &wc->refs[level],
6583 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006584 BUG_ON(ret == -ENOMEM);
6585 if (ret)
6586 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006587 BUG_ON(wc->refs[level] == 0);
6588 }
6589
Yan Zheng2c47e6052009-06-27 21:07:35 -04006590 if (wc->stage == DROP_REFERENCE) {
6591 if (wc->refs[level] > 1)
6592 return 1;
6593
6594 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006595 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006596 path->locks[level] = 0;
6597 }
6598 return 0;
6599 }
6600
6601 /* wc->stage == UPDATE_BACKREF */
6602 if (!(wc->flags[level] & flag)) {
6603 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006604 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006605 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006606 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006607 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006608 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6609 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006610 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006611 wc->flags[level] |= flag;
6612 }
6613
6614 /*
6615 * the block is shared by multiple trees, so it's not good to
6616 * keep the tree lock
6617 */
6618 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006619 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006620 path->locks[level] = 0;
6621 }
6622 return 0;
6623}
6624
6625/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006626 * hepler to process tree block pointer.
6627 *
6628 * when wc->stage == DROP_REFERENCE, this function checks
6629 * reference count of the block pointed to. if the block
6630 * is shared and we need update back refs for the subtree
6631 * rooted at the block, this function changes wc->stage to
6632 * UPDATE_BACKREF. if the block is shared and there is no
6633 * need to update back, this function drops the reference
6634 * to the block.
6635 *
6636 * NOTE: return value 1 means we should stop walking down.
6637 */
6638static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6639 struct btrfs_root *root,
6640 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006641 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006642{
6643 u64 bytenr;
6644 u64 generation;
6645 u64 parent;
6646 u32 blocksize;
6647 struct btrfs_key key;
6648 struct extent_buffer *next;
6649 int level = wc->level;
6650 int reada = 0;
6651 int ret = 0;
6652
6653 generation = btrfs_node_ptr_generation(path->nodes[level],
6654 path->slots[level]);
6655 /*
6656 * if the lower level block was created before the snapshot
6657 * was created, we know there is no need to update back refs
6658 * for the subtree
6659 */
6660 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006661 generation <= root->root_key.offset) {
6662 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006663 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006664 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006665
6666 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6667 blocksize = btrfs_level_size(root, level - 1);
6668
6669 next = btrfs_find_tree_block(root, bytenr, blocksize);
6670 if (!next) {
6671 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00006672 if (!next)
6673 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006674 reada = 1;
6675 }
6676 btrfs_tree_lock(next);
6677 btrfs_set_lock_blocking(next);
6678
Yan, Zheng94fcca92009-10-09 09:25:16 -04006679 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6680 &wc->refs[level - 1],
6681 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006682 if (ret < 0) {
6683 btrfs_tree_unlock(next);
6684 return ret;
6685 }
6686
Yan, Zheng94fcca92009-10-09 09:25:16 -04006687 BUG_ON(wc->refs[level - 1] == 0);
6688 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006689
Yan, Zheng94fcca92009-10-09 09:25:16 -04006690 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006691 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006692 if (level == 1 &&
6693 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6694 goto skip;
6695
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006696 if (!wc->update_ref ||
6697 generation <= root->root_key.offset)
6698 goto skip;
6699
6700 btrfs_node_key_to_cpu(path->nodes[level], &key,
6701 path->slots[level]);
6702 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6703 if (ret < 0)
6704 goto skip;
6705
6706 wc->stage = UPDATE_BACKREF;
6707 wc->shared_level = level - 1;
6708 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006709 } else {
6710 if (level == 1 &&
6711 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6712 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006713 }
6714
Chris Masonb9fab912012-05-06 07:23:47 -04006715 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006716 btrfs_tree_unlock(next);
6717 free_extent_buffer(next);
6718 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006719 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006720 }
6721
6722 if (!next) {
6723 if (reada && level == 1)
6724 reada_walk_down(trans, root, wc, path);
6725 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006726 if (!next)
6727 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006728 btrfs_tree_lock(next);
6729 btrfs_set_lock_blocking(next);
6730 }
6731
6732 level--;
6733 BUG_ON(level != btrfs_header_level(next));
6734 path->nodes[level] = next;
6735 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006736 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006737 wc->level = level;
6738 if (wc->level == 1)
6739 wc->reada_slot = 0;
6740 return 0;
6741skip:
6742 wc->refs[level - 1] = 0;
6743 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006744 if (wc->stage == DROP_REFERENCE) {
6745 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6746 parent = path->nodes[level]->start;
6747 } else {
6748 BUG_ON(root->root_key.objectid !=
6749 btrfs_header_owner(path->nodes[level]));
6750 parent = 0;
6751 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006752
Yan, Zheng94fcca92009-10-09 09:25:16 -04006753 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006754 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006755 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006756 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006757 btrfs_tree_unlock(next);
6758 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006759 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006760 return 1;
6761}
6762
6763/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006764 * hepler to process tree block while walking up the tree.
6765 *
6766 * when wc->stage == DROP_REFERENCE, this function drops
6767 * reference count on the block.
6768 *
6769 * when wc->stage == UPDATE_BACKREF, this function changes
6770 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6771 * to UPDATE_BACKREF previously while processing the block.
6772 *
6773 * NOTE: return value 1 means we should stop walking up.
6774 */
6775static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6776 struct btrfs_root *root,
6777 struct btrfs_path *path,
6778 struct walk_control *wc)
6779{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006780 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006781 int level = wc->level;
6782 struct extent_buffer *eb = path->nodes[level];
6783 u64 parent = 0;
6784
6785 if (wc->stage == UPDATE_BACKREF) {
6786 BUG_ON(wc->shared_level < level);
6787 if (level < wc->shared_level)
6788 goto out;
6789
Yan Zheng2c47e6052009-06-27 21:07:35 -04006790 ret = find_next_key(path, level + 1, &wc->update_progress);
6791 if (ret > 0)
6792 wc->update_ref = 0;
6793
6794 wc->stage = DROP_REFERENCE;
6795 wc->shared_level = -1;
6796 path->slots[level] = 0;
6797
6798 /*
6799 * check reference count again if the block isn't locked.
6800 * we should start walking down the tree again if reference
6801 * count is one.
6802 */
6803 if (!path->locks[level]) {
6804 BUG_ON(level == 0);
6805 btrfs_tree_lock(eb);
6806 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006807 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006808
6809 ret = btrfs_lookup_extent_info(trans, root,
6810 eb->start, eb->len,
6811 &wc->refs[level],
6812 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006813 if (ret < 0) {
6814 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006815 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006816 return ret;
6817 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006818 BUG_ON(wc->refs[level] == 0);
6819 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006820 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006821 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006822 return 1;
6823 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006824 }
6825 }
6826
6827 /* wc->stage == DROP_REFERENCE */
6828 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6829
6830 if (wc->refs[level] == 1) {
6831 if (level == 0) {
6832 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006833 ret = btrfs_dec_ref(trans, root, eb, 1,
6834 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006835 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006836 ret = btrfs_dec_ref(trans, root, eb, 0,
6837 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006838 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006839 }
6840 /* make block locked assertion in clean_tree_block happy */
6841 if (!path->locks[level] &&
6842 btrfs_header_generation(eb) == trans->transid) {
6843 btrfs_tree_lock(eb);
6844 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006845 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006846 }
6847 clean_tree_block(trans, root, eb);
6848 }
6849
6850 if (eb == root->node) {
6851 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6852 parent = eb->start;
6853 else
6854 BUG_ON(root->root_key.objectid !=
6855 btrfs_header_owner(eb));
6856 } else {
6857 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6858 parent = path->nodes[level + 1]->start;
6859 else
6860 BUG_ON(root->root_key.objectid !=
6861 btrfs_header_owner(path->nodes[level + 1]));
6862 }
6863
Jan Schmidt5581a512012-05-16 17:04:52 +02006864 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006865out:
6866 wc->refs[level] = 0;
6867 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006868 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006869}
6870
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006871static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6872 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006873 struct btrfs_path *path,
6874 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006875{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006876 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006877 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006878 int ret;
6879
Yan Zheng2c47e6052009-06-27 21:07:35 -04006880 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006881 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006882 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006883 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006884
Yan Zheng2c47e6052009-06-27 21:07:35 -04006885 if (level == 0)
6886 break;
6887
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006888 if (path->slots[level] >=
6889 btrfs_header_nritems(path->nodes[level]))
6890 break;
6891
Yan, Zheng94fcca92009-10-09 09:25:16 -04006892 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006893 if (ret > 0) {
6894 path->slots[level]++;
6895 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006896 } else if (ret < 0)
6897 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006898 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006899 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006900 return 0;
6901}
6902
Chris Masond3977122009-01-05 21:25:51 -05006903static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006904 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006905 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006906 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006907{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006908 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006909 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006910
Yan Zheng2c47e6052009-06-27 21:07:35 -04006911 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6912 while (level < max_level && path->nodes[level]) {
6913 wc->level = level;
6914 if (path->slots[level] + 1 <
6915 btrfs_header_nritems(path->nodes[level])) {
6916 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006917 return 0;
6918 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006919 ret = walk_up_proc(trans, root, path, wc);
6920 if (ret > 0)
6921 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006922
Yan Zheng2c47e6052009-06-27 21:07:35 -04006923 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006924 btrfs_tree_unlock_rw(path->nodes[level],
6925 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006926 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006927 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006928 free_extent_buffer(path->nodes[level]);
6929 path->nodes[level] = NULL;
6930 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006931 }
6932 }
6933 return 1;
6934}
6935
Chris Mason9aca1d52007-03-13 11:09:37 -04006936/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006937 * drop a subvolume tree.
6938 *
6939 * this function traverses the tree freeing any blocks that only
6940 * referenced by the tree.
6941 *
6942 * when a shared tree block is found. this function decreases its
6943 * reference count by one. if update_ref is true, this function
6944 * also make sure backrefs for the shared block and all lower level
6945 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006946 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04006947int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006948 struct btrfs_block_rsv *block_rsv, int update_ref,
6949 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05006950{
Chris Mason5caf2a02007-04-02 11:20:42 -04006951 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006952 struct btrfs_trans_handle *trans;
6953 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006954 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006955 struct walk_control *wc;
6956 struct btrfs_key key;
6957 int err = 0;
6958 int ret;
6959 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006960
Chris Mason5caf2a02007-04-02 11:20:42 -04006961 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006962 if (!path) {
6963 err = -ENOMEM;
6964 goto out;
6965 }
Chris Mason20524f02007-03-10 06:35:47 -05006966
Yan Zheng2c47e6052009-06-27 21:07:35 -04006967 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07006968 if (!wc) {
6969 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006970 err = -ENOMEM;
6971 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07006972 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006973
Yan, Zhenga22285a2010-05-16 10:48:46 -04006974 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006975 if (IS_ERR(trans)) {
6976 err = PTR_ERR(trans);
6977 goto out_free;
6978 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006979
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006980 if (block_rsv)
6981 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006982
Chris Mason9f3a7422007-08-07 15:52:19 -04006983 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006984 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006985 path->nodes[level] = btrfs_lock_root_node(root);
6986 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006987 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006988 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006989 memset(&wc->update_progress, 0,
6990 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006991 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006992 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006993 memcpy(&wc->update_progress, &key,
6994 sizeof(wc->update_progress));
6995
Chris Mason6702ed42007-08-07 16:15:09 -04006996 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006997 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006998 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006999 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7000 path->lowest_level = 0;
7001 if (ret < 0) {
7002 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007003 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007004 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007005 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007006
Chris Mason7d9eb122008-07-08 14:19:17 -04007007 /*
7008 * unlock our path, this is safe because only this
7009 * function is allowed to delete this snapshot
7010 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007011 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007012
Yan Zheng2c47e6052009-06-27 21:07:35 -04007013 level = btrfs_header_level(root->node);
7014 while (1) {
7015 btrfs_tree_lock(path->nodes[level]);
7016 btrfs_set_lock_blocking(path->nodes[level]);
7017
7018 ret = btrfs_lookup_extent_info(trans, root,
7019 path->nodes[level]->start,
7020 path->nodes[level]->len,
7021 &wc->refs[level],
7022 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007023 if (ret < 0) {
7024 err = ret;
7025 goto out_end_trans;
7026 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007027 BUG_ON(wc->refs[level] == 0);
7028
7029 if (level == root_item->drop_level)
7030 break;
7031
7032 btrfs_tree_unlock(path->nodes[level]);
7033 WARN_ON(wc->refs[level] != 1);
7034 level--;
7035 }
7036 }
7037
7038 wc->level = level;
7039 wc->shared_level = -1;
7040 wc->stage = DROP_REFERENCE;
7041 wc->update_ref = update_ref;
7042 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007043 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007044 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007045
7046 while (1) {
7047 ret = walk_down_tree(trans, root, path, wc);
7048 if (ret < 0) {
7049 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007050 break;
7051 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007052
7053 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7054 if (ret < 0) {
7055 err = ret;
7056 break;
7057 }
7058
7059 if (ret > 0) {
7060 BUG_ON(wc->stage != DROP_REFERENCE);
7061 break;
7062 }
7063
7064 if (wc->stage == DROP_REFERENCE) {
7065 level = wc->level;
7066 btrfs_node_key(path->nodes[level],
7067 &root_item->drop_progress,
7068 path->slots[level]);
7069 root_item->drop_level = level;
7070 }
7071
7072 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007073 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007074 ret = btrfs_update_root(trans, tree_root,
7075 &root->root_key,
7076 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007077 if (ret) {
7078 btrfs_abort_transaction(trans, tree_root, ret);
7079 err = ret;
7080 goto out_end_trans;
7081 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007082
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007083 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007084 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007085 if (IS_ERR(trans)) {
7086 err = PTR_ERR(trans);
7087 goto out_free;
7088 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007089 if (block_rsv)
7090 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007091 }
Chris Mason20524f02007-03-10 06:35:47 -05007092 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007093 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007094 if (err)
7095 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007096
7097 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007098 if (ret) {
7099 btrfs_abort_transaction(trans, tree_root, ret);
7100 goto out_end_trans;
7101 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007102
Yan, Zheng76dda932009-09-21 16:00:26 -04007103 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7104 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7105 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007106 if (ret < 0) {
7107 btrfs_abort_transaction(trans, tree_root, ret);
7108 err = ret;
7109 goto out_end_trans;
7110 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007111 /* if we fail to delete the orphan item this time
7112 * around, it'll get picked up the next time.
7113 *
7114 * The most common failure here is just -ENOENT.
7115 */
7116 btrfs_del_orphan_item(trans, tree_root,
7117 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007118 }
7119 }
7120
7121 if (root->in_radix) {
7122 btrfs_free_fs_root(tree_root->fs_info, root);
7123 } else {
7124 free_extent_buffer(root->node);
7125 free_extent_buffer(root->commit_root);
7126 kfree(root);
7127 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007128out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007129 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007130out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007131 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007132 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007133out:
7134 if (err)
7135 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007136 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007137}
Chris Mason9078a3e2007-04-26 16:46:15 -04007138
Yan Zheng2c47e6052009-06-27 21:07:35 -04007139/*
7140 * drop subtree rooted at tree block 'node'.
7141 *
7142 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007143 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007144 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007145int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7146 struct btrfs_root *root,
7147 struct extent_buffer *node,
7148 struct extent_buffer *parent)
7149{
7150 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007151 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007152 int level;
7153 int parent_level;
7154 int ret = 0;
7155 int wret;
7156
Yan Zheng2c47e6052009-06-27 21:07:35 -04007157 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7158
Yan Zhengf82d02d2008-10-29 14:49:05 -04007159 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007160 if (!path)
7161 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007162
Yan Zheng2c47e6052009-06-27 21:07:35 -04007163 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007164 if (!wc) {
7165 btrfs_free_path(path);
7166 return -ENOMEM;
7167 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007168
Chris Masonb9447ef2009-03-09 11:45:38 -04007169 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007170 parent_level = btrfs_header_level(parent);
7171 extent_buffer_get(parent);
7172 path->nodes[parent_level] = parent;
7173 path->slots[parent_level] = btrfs_header_nritems(parent);
7174
Chris Masonb9447ef2009-03-09 11:45:38 -04007175 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007176 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007177 path->nodes[level] = node;
7178 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007179 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007180
7181 wc->refs[parent_level] = 1;
7182 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7183 wc->level = level;
7184 wc->shared_level = -1;
7185 wc->stage = DROP_REFERENCE;
7186 wc->update_ref = 0;
7187 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007188 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007189 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007190
7191 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007192 wret = walk_down_tree(trans, root, path, wc);
7193 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007194 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007195 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007196 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007197
Yan Zheng2c47e6052009-06-27 21:07:35 -04007198 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007199 if (wret < 0)
7200 ret = wret;
7201 if (wret != 0)
7202 break;
7203 }
7204
Yan Zheng2c47e6052009-06-27 21:07:35 -04007205 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007206 btrfs_free_path(path);
7207 return ret;
7208}
7209
Chris Masonec44a352008-04-28 15:29:52 -04007210static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7211{
7212 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007213 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007214
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007215 /*
7216 * if restripe for this chunk_type is on pick target profile and
7217 * return, otherwise do the usual balance
7218 */
7219 stripped = get_restripe_target(root->fs_info, flags);
7220 if (stripped)
7221 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007222
Chris Masoncd02dca2010-12-13 14:56:23 -05007223 /*
7224 * we add in the count of missing devices because we want
7225 * to make sure that any RAID levels on a degraded FS
7226 * continue to be honored.
7227 */
7228 num_devices = root->fs_info->fs_devices->rw_devices +
7229 root->fs_info->fs_devices->missing_devices;
7230
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007231 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7232 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7233
Chris Masonec44a352008-04-28 15:29:52 -04007234 if (num_devices == 1) {
7235 stripped |= BTRFS_BLOCK_GROUP_DUP;
7236 stripped = flags & ~stripped;
7237
7238 /* turn raid0 into single device chunks */
7239 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7240 return stripped;
7241
7242 /* turn mirroring into duplication */
7243 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7244 BTRFS_BLOCK_GROUP_RAID10))
7245 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007246 } else {
7247 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007248 if (flags & stripped)
7249 return flags;
7250
7251 stripped |= BTRFS_BLOCK_GROUP_DUP;
7252 stripped = flags & ~stripped;
7253
7254 /* switch duplicated blocks with raid1 */
7255 if (flags & BTRFS_BLOCK_GROUP_DUP)
7256 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7257
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007258 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007259 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007260
Chris Masonec44a352008-04-28 15:29:52 -04007261 return flags;
7262}
7263
Miao Xie199c36e2011-07-15 10:34:36 +00007264static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007265{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007266 struct btrfs_space_info *sinfo = cache->space_info;
7267 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007268 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007269 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007270
Chris Masonc286ac42008-07-22 23:06:41 -04007271
Miao Xie199c36e2011-07-15 10:34:36 +00007272 /*
7273 * We need some metadata space and system metadata space for
7274 * allocating chunks in some corner cases until we force to set
7275 * it to be readonly.
7276 */
7277 if ((sinfo->flags &
7278 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7279 !force)
7280 min_allocable_bytes = 1 * 1024 * 1024;
7281 else
7282 min_allocable_bytes = 0;
7283
Yan, Zhengf0486c62010-05-16 10:46:25 -04007284 spin_lock(&sinfo->lock);
7285 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007286
7287 if (cache->ro) {
7288 ret = 0;
7289 goto out;
7290 }
7291
Yan, Zhengf0486c62010-05-16 10:46:25 -04007292 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7293 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007294
Yan, Zhengf0486c62010-05-16 10:46:25 -04007295 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007296 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7297 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007298 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007299 cache->ro = 1;
7300 ret = 0;
7301 }
WuBo61cfea92011-07-26 03:30:11 +00007302out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007303 spin_unlock(&cache->lock);
7304 spin_unlock(&sinfo->lock);
7305 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007306}
7307
Yan, Zhengf0486c62010-05-16 10:46:25 -04007308int btrfs_set_block_group_ro(struct btrfs_root *root,
7309 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007310
7311{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007312 struct btrfs_trans_handle *trans;
7313 u64 alloc_flags;
7314 int ret;
7315
7316 BUG_ON(cache->ro);
7317
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007318 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007319 if (IS_ERR(trans))
7320 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007321
7322 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007323 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007324 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007325 CHUNK_ALLOC_FORCE);
7326 if (ret < 0)
7327 goto out;
7328 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007329
Miao Xie199c36e2011-07-15 10:34:36 +00007330 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007331 if (!ret)
7332 goto out;
7333 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007334 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007335 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007336 if (ret < 0)
7337 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007338 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007339out:
7340 btrfs_end_transaction(trans, root);
7341 return ret;
7342}
7343
Chris Masonc87f08c2011-02-16 13:57:04 -05007344int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7345 struct btrfs_root *root, u64 type)
7346{
7347 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007348 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007349 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007350}
7351
Miao Xie6d07bce2011-01-05 10:07:31 +00007352/*
7353 * helper to account the unused space of all the readonly block group in the
7354 * list. takes mirrors into account.
7355 */
7356static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7357{
7358 struct btrfs_block_group_cache *block_group;
7359 u64 free_bytes = 0;
7360 int factor;
7361
7362 list_for_each_entry(block_group, groups_list, list) {
7363 spin_lock(&block_group->lock);
7364
7365 if (!block_group->ro) {
7366 spin_unlock(&block_group->lock);
7367 continue;
7368 }
7369
7370 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7371 BTRFS_BLOCK_GROUP_RAID10 |
7372 BTRFS_BLOCK_GROUP_DUP))
7373 factor = 2;
7374 else
7375 factor = 1;
7376
7377 free_bytes += (block_group->key.offset -
7378 btrfs_block_group_used(&block_group->item)) *
7379 factor;
7380
7381 spin_unlock(&block_group->lock);
7382 }
7383
7384 return free_bytes;
7385}
7386
7387/*
7388 * helper to account the unused space of all the readonly block group in the
7389 * space_info. takes mirrors into account.
7390 */
7391u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7392{
7393 int i;
7394 u64 free_bytes = 0;
7395
7396 spin_lock(&sinfo->lock);
7397
7398 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7399 if (!list_empty(&sinfo->block_groups[i]))
7400 free_bytes += __btrfs_get_ro_block_group_free_space(
7401 &sinfo->block_groups[i]);
7402
7403 spin_unlock(&sinfo->lock);
7404
7405 return free_bytes;
7406}
7407
Jeff Mahoney143bede2012-03-01 14:56:26 +01007408void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007409 struct btrfs_block_group_cache *cache)
7410{
7411 struct btrfs_space_info *sinfo = cache->space_info;
7412 u64 num_bytes;
7413
7414 BUG_ON(!cache->ro);
7415
7416 spin_lock(&sinfo->lock);
7417 spin_lock(&cache->lock);
7418 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7419 cache->bytes_super - btrfs_block_group_used(&cache->item);
7420 sinfo->bytes_readonly -= num_bytes;
7421 cache->ro = 0;
7422 spin_unlock(&cache->lock);
7423 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007424}
7425
Josef Bacikba1bf482009-09-11 16:11:19 -04007426/*
7427 * checks to see if its even possible to relocate this block group.
7428 *
7429 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7430 * ok to go ahead and try.
7431 */
7432int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007433{
Zheng Yan1a40e232008-09-26 10:09:34 -04007434 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007435 struct btrfs_space_info *space_info;
7436 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7437 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007438 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007439 u64 dev_min = 1;
7440 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007441 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007442 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007443 int full = 0;
7444 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007445
Josef Bacikba1bf482009-09-11 16:11:19 -04007446 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007447
Josef Bacikba1bf482009-09-11 16:11:19 -04007448 /* odd, couldn't find the block group, leave it alone */
7449 if (!block_group)
7450 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007451
liubocdcb7252011-08-03 10:15:25 +00007452 min_free = btrfs_block_group_used(&block_group->item);
7453
Josef Bacikba1bf482009-09-11 16:11:19 -04007454 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007455 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007456 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007457
Josef Bacikba1bf482009-09-11 16:11:19 -04007458 space_info = block_group->space_info;
7459 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007460
Josef Bacikba1bf482009-09-11 16:11:19 -04007461 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007462
Josef Bacikba1bf482009-09-11 16:11:19 -04007463 /*
7464 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007465 * relocate it unless we're able to allocate a new chunk below.
7466 *
7467 * Otherwise, we need to make sure we have room in the space to handle
7468 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007469 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007470 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007471 (space_info->bytes_used + space_info->bytes_reserved +
7472 space_info->bytes_pinned + space_info->bytes_readonly +
7473 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007474 spin_unlock(&space_info->lock);
7475 goto out;
7476 }
7477 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007478
Josef Bacikba1bf482009-09-11 16:11:19 -04007479 /*
7480 * ok we don't have enough space, but maybe we have free space on our
7481 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007482 * alloc devices and guess if we have enough space. if this block
7483 * group is going to be restriped, run checks against the target
7484 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007485 */
7486 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007487
liubocdcb7252011-08-03 10:15:25 +00007488 /*
7489 * index:
7490 * 0: raid10
7491 * 1: raid1
7492 * 2: dup
7493 * 3: raid0
7494 * 4: single
7495 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007496 target = get_restripe_target(root->fs_info, block_group->flags);
7497 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007498 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007499 } else {
7500 /*
7501 * this is just a balance, so if we were marked as full
7502 * we know there is no space for a new chunk
7503 */
7504 if (full)
7505 goto out;
7506
7507 index = get_block_group_index(block_group);
7508 }
7509
liubocdcb7252011-08-03 10:15:25 +00007510 if (index == 0) {
7511 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007512 /* Divide by 2 */
7513 min_free >>= 1;
liubocdcb7252011-08-03 10:15:25 +00007514 } else if (index == 1) {
7515 dev_min = 2;
7516 } else if (index == 2) {
Josef Bacik6719db62011-08-20 08:29:51 -04007517 /* Multiply by 2 */
7518 min_free <<= 1;
liubocdcb7252011-08-03 10:15:25 +00007519 } else if (index == 3) {
7520 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007521 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007522 }
7523
Josef Bacikba1bf482009-09-11 16:11:19 -04007524 mutex_lock(&root->fs_info->chunk_mutex);
7525 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007526 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007527
Josef Bacikba1bf482009-09-11 16:11:19 -04007528 /*
7529 * check to make sure we can actually find a chunk with enough
7530 * space to fit our block group in.
7531 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007532 if (device->total_bytes > device->bytes_used + min_free &&
7533 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007534 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007535 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007536 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007537 dev_nr++;
7538
7539 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007540 break;
liubocdcb7252011-08-03 10:15:25 +00007541
Josef Bacikba1bf482009-09-11 16:11:19 -04007542 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007543 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007544 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007545 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007546out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007547 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007548 return ret;
7549}
7550
Christoph Hellwigb2950862008-12-02 09:54:17 -05007551static int find_first_block_group(struct btrfs_root *root,
7552 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007553{
Chris Mason925baed2008-06-25 16:01:30 -04007554 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007555 struct btrfs_key found_key;
7556 struct extent_buffer *leaf;
7557 int slot;
7558
7559 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7560 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007561 goto out;
7562
Chris Masond3977122009-01-05 21:25:51 -05007563 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007564 slot = path->slots[0];
7565 leaf = path->nodes[0];
7566 if (slot >= btrfs_header_nritems(leaf)) {
7567 ret = btrfs_next_leaf(root, path);
7568 if (ret == 0)
7569 continue;
7570 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007571 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007572 break;
7573 }
7574 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7575
7576 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007577 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7578 ret = 0;
7579 goto out;
7580 }
Chris Mason0b86a832008-03-24 15:01:56 -04007581 path->slots[0]++;
7582 }
Chris Mason925baed2008-06-25 16:01:30 -04007583out:
Chris Mason0b86a832008-03-24 15:01:56 -04007584 return ret;
7585}
7586
Josef Bacik0af3d002010-06-21 14:48:16 -04007587void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7588{
7589 struct btrfs_block_group_cache *block_group;
7590 u64 last = 0;
7591
7592 while (1) {
7593 struct inode *inode;
7594
7595 block_group = btrfs_lookup_first_block_group(info, last);
7596 while (block_group) {
7597 spin_lock(&block_group->lock);
7598 if (block_group->iref)
7599 break;
7600 spin_unlock(&block_group->lock);
7601 block_group = next_block_group(info->tree_root,
7602 block_group);
7603 }
7604 if (!block_group) {
7605 if (last == 0)
7606 break;
7607 last = 0;
7608 continue;
7609 }
7610
7611 inode = block_group->inode;
7612 block_group->iref = 0;
7613 block_group->inode = NULL;
7614 spin_unlock(&block_group->lock);
7615 iput(inode);
7616 last = block_group->key.objectid + block_group->key.offset;
7617 btrfs_put_block_group(block_group);
7618 }
7619}
7620
Zheng Yan1a40e232008-09-26 10:09:34 -04007621int btrfs_free_block_groups(struct btrfs_fs_info *info)
7622{
7623 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007624 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007625 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007626 struct rb_node *n;
7627
Yan Zheng11833d62009-09-11 16:11:19 -04007628 down_write(&info->extent_commit_sem);
7629 while (!list_empty(&info->caching_block_groups)) {
7630 caching_ctl = list_entry(info->caching_block_groups.next,
7631 struct btrfs_caching_control, list);
7632 list_del(&caching_ctl->list);
7633 put_caching_control(caching_ctl);
7634 }
7635 up_write(&info->extent_commit_sem);
7636
Zheng Yan1a40e232008-09-26 10:09:34 -04007637 spin_lock(&info->block_group_cache_lock);
7638 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7639 block_group = rb_entry(n, struct btrfs_block_group_cache,
7640 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007641 rb_erase(&block_group->cache_node,
7642 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007643 spin_unlock(&info->block_group_cache_lock);
7644
Josef Bacik80eb2342008-10-29 14:49:05 -04007645 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007646 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007647 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007648
Josef Bacik817d52f2009-07-13 21:29:25 -04007649 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007650 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007651
Josef Bacik3c148742011-02-02 15:53:47 +00007652 /*
7653 * We haven't cached this block group, which means we could
7654 * possibly have excluded extents on this block group.
7655 */
7656 if (block_group->cached == BTRFS_CACHE_NO)
7657 free_excluded_extents(info->extent_root, block_group);
7658
Josef Bacik817d52f2009-07-13 21:29:25 -04007659 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007660 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007661
7662 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007663 }
7664 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007665
7666 /* now that all the block groups are freed, go through and
7667 * free all the space_info structs. This is only called during
7668 * the final stages of unmount, and so we know nobody is
7669 * using them. We call synchronize_rcu() once before we start,
7670 * just to be on the safe side.
7671 */
7672 synchronize_rcu();
7673
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007674 release_global_block_rsv(info);
7675
Chris Mason4184ea72009-03-10 12:39:20 -04007676 while(!list_empty(&info->space_info)) {
7677 space_info = list_entry(info->space_info.next,
7678 struct btrfs_space_info,
7679 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007680 if (space_info->bytes_pinned > 0 ||
Josef Bacikfb25e912011-07-26 17:00:46 -04007681 space_info->bytes_reserved > 0 ||
7682 space_info->bytes_may_use > 0) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007683 WARN_ON(1);
7684 dump_space_info(space_info, 0, 0);
7685 }
Chris Mason4184ea72009-03-10 12:39:20 -04007686 list_del(&space_info->list);
7687 kfree(space_info);
7688 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007689 return 0;
7690}
7691
Yan, Zhengb742bb82010-05-16 10:46:24 -04007692static void __link_block_group(struct btrfs_space_info *space_info,
7693 struct btrfs_block_group_cache *cache)
7694{
7695 int index = get_block_group_index(cache);
7696
7697 down_write(&space_info->groups_sem);
7698 list_add_tail(&cache->list, &space_info->block_groups[index]);
7699 up_write(&space_info->groups_sem);
7700}
7701
Chris Mason9078a3e2007-04-26 16:46:15 -04007702int btrfs_read_block_groups(struct btrfs_root *root)
7703{
7704 struct btrfs_path *path;
7705 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007706 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007707 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007708 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007709 struct btrfs_key key;
7710 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007711 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007712 int need_clear = 0;
7713 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007714
Chris Masonbe744172007-05-06 10:15:01 -04007715 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007716 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007717 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007718 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007719 path = btrfs_alloc_path();
7720 if (!path)
7721 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007722 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007723
David Sterba6c417612011-04-13 15:41:04 +02007724 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007725 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02007726 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04007727 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007728 if (btrfs_test_opt(root, CLEAR_CACHE))
7729 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007730
Chris Masond3977122009-01-05 21:25:51 -05007731 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007732 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007733 if (ret > 0)
7734 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007735 if (ret != 0)
7736 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007737 leaf = path->nodes[0];
7738 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007739 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007740 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007741 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007742 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007743 }
Li Zefan34d52cb2011-03-29 13:46:06 +08007744 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7745 GFP_NOFS);
7746 if (!cache->free_space_ctl) {
7747 kfree(cache);
7748 ret = -ENOMEM;
7749 goto error;
7750 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007751
Yan Zhengd2fb3432008-12-11 16:30:39 -05007752 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007753 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007754 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007755 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007756 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007757
Liu Bocf7c1ef2012-07-06 03:31:34 -06007758 if (need_clear) {
7759 /*
7760 * When we mount with old space cache, we need to
7761 * set BTRFS_DC_CLEAR and set dirty flag.
7762 *
7763 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7764 * truncate the old free space cache inode and
7765 * setup a new one.
7766 * b) Setting 'dirty flag' makes sure that we flush
7767 * the new space cache info onto disk.
7768 */
Josef Bacik0af3d002010-06-21 14:48:16 -04007769 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007770 if (btrfs_test_opt(root, SPACE_CACHE))
7771 cache->dirty = 1;
7772 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007773
Chris Mason5f39d392007-10-15 16:14:19 -04007774 read_extent_buffer(leaf, &cache->item,
7775 btrfs_item_ptr_offset(leaf, path->slots[0]),
7776 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007777 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007778
Chris Mason9078a3e2007-04-26 16:46:15 -04007779 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007780 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007781 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007782 cache->sectorsize = root->sectorsize;
7783
Li Zefan34d52cb2011-03-29 13:46:06 +08007784 btrfs_init_free_space_ctl(cache);
7785
Josef Bacik817d52f2009-07-13 21:29:25 -04007786 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007787 * We need to exclude the super stripes now so that the space
7788 * info has super bytes accounted for, otherwise we'll think
7789 * we have more space than we actually do.
7790 */
7791 exclude_super_stripes(root, cache);
7792
7793 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007794 * check for two cases, either we are full, and therefore
7795 * don't need to bother with the caching work since we won't
7796 * find any space, or we are empty, and we can just add all
7797 * the space in and be done with it. This saves us _alot_ of
7798 * time, particularly in the full case.
7799 */
7800 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007801 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007802 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007803 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007804 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007805 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007806 cache->cached = BTRFS_CACHE_FINISHED;
7807 add_new_free_space(cache, root->fs_info,
7808 found_key.objectid,
7809 found_key.objectid +
7810 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007811 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007812 }
Chris Mason96b51792007-10-15 16:15:19 -04007813
Chris Mason6324fbf2008-03-24 15:01:59 -04007814 ret = update_space_info(info, cache->flags, found_key.offset,
7815 btrfs_block_group_used(&cache->item),
7816 &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007817 BUG_ON(ret); /* -ENOMEM */
Chris Mason6324fbf2008-03-24 15:01:59 -04007818 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007819 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007820 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007821 spin_unlock(&cache->space_info->lock);
7822
Yan, Zhengb742bb82010-05-16 10:46:24 -04007823 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007824
Josef Bacik0f9dd462008-09-23 13:14:11 -04007825 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007826 BUG_ON(ret); /* Logic error */
Chris Mason75ccf472008-09-30 19:24:06 -04007827
7828 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007829 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007830 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007831 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007832
7833 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7834 if (!(get_alloc_profile(root, space_info->flags) &
7835 (BTRFS_BLOCK_GROUP_RAID10 |
7836 BTRFS_BLOCK_GROUP_RAID1 |
7837 BTRFS_BLOCK_GROUP_DUP)))
7838 continue;
7839 /*
7840 * avoid allocating from un-mirrored block group if there are
7841 * mirrored block groups.
7842 */
7843 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007844 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007845 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007846 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007847 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007848
7849 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007850 ret = 0;
7851error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007852 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007853 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007854}
Chris Mason6324fbf2008-03-24 15:01:59 -04007855
Josef Bacikea658ba2012-09-11 16:57:25 -04007856void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
7857 struct btrfs_root *root)
7858{
7859 struct btrfs_block_group_cache *block_group, *tmp;
7860 struct btrfs_root *extent_root = root->fs_info->extent_root;
7861 struct btrfs_block_group_item item;
7862 struct btrfs_key key;
7863 int ret = 0;
7864
7865 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
7866 new_bg_list) {
7867 list_del_init(&block_group->new_bg_list);
7868
7869 if (ret)
7870 continue;
7871
7872 spin_lock(&block_group->lock);
7873 memcpy(&item, &block_group->item, sizeof(item));
7874 memcpy(&key, &block_group->key, sizeof(key));
7875 spin_unlock(&block_group->lock);
7876
7877 ret = btrfs_insert_item(trans, extent_root, &key, &item,
7878 sizeof(item));
7879 if (ret)
7880 btrfs_abort_transaction(trans, extent_root, ret);
7881 }
7882}
7883
Chris Mason6324fbf2008-03-24 15:01:59 -04007884int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7885 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007886 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007887 u64 size)
7888{
7889 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007890 struct btrfs_root *extent_root;
7891 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007892
7893 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007894
Chris Mason12fcfd22009-03-24 10:24:20 -04007895 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007896
Chris Mason8f18cf12008-04-25 16:53:30 -04007897 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007898 if (!cache)
7899 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08007900 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7901 GFP_NOFS);
7902 if (!cache->free_space_ctl) {
7903 kfree(cache);
7904 return -ENOMEM;
7905 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007906
Chris Masone17cade2008-04-15 15:41:47 -04007907 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007908 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007909 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007910 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007911 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007912
Yan Zhengd2fb3432008-12-11 16:30:39 -05007913 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007914 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007915 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007916 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04007917 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007918
Li Zefan34d52cb2011-03-29 13:46:06 +08007919 btrfs_init_free_space_ctl(cache);
7920
Chris Mason6324fbf2008-03-24 15:01:59 -04007921 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007922 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7923 cache->flags = type;
7924 btrfs_set_block_group_flags(&cache->item, type);
7925
Yan Zheng11833d62009-09-11 16:11:19 -04007926 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007927 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007928 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007929
Josef Bacik817d52f2009-07-13 21:29:25 -04007930 add_new_free_space(cache, root->fs_info, chunk_offset,
7931 chunk_offset + size);
7932
Yan Zheng11833d62009-09-11 16:11:19 -04007933 free_excluded_extents(root, cache);
7934
Chris Mason6324fbf2008-03-24 15:01:59 -04007935 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7936 &cache->space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007937 BUG_ON(ret); /* -ENOMEM */
Li Zefanc7c144d2011-12-07 10:39:22 +08007938 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04007939
7940 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007941 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007942 spin_unlock(&cache->space_info->lock);
7943
Yan, Zhengb742bb82010-05-16 10:46:24 -04007944 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007945
Josef Bacik0f9dd462008-09-23 13:14:11 -04007946 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007947 BUG_ON(ret); /* Logic error */
Chris Masonc286ac42008-07-22 23:06:41 -04007948
Josef Bacikea658ba2012-09-11 16:57:25 -04007949 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04007950
Chris Masond18a2c42008-04-04 15:40:00 -04007951 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007952
Chris Mason6324fbf2008-03-24 15:01:59 -04007953 return 0;
7954}
Zheng Yan1a40e232008-09-26 10:09:34 -04007955
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007956static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
7957{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03007958 u64 extra_flags = chunk_to_extended(flags) &
7959 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007960
7961 if (flags & BTRFS_BLOCK_GROUP_DATA)
7962 fs_info->avail_data_alloc_bits &= ~extra_flags;
7963 if (flags & BTRFS_BLOCK_GROUP_METADATA)
7964 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
7965 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
7966 fs_info->avail_system_alloc_bits &= ~extra_flags;
7967}
7968
Zheng Yan1a40e232008-09-26 10:09:34 -04007969int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7970 struct btrfs_root *root, u64 group_start)
7971{
7972 struct btrfs_path *path;
7973 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007974 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007975 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007976 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007977 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007978 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007979 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04007980 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007981
Zheng Yan1a40e232008-09-26 10:09:34 -04007982 root = root->fs_info->extent_root;
7983
7984 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7985 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007986 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007987
liubo9f7c43c2011-03-07 02:13:33 +00007988 /*
7989 * Free the reserved super bytes from this block group before
7990 * remove it.
7991 */
7992 free_excluded_extents(root, block_group);
7993
Zheng Yan1a40e232008-09-26 10:09:34 -04007994 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007995 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04007996 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7997 BTRFS_BLOCK_GROUP_RAID1 |
7998 BTRFS_BLOCK_GROUP_RAID10))
7999 factor = 2;
8000 else
8001 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008002
Chris Mason44fb5512009-06-04 15:34:51 -04008003 /* make sure this block group isn't part of an allocation cluster */
8004 cluster = &root->fs_info->data_alloc_cluster;
8005 spin_lock(&cluster->refill_lock);
8006 btrfs_return_cluster_to_free_space(block_group, cluster);
8007 spin_unlock(&cluster->refill_lock);
8008
8009 /*
8010 * make sure this block group isn't part of a metadata
8011 * allocation cluster
8012 */
8013 cluster = &root->fs_info->meta_alloc_cluster;
8014 spin_lock(&cluster->refill_lock);
8015 btrfs_return_cluster_to_free_space(block_group, cluster);
8016 spin_unlock(&cluster->refill_lock);
8017
Zheng Yan1a40e232008-09-26 10:09:34 -04008018 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008019 if (!path) {
8020 ret = -ENOMEM;
8021 goto out;
8022 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008023
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008024 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008025 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008026 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008027 if (ret) {
8028 btrfs_add_delayed_iput(inode);
8029 goto out;
8030 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008031 clear_nlink(inode);
8032 /* One for the block groups ref */
8033 spin_lock(&block_group->lock);
8034 if (block_group->iref) {
8035 block_group->iref = 0;
8036 block_group->inode = NULL;
8037 spin_unlock(&block_group->lock);
8038 iput(inode);
8039 } else {
8040 spin_unlock(&block_group->lock);
8041 }
8042 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008043 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008044 }
8045
8046 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8047 key.offset = block_group->key.objectid;
8048 key.type = 0;
8049
8050 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8051 if (ret < 0)
8052 goto out;
8053 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008054 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008055 if (ret == 0) {
8056 ret = btrfs_del_item(trans, tree_root, path);
8057 if (ret)
8058 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008059 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008060 }
8061
Yan Zheng3dfdb932009-01-21 10:49:16 -05008062 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008063 rb_erase(&block_group->cache_node,
8064 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008065 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008066
Josef Bacik80eb2342008-10-29 14:49:05 -04008067 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008068 /*
8069 * we must use list_del_init so people can check to see if they
8070 * are still on the list after taking the semaphore
8071 */
8072 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008073 if (list_empty(&block_group->space_info->block_groups[index]))
8074 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008075 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008076
Josef Bacik817d52f2009-07-13 21:29:25 -04008077 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008078 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008079
8080 btrfs_remove_free_space_cache(block_group);
8081
Yan Zhengc146afa2008-11-12 14:34:12 -05008082 spin_lock(&block_group->space_info->lock);
8083 block_group->space_info->total_bytes -= block_group->key.offset;
8084 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008085 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008086 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008087
Josef Bacik0af3d002010-06-21 14:48:16 -04008088 memcpy(&key, &block_group->key, sizeof(key));
8089
Chris Mason283bb192009-07-24 16:30:55 -04008090 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008091
Chris Masonfa9c0d792009-04-03 09:47:43 -04008092 btrfs_put_block_group(block_group);
8093 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008094
8095 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8096 if (ret > 0)
8097 ret = -EIO;
8098 if (ret < 0)
8099 goto out;
8100
8101 ret = btrfs_del_item(trans, root, path);
8102out:
8103 btrfs_free_path(path);
8104 return ret;
8105}
liuboacce9522011-01-06 19:30:25 +08008106
liuboc59021f2011-03-07 02:13:14 +00008107int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8108{
8109 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008110 struct btrfs_super_block *disk_super;
8111 u64 features;
8112 u64 flags;
8113 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008114 int ret;
8115
David Sterba6c417612011-04-13 15:41:04 +02008116 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008117 if (!btrfs_super_root(disk_super))
8118 return 1;
liuboc59021f2011-03-07 02:13:14 +00008119
liubo1aba86d2011-04-08 08:44:37 +00008120 features = btrfs_super_incompat_flags(disk_super);
8121 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8122 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008123
liubo1aba86d2011-04-08 08:44:37 +00008124 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8125 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008126 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008127 goto out;
liuboc59021f2011-03-07 02:13:14 +00008128
liubo1aba86d2011-04-08 08:44:37 +00008129 if (mixed) {
8130 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8131 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8132 } else {
8133 flags = BTRFS_BLOCK_GROUP_METADATA;
8134 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8135 if (ret)
8136 goto out;
8137
8138 flags = BTRFS_BLOCK_GROUP_DATA;
8139 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8140 }
8141out:
liuboc59021f2011-03-07 02:13:14 +00008142 return ret;
8143}
8144
liuboacce9522011-01-06 19:30:25 +08008145int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8146{
8147 return unpin_extent_range(root, start, end);
8148}
8149
8150int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008151 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008152{
Li Dongyang5378e602011-03-24 10:24:27 +00008153 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008154}
Li Dongyangf7039b12011-03-24 10:24:28 +00008155
8156int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8157{
8158 struct btrfs_fs_info *fs_info = root->fs_info;
8159 struct btrfs_block_group_cache *cache = NULL;
8160 u64 group_trimmed;
8161 u64 start;
8162 u64 end;
8163 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008164 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008165 int ret = 0;
8166
Liu Bo2cac13e2012-02-09 18:17:41 +08008167 /*
8168 * try to trim all FS space, our block group may start from non-zero.
8169 */
8170 if (range->len == total_bytes)
8171 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8172 else
8173 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008174
8175 while (cache) {
8176 if (cache->key.objectid >= (range->start + range->len)) {
8177 btrfs_put_block_group(cache);
8178 break;
8179 }
8180
8181 start = max(range->start, cache->key.objectid);
8182 end = min(range->start + range->len,
8183 cache->key.objectid + cache->key.offset);
8184
8185 if (end - start >= range->minlen) {
8186 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008187 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008188 if (!ret)
8189 wait_block_group_cache_done(cache);
8190 }
8191 ret = btrfs_trim_block_group(cache,
8192 &group_trimmed,
8193 start,
8194 end,
8195 range->minlen);
8196
8197 trimmed += group_trimmed;
8198 if (ret) {
8199 btrfs_put_block_group(cache);
8200 break;
8201 }
8202 }
8203
8204 cache = next_block_group(fs_info->tree_root, cache);
8205 }
8206
8207 range->len = trimmed;
8208 return ret;
8209}