blob: f8a358aee060e8591a779baa2eba44a406ebe3ff [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,
471 struct btrfs_trans_handle *trans,
Josef Bacikb8399de2010-12-08 09:15:11 -0500472 struct btrfs_root *root,
Josef Bacik9d66e232010-08-25 16:54:15 -0400473 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400474{
Josef Bacik291c7d22011-11-14 13:52:14 -0500475 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400476 struct btrfs_fs_info *fs_info = cache->fs_info;
477 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400478 int ret = 0;
479
Josef Bacik291c7d22011-11-14 13:52:14 -0500480 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100481 if (!caching_ctl)
482 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500483
484 INIT_LIST_HEAD(&caching_ctl->list);
485 mutex_init(&caching_ctl->mutex);
486 init_waitqueue_head(&caching_ctl->wait);
487 caching_ctl->block_group = cache;
488 caching_ctl->progress = cache->key.objectid;
489 atomic_set(&caching_ctl->count, 1);
490 caching_ctl->work.func = caching_thread;
491
492 spin_lock(&cache->lock);
493 /*
494 * This should be a rare occasion, but this could happen I think in the
495 * case where one thread starts to load the space cache info, and then
496 * some other thread starts a transaction commit which tries to do an
497 * allocation while the other thread is still loading the space cache
498 * info. The previous loop should have kept us from choosing this block
499 * group, but if we've moved to the state where we will wait on caching
500 * block groups we need to first check if we're doing a fast load here,
501 * so we can wait for it to finish, otherwise we could end up allocating
502 * from a block group who's cache gets evicted for one reason or
503 * another.
504 */
505 while (cache->cached == BTRFS_CACHE_FAST) {
506 struct btrfs_caching_control *ctl;
507
508 ctl = cache->caching_ctl;
509 atomic_inc(&ctl->count);
510 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
511 spin_unlock(&cache->lock);
512
513 schedule();
514
515 finish_wait(&ctl->wait, &wait);
516 put_caching_control(ctl);
517 spin_lock(&cache->lock);
518 }
519
520 if (cache->cached != BTRFS_CACHE_NO) {
521 spin_unlock(&cache->lock);
522 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400523 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500524 }
525 WARN_ON(cache->caching_ctl);
526 cache->caching_ctl = caching_ctl;
527 cache->cached = BTRFS_CACHE_FAST;
528 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400529
Josef Bacik9d66e232010-08-25 16:54:15 -0400530 /*
531 * We can't do the read from on-disk cache during a commit since we need
Josef Bacikb8399de2010-12-08 09:15:11 -0500532 * to have the normal tree locking. Also if we are currently trying to
533 * allocate blocks for the tree root we can't do the fast caching since
534 * we likely hold important locks.
Josef Bacik9d66e232010-08-25 16:54:15 -0400535 */
Josef Bacikd53ba472012-04-12 16:03:57 -0400536 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400537 ret = load_free_space_cache(fs_info, cache);
538
539 spin_lock(&cache->lock);
540 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500541 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400542 cache->cached = BTRFS_CACHE_FINISHED;
543 cache->last_byte_to_unpin = (u64)-1;
544 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500545 if (load_cache_only) {
546 cache->caching_ctl = NULL;
547 cache->cached = BTRFS_CACHE_NO;
548 } else {
549 cache->cached = BTRFS_CACHE_STARTED;
550 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400551 }
552 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500553 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000554 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500555 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000556 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400557 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000558 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500559 } else {
560 /*
561 * We are not going to do the fast caching, set cached to the
562 * appropriate value and wakeup any waiters.
563 */
564 spin_lock(&cache->lock);
565 if (load_cache_only) {
566 cache->caching_ctl = NULL;
567 cache->cached = BTRFS_CACHE_NO;
568 } else {
569 cache->cached = BTRFS_CACHE_STARTED;
570 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400571 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500572 wake_up(&caching_ctl->wait);
573 }
574
575 if (load_cache_only) {
576 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400577 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400578 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400579
Yan Zheng11833d62009-09-11 16:11:19 -0400580 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500581 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400582 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
583 up_write(&fs_info->extent_commit_sem);
584
Josef Bacik11dfe352009-11-13 20:12:59 +0000585 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400586
Josef Bacikbab39bf2011-06-30 14:42:28 -0400587 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400588
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400589 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400590}
591
Josef Bacik0f9dd462008-09-23 13:14:11 -0400592/*
593 * return the block group that starts at or after bytenr
594 */
Chris Masond3977122009-01-05 21:25:51 -0500595static struct btrfs_block_group_cache *
596btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400597{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400598 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400599
Josef Bacik0f9dd462008-09-23 13:14:11 -0400600 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400601
Josef Bacik0f9dd462008-09-23 13:14:11 -0400602 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400603}
604
Josef Bacik0f9dd462008-09-23 13:14:11 -0400605/*
Sankar P9f556842009-05-14 13:52:22 -0400606 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400607 */
Chris Masond3977122009-01-05 21:25:51 -0500608struct btrfs_block_group_cache *btrfs_lookup_block_group(
609 struct btrfs_fs_info *info,
610 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400611{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400612 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400613
Josef Bacik0f9dd462008-09-23 13:14:11 -0400614 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400615
Josef Bacik0f9dd462008-09-23 13:14:11 -0400616 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400617}
Chris Mason0b86a832008-03-24 15:01:56 -0400618
Josef Bacik0f9dd462008-09-23 13:14:11 -0400619static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
620 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400621{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400622 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400623 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400624
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200625 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400626
Chris Mason4184ea72009-03-10 12:39:20 -0400627 rcu_read_lock();
628 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400629 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400630 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400631 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400632 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400633 }
Chris Mason4184ea72009-03-10 12:39:20 -0400634 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400635 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400636}
637
Chris Mason4184ea72009-03-10 12:39:20 -0400638/*
639 * after adding space to the filesystem, we need to clear the full flags
640 * on all the space infos.
641 */
642void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
643{
644 struct list_head *head = &info->space_info;
645 struct btrfs_space_info *found;
646
647 rcu_read_lock();
648 list_for_each_entry_rcu(found, head, list)
649 found->full = 0;
650 rcu_read_unlock();
651}
652
Yan Zhengd2fb3432008-12-11 16:30:39 -0500653u64 btrfs_find_block_group(struct btrfs_root *root,
654 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400655{
Chris Mason96b51792007-10-15 16:15:19 -0400656 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400657 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500658 u64 last = max(search_hint, search_start);
659 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400660 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500661 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400662 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400663again:
Zheng Yane8569812008-09-26 10:05:48 -0400664 while (1) {
665 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400666 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400667 break;
Chris Mason96b51792007-10-15 16:15:19 -0400668
Chris Masonc286ac42008-07-22 23:06:41 -0400669 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400670 last = cache->key.objectid + cache->key.offset;
671 used = btrfs_block_group_used(&cache->item);
672
Yan Zhengd2fb3432008-12-11 16:30:39 -0500673 if ((full_search || !cache->ro) &&
674 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400675 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500676 div_factor(cache->key.offset, factor)) {
677 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400678 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400679 btrfs_put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400680 goto found;
681 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400682 }
Chris Masonc286ac42008-07-22 23:06:41 -0400683 spin_unlock(&cache->lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400684 btrfs_put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400685 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400686 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400687 if (!wrapped) {
688 last = search_start;
689 wrapped = 1;
690 goto again;
691 }
692 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400693 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400694 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400695 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400696 goto again;
697 }
Chris Masonbe744172007-05-06 10:15:01 -0400698found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500699 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400700}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400701
Chris Masone02119d2008-09-05 16:13:11 -0400702/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400703int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400704{
705 int ret;
706 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400707 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400708
Zheng Yan31840ae2008-09-23 13:14:14 -0400709 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700710 if (!path)
711 return -ENOMEM;
712
Chris Masone02119d2008-09-05 16:13:11 -0400713 key.objectid = start;
714 key.offset = len;
715 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
716 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
717 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400718 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500719 return ret;
720}
721
Chris Masond8d5f3e2007-12-11 12:42:00 -0500722/*
Yan, Zhenga22285a2010-05-16 10:48:46 -0400723 * helper function to lookup reference count and flags of extent.
724 *
725 * the head node for delayed ref is used to store the sum of all the
726 * reference count modifications queued up in the rbtree. the head
727 * node may also store the extent flags to set. This way you can check
728 * to see what the reference count and extent flags would be if all of
729 * the delayed refs are not processed.
730 */
731int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
732 struct btrfs_root *root, u64 bytenr,
733 u64 num_bytes, u64 *refs, u64 *flags)
734{
735 struct btrfs_delayed_ref_head *head;
736 struct btrfs_delayed_ref_root *delayed_refs;
737 struct btrfs_path *path;
738 struct btrfs_extent_item *ei;
739 struct extent_buffer *leaf;
740 struct btrfs_key key;
741 u32 item_size;
742 u64 num_refs;
743 u64 extent_flags;
744 int ret;
745
746 path = btrfs_alloc_path();
747 if (!path)
748 return -ENOMEM;
749
750 key.objectid = bytenr;
751 key.type = BTRFS_EXTENT_ITEM_KEY;
752 key.offset = num_bytes;
753 if (!trans) {
754 path->skip_locking = 1;
755 path->search_commit_root = 1;
756 }
757again:
758 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
759 &key, path, 0, 0);
760 if (ret < 0)
761 goto out_free;
762
763 if (ret == 0) {
764 leaf = path->nodes[0];
765 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
766 if (item_size >= sizeof(*ei)) {
767 ei = btrfs_item_ptr(leaf, path->slots[0],
768 struct btrfs_extent_item);
769 num_refs = btrfs_extent_refs(leaf, ei);
770 extent_flags = btrfs_extent_flags(leaf, ei);
771 } else {
772#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
773 struct btrfs_extent_item_v0 *ei0;
774 BUG_ON(item_size != sizeof(*ei0));
775 ei0 = btrfs_item_ptr(leaf, path->slots[0],
776 struct btrfs_extent_item_v0);
777 num_refs = btrfs_extent_refs_v0(leaf, ei0);
778 /* FIXME: this isn't correct for data */
779 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
780#else
781 BUG();
782#endif
783 }
784 BUG_ON(num_refs == 0);
785 } else {
786 num_refs = 0;
787 extent_flags = 0;
788 ret = 0;
789 }
790
791 if (!trans)
792 goto out;
793
794 delayed_refs = &trans->transaction->delayed_refs;
795 spin_lock(&delayed_refs->lock);
796 head = btrfs_find_delayed_ref_head(trans, bytenr);
797 if (head) {
798 if (!mutex_trylock(&head->mutex)) {
799 atomic_inc(&head->node.refs);
800 spin_unlock(&delayed_refs->lock);
801
David Sterbab3b4aa72011-04-21 01:20:15 +0200802 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400803
David Sterba8cc33e52011-05-02 15:29:25 +0200804 /*
805 * Mutex was contended, block until it's released and try
806 * again
807 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400808 mutex_lock(&head->mutex);
809 mutex_unlock(&head->mutex);
810 btrfs_put_delayed_ref(&head->node);
811 goto again;
812 }
813 if (head->extent_op && head->extent_op->update_flags)
814 extent_flags |= head->extent_op->flags_to_set;
815 else
816 BUG_ON(num_refs == 0);
817
818 num_refs += head->node.ref_mod;
819 mutex_unlock(&head->mutex);
820 }
821 spin_unlock(&delayed_refs->lock);
822out:
823 WARN_ON(num_refs == 0);
824 if (refs)
825 *refs = num_refs;
826 if (flags)
827 *flags = extent_flags;
828out_free:
829 btrfs_free_path(path);
830 return ret;
831}
832
833/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500834 * Back reference rules. Back refs have three main goals:
835 *
836 * 1) differentiate between all holders of references to an extent so that
837 * when a reference is dropped we can make sure it was a valid reference
838 * before freeing the extent.
839 *
840 * 2) Provide enough information to quickly find the holders of an extent
841 * if we notice a given block is corrupted or bad.
842 *
843 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
844 * maintenance. This is actually the same as #2, but with a slightly
845 * different use case.
846 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 * There are two kinds of back refs. The implicit back refs is optimized
848 * for pointers in non-shared tree blocks. For a given pointer in a block,
849 * back refs of this kind provide information about the block's owner tree
850 * and the pointer's key. These information allow us to find the block by
851 * b-tree searching. The full back refs is for pointers in tree blocks not
852 * referenced by their owner trees. The location of tree block is recorded
853 * in the back refs. Actually the full back refs is generic, and can be
854 * used in all cases the implicit back refs is used. The major shortcoming
855 * of the full back refs is its overhead. Every time a tree block gets
856 * COWed, we have to update back refs entry for all pointers in it.
857 *
858 * For a newly allocated tree block, we use implicit back refs for
859 * pointers in it. This means most tree related operations only involve
860 * implicit back refs. For a tree block created in old transaction, the
861 * only way to drop a reference to it is COW it. So we can detect the
862 * event that tree block loses its owner tree's reference and do the
863 * back refs conversion.
864 *
865 * When a tree block is COW'd through a tree, there are four cases:
866 *
867 * The reference count of the block is one and the tree is the block's
868 * owner tree. Nothing to do in this case.
869 *
870 * The reference count of the block is one and the tree is not the
871 * block's owner tree. In this case, full back refs is used for pointers
872 * in the block. Remove these full back refs, add implicit back refs for
873 * every pointers in the new block.
874 *
875 * The reference count of the block is greater than one and the tree is
876 * the block's owner tree. In this case, implicit back refs is used for
877 * pointers in the block. Add full back refs for every pointers in the
878 * block, increase lower level extents' reference counts. The original
879 * implicit back refs are entailed to the new block.
880 *
881 * The reference count of the block is greater than one and the tree is
882 * not the block's owner tree. Add implicit back refs for every pointer in
883 * the new block, increase lower level extents' reference count.
884 *
885 * Back Reference Key composing:
886 *
887 * The key objectid corresponds to the first byte in the extent,
888 * The key type is used to differentiate between types of back refs.
889 * There are different meanings of the key offset for different types
890 * of back refs.
891 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500892 * File extents can be referenced by:
893 *
894 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400895 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500896 * - different offsets inside a file (bookend extents in file.c)
897 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400898 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500899 *
900 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500901 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 * - original offset in the file
903 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400904 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400905 * The key offset for the implicit back refs is hash of the first
906 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500907 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400908 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500909 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400910 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500911 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400912 * The key offset for the implicit back refs is the first byte of
913 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500914 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 * When a file extent is allocated, The implicit back refs is used.
916 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500917 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 * (root_key.objectid, inode objectid, offset in file, 1)
919 *
920 * When a file extent is removed file truncation, we find the
921 * corresponding implicit back refs and check the following fields:
922 *
923 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500924 *
925 * Btree extents can be referenced by:
926 *
927 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500928 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929 * Both the implicit back refs and the full back refs for tree blocks
930 * only consist of key. The key offset for the implicit back refs is
931 * objectid of block's owner tree. The key offset for the full back refs
932 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500933 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400934 * When implicit back refs is used, information about the lowest key and
935 * level of the tree block are required. These information are stored in
936 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500937 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400938
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400939#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
940static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
941 struct btrfs_root *root,
942 struct btrfs_path *path,
943 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500944{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 struct btrfs_extent_item *item;
946 struct btrfs_extent_item_v0 *ei0;
947 struct btrfs_extent_ref_v0 *ref0;
948 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400949 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 struct btrfs_key key;
951 struct btrfs_key found_key;
952 u32 new_size = sizeof(*item);
953 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500954 int ret;
955
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 leaf = path->nodes[0];
957 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500958
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
960 ei0 = btrfs_item_ptr(leaf, path->slots[0],
961 struct btrfs_extent_item_v0);
962 refs = btrfs_extent_refs_v0(leaf, ei0);
963
964 if (owner == (u64)-1) {
965 while (1) {
966 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
967 ret = btrfs_next_leaf(root, path);
968 if (ret < 0)
969 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971 leaf = path->nodes[0];
972 }
973 btrfs_item_key_to_cpu(leaf, &found_key,
974 path->slots[0]);
975 BUG_ON(key.objectid != found_key.objectid);
976 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
977 path->slots[0]++;
978 continue;
979 }
980 ref0 = btrfs_item_ptr(leaf, path->slots[0],
981 struct btrfs_extent_ref_v0);
982 owner = btrfs_ref_objectid_v0(leaf, ref0);
983 break;
984 }
985 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200986 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987
988 if (owner < BTRFS_FIRST_FREE_OBJECTID)
989 new_size += sizeof(*bi);
990
991 new_size -= sizeof(*ei0);
992 ret = btrfs_search_slot(trans, root, &key, path,
993 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400994 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100996 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997
Jeff Mahoney143bede2012-03-01 14:56:26 +0100998 btrfs_extend_item(trans, root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999
1000 leaf = path->nodes[0];
1001 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1002 btrfs_set_extent_refs(leaf, item, refs);
1003 /* FIXME: get real generation */
1004 btrfs_set_extent_generation(leaf, item, 0);
1005 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1006 btrfs_set_extent_flags(leaf, item,
1007 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1008 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1009 bi = (struct btrfs_tree_block_info *)(item + 1);
1010 /* FIXME: get first key of the block */
1011 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1012 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1013 } else {
1014 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1015 }
1016 btrfs_mark_buffer_dirty(leaf);
1017 return 0;
1018}
1019#endif
1020
1021static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1022{
1023 u32 high_crc = ~(u32)0;
1024 u32 low_crc = ~(u32)0;
1025 __le64 lenum;
1026
1027 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001028 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001029 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001030 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001032 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001033
1034 return ((u64)high_crc << 31) ^ (u64)low_crc;
1035}
1036
1037static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1038 struct btrfs_extent_data_ref *ref)
1039{
1040 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1041 btrfs_extent_data_ref_objectid(leaf, ref),
1042 btrfs_extent_data_ref_offset(leaf, ref));
1043}
1044
1045static int match_extent_data_ref(struct extent_buffer *leaf,
1046 struct btrfs_extent_data_ref *ref,
1047 u64 root_objectid, u64 owner, u64 offset)
1048{
1049 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1050 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1051 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1052 return 0;
1053 return 1;
1054}
1055
1056static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1057 struct btrfs_root *root,
1058 struct btrfs_path *path,
1059 u64 bytenr, u64 parent,
1060 u64 root_objectid,
1061 u64 owner, u64 offset)
1062{
1063 struct btrfs_key key;
1064 struct btrfs_extent_data_ref *ref;
1065 struct extent_buffer *leaf;
1066 u32 nritems;
1067 int ret;
1068 int recow;
1069 int err = -ENOENT;
1070
1071 key.objectid = bytenr;
1072 if (parent) {
1073 key.type = BTRFS_SHARED_DATA_REF_KEY;
1074 key.offset = parent;
1075 } else {
1076 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1077 key.offset = hash_extent_data_ref(root_objectid,
1078 owner, offset);
1079 }
1080again:
1081 recow = 0;
1082 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1083 if (ret < 0) {
1084 err = ret;
1085 goto fail;
1086 }
1087
1088 if (parent) {
1089 if (!ret)
1090 return 0;
1091#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1092 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001093 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1095 if (ret < 0) {
1096 err = ret;
1097 goto fail;
1098 }
1099 if (!ret)
1100 return 0;
1101#endif
1102 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001103 }
1104
1105 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001106 nritems = btrfs_header_nritems(leaf);
1107 while (1) {
1108 if (path->slots[0] >= nritems) {
1109 ret = btrfs_next_leaf(root, path);
1110 if (ret < 0)
1111 err = ret;
1112 if (ret)
1113 goto fail;
1114
1115 leaf = path->nodes[0];
1116 nritems = btrfs_header_nritems(leaf);
1117 recow = 1;
1118 }
1119
1120 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1121 if (key.objectid != bytenr ||
1122 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1123 goto fail;
1124
1125 ref = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_extent_data_ref);
1127
1128 if (match_extent_data_ref(leaf, ref, root_objectid,
1129 owner, offset)) {
1130 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001131 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001132 goto again;
1133 }
1134 err = 0;
1135 break;
1136 }
1137 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001138 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001139fail:
1140 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001141}
1142
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001143static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1144 struct btrfs_root *root,
1145 struct btrfs_path *path,
1146 u64 bytenr, u64 parent,
1147 u64 root_objectid, u64 owner,
1148 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001149{
1150 struct btrfs_key key;
1151 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001152 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001153 u32 num_refs;
1154 int ret;
1155
1156 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 if (parent) {
1158 key.type = BTRFS_SHARED_DATA_REF_KEY;
1159 key.offset = parent;
1160 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001161 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001162 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1163 key.offset = hash_extent_data_ref(root_objectid,
1164 owner, offset);
1165 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001166 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001167
1168 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1169 if (ret && ret != -EEXIST)
1170 goto fail;
1171
1172 leaf = path->nodes[0];
1173 if (parent) {
1174 struct btrfs_shared_data_ref *ref;
1175 ref = btrfs_item_ptr(leaf, path->slots[0],
1176 struct btrfs_shared_data_ref);
1177 if (ret == 0) {
1178 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1179 } else {
1180 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1181 num_refs += refs_to_add;
1182 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1183 }
1184 } else {
1185 struct btrfs_extent_data_ref *ref;
1186 while (ret == -EEXIST) {
1187 ref = btrfs_item_ptr(leaf, path->slots[0],
1188 struct btrfs_extent_data_ref);
1189 if (match_extent_data_ref(leaf, ref, root_objectid,
1190 owner, offset))
1191 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001192 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 key.offset++;
1194 ret = btrfs_insert_empty_item(trans, root, path, &key,
1195 size);
1196 if (ret && ret != -EEXIST)
1197 goto fail;
1198
1199 leaf = path->nodes[0];
1200 }
1201 ref = btrfs_item_ptr(leaf, path->slots[0],
1202 struct btrfs_extent_data_ref);
1203 if (ret == 0) {
1204 btrfs_set_extent_data_ref_root(leaf, ref,
1205 root_objectid);
1206 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1207 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1208 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1209 } else {
1210 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1211 num_refs += refs_to_add;
1212 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1213 }
1214 }
1215 btrfs_mark_buffer_dirty(leaf);
1216 ret = 0;
1217fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001218 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001219 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001220}
1221
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1223 struct btrfs_root *root,
1224 struct btrfs_path *path,
1225 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001226{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001227 struct btrfs_key key;
1228 struct btrfs_extent_data_ref *ref1 = NULL;
1229 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001230 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001231 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001232 int ret = 0;
1233
1234 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001235 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1236
1237 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1238 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1239 struct btrfs_extent_data_ref);
1240 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1241 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1242 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1243 struct btrfs_shared_data_ref);
1244 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1245#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1246 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1247 struct btrfs_extent_ref_v0 *ref0;
1248 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1249 struct btrfs_extent_ref_v0);
1250 num_refs = btrfs_ref_count_v0(leaf, ref0);
1251#endif
1252 } else {
1253 BUG();
1254 }
1255
Chris Mason56bec292009-03-13 10:10:06 -04001256 BUG_ON(num_refs < refs_to_drop);
1257 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001258
Zheng Yan31840ae2008-09-23 13:14:14 -04001259 if (num_refs == 0) {
1260 ret = btrfs_del_item(trans, root, path);
1261 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001262 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1263 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1264 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1265 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1266#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1267 else {
1268 struct btrfs_extent_ref_v0 *ref0;
1269 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1270 struct btrfs_extent_ref_v0);
1271 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1272 }
1273#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001274 btrfs_mark_buffer_dirty(leaf);
1275 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001276 return ret;
1277}
1278
1279static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1280 struct btrfs_path *path,
1281 struct btrfs_extent_inline_ref *iref)
1282{
1283 struct btrfs_key key;
1284 struct extent_buffer *leaf;
1285 struct btrfs_extent_data_ref *ref1;
1286 struct btrfs_shared_data_ref *ref2;
1287 u32 num_refs = 0;
1288
1289 leaf = path->nodes[0];
1290 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1291 if (iref) {
1292 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1293 BTRFS_EXTENT_DATA_REF_KEY) {
1294 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1295 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1296 } else {
1297 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1298 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1299 }
1300 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1301 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1302 struct btrfs_extent_data_ref);
1303 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1304 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1305 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1306 struct btrfs_shared_data_ref);
1307 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1308#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1309 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1310 struct btrfs_extent_ref_v0 *ref0;
1311 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1312 struct btrfs_extent_ref_v0);
1313 num_refs = btrfs_ref_count_v0(leaf, ref0);
1314#endif
1315 } else {
1316 WARN_ON(1);
1317 }
1318 return num_refs;
1319}
1320
1321static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1322 struct btrfs_root *root,
1323 struct btrfs_path *path,
1324 u64 bytenr, u64 parent,
1325 u64 root_objectid)
1326{
1327 struct btrfs_key key;
1328 int ret;
1329
1330 key.objectid = bytenr;
1331 if (parent) {
1332 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1333 key.offset = parent;
1334 } else {
1335 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1336 key.offset = root_objectid;
1337 }
1338
1339 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1340 if (ret > 0)
1341 ret = -ENOENT;
1342#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1343 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001344 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001345 key.type = BTRFS_EXTENT_REF_V0_KEY;
1346 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1347 if (ret > 0)
1348 ret = -ENOENT;
1349 }
1350#endif
1351 return ret;
1352}
1353
1354static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1355 struct btrfs_root *root,
1356 struct btrfs_path *path,
1357 u64 bytenr, u64 parent,
1358 u64 root_objectid)
1359{
1360 struct btrfs_key key;
1361 int ret;
1362
1363 key.objectid = bytenr;
1364 if (parent) {
1365 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1366 key.offset = parent;
1367 } else {
1368 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1369 key.offset = root_objectid;
1370 }
1371
1372 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001373 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001374 return ret;
1375}
1376
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001377static inline int extent_ref_type(u64 parent, u64 owner)
1378{
1379 int type;
1380 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1381 if (parent > 0)
1382 type = BTRFS_SHARED_BLOCK_REF_KEY;
1383 else
1384 type = BTRFS_TREE_BLOCK_REF_KEY;
1385 } else {
1386 if (parent > 0)
1387 type = BTRFS_SHARED_DATA_REF_KEY;
1388 else
1389 type = BTRFS_EXTENT_DATA_REF_KEY;
1390 }
1391 return type;
1392}
1393
Yan Zheng2c47e6052009-06-27 21:07:35 -04001394static int find_next_key(struct btrfs_path *path, int level,
1395 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396
1397{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001398 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001399 if (!path->nodes[level])
1400 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401 if (path->slots[level] + 1 >=
1402 btrfs_header_nritems(path->nodes[level]))
1403 continue;
1404 if (level == 0)
1405 btrfs_item_key_to_cpu(path->nodes[level], key,
1406 path->slots[level] + 1);
1407 else
1408 btrfs_node_key_to_cpu(path->nodes[level], key,
1409 path->slots[level] + 1);
1410 return 0;
1411 }
1412 return 1;
1413}
1414
1415/*
1416 * look for inline back ref. if back ref is found, *ref_ret is set
1417 * to the address of inline back ref, and 0 is returned.
1418 *
1419 * if back ref isn't found, *ref_ret is set to the address where it
1420 * should be inserted, and -ENOENT is returned.
1421 *
1422 * if insert is true and there are too many inline back refs, the path
1423 * points to the extent item, and -EAGAIN is returned.
1424 *
1425 * NOTE: inline back refs are ordered in the same way that back ref
1426 * items in the tree are ordered.
1427 */
1428static noinline_for_stack
1429int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1430 struct btrfs_root *root,
1431 struct btrfs_path *path,
1432 struct btrfs_extent_inline_ref **ref_ret,
1433 u64 bytenr, u64 num_bytes,
1434 u64 parent, u64 root_objectid,
1435 u64 owner, u64 offset, int insert)
1436{
1437 struct btrfs_key key;
1438 struct extent_buffer *leaf;
1439 struct btrfs_extent_item *ei;
1440 struct btrfs_extent_inline_ref *iref;
1441 u64 flags;
1442 u64 item_size;
1443 unsigned long ptr;
1444 unsigned long end;
1445 int extra_size;
1446 int type;
1447 int want;
1448 int ret;
1449 int err = 0;
1450
1451 key.objectid = bytenr;
1452 key.type = BTRFS_EXTENT_ITEM_KEY;
1453 key.offset = num_bytes;
1454
1455 want = extent_ref_type(parent, owner);
1456 if (insert) {
1457 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001458 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001459 } else
1460 extra_size = -1;
1461 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1462 if (ret < 0) {
1463 err = ret;
1464 goto out;
1465 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 if (ret && !insert) {
1467 err = -ENOENT;
1468 goto out;
1469 }
1470 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001471
1472 leaf = path->nodes[0];
1473 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1474#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1475 if (item_size < sizeof(*ei)) {
1476 if (!insert) {
1477 err = -ENOENT;
1478 goto out;
1479 }
1480 ret = convert_extent_item_v0(trans, root, path, owner,
1481 extra_size);
1482 if (ret < 0) {
1483 err = ret;
1484 goto out;
1485 }
1486 leaf = path->nodes[0];
1487 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1488 }
1489#endif
1490 BUG_ON(item_size < sizeof(*ei));
1491
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001492 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1493 flags = btrfs_extent_flags(leaf, ei);
1494
1495 ptr = (unsigned long)(ei + 1);
1496 end = (unsigned long)ei + item_size;
1497
1498 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1499 ptr += sizeof(struct btrfs_tree_block_info);
1500 BUG_ON(ptr > end);
1501 } else {
1502 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1503 }
1504
1505 err = -ENOENT;
1506 while (1) {
1507 if (ptr >= end) {
1508 WARN_ON(ptr > end);
1509 break;
1510 }
1511 iref = (struct btrfs_extent_inline_ref *)ptr;
1512 type = btrfs_extent_inline_ref_type(leaf, iref);
1513 if (want < type)
1514 break;
1515 if (want > type) {
1516 ptr += btrfs_extent_inline_ref_size(type);
1517 continue;
1518 }
1519
1520 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1521 struct btrfs_extent_data_ref *dref;
1522 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1523 if (match_extent_data_ref(leaf, dref, root_objectid,
1524 owner, offset)) {
1525 err = 0;
1526 break;
1527 }
1528 if (hash_extent_data_ref_item(leaf, dref) <
1529 hash_extent_data_ref(root_objectid, owner, offset))
1530 break;
1531 } else {
1532 u64 ref_offset;
1533 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1534 if (parent > 0) {
1535 if (parent == ref_offset) {
1536 err = 0;
1537 break;
1538 }
1539 if (ref_offset < parent)
1540 break;
1541 } else {
1542 if (root_objectid == ref_offset) {
1543 err = 0;
1544 break;
1545 }
1546 if (ref_offset < root_objectid)
1547 break;
1548 }
1549 }
1550 ptr += btrfs_extent_inline_ref_size(type);
1551 }
1552 if (err == -ENOENT && insert) {
1553 if (item_size + extra_size >=
1554 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1555 err = -EAGAIN;
1556 goto out;
1557 }
1558 /*
1559 * To add new inline back ref, we have to make sure
1560 * there is no corresponding back ref item.
1561 * For simplicity, we just do not add new inline back
1562 * ref if there is any kind of item for this block
1563 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001564 if (find_next_key(path, 0, &key) == 0 &&
1565 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001566 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001567 err = -EAGAIN;
1568 goto out;
1569 }
1570 }
1571 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1572out:
Yan Zheng85d41982009-06-11 08:51:10 -04001573 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001574 path->keep_locks = 0;
1575 btrfs_unlock_up_safe(path, 1);
1576 }
1577 return err;
1578}
1579
1580/*
1581 * helper to add new inline back ref
1582 */
1583static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001584void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1585 struct btrfs_root *root,
1586 struct btrfs_path *path,
1587 struct btrfs_extent_inline_ref *iref,
1588 u64 parent, u64 root_objectid,
1589 u64 owner, u64 offset, int refs_to_add,
1590 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591{
1592 struct extent_buffer *leaf;
1593 struct btrfs_extent_item *ei;
1594 unsigned long ptr;
1595 unsigned long end;
1596 unsigned long item_offset;
1597 u64 refs;
1598 int size;
1599 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001600
1601 leaf = path->nodes[0];
1602 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1603 item_offset = (unsigned long)iref - (unsigned long)ei;
1604
1605 type = extent_ref_type(parent, owner);
1606 size = btrfs_extent_inline_ref_size(type);
1607
Jeff Mahoney143bede2012-03-01 14:56:26 +01001608 btrfs_extend_item(trans, root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001609
1610 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1611 refs = btrfs_extent_refs(leaf, ei);
1612 refs += refs_to_add;
1613 btrfs_set_extent_refs(leaf, ei, refs);
1614 if (extent_op)
1615 __run_delayed_extent_op(extent_op, leaf, ei);
1616
1617 ptr = (unsigned long)ei + item_offset;
1618 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1619 if (ptr < end - size)
1620 memmove_extent_buffer(leaf, ptr + size, ptr,
1621 end - size - ptr);
1622
1623 iref = (struct btrfs_extent_inline_ref *)ptr;
1624 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1625 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1626 struct btrfs_extent_data_ref *dref;
1627 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1628 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1629 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1630 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1631 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1632 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1633 struct btrfs_shared_data_ref *sref;
1634 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1635 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1636 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1637 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1638 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1639 } else {
1640 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1641 }
1642 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001643}
1644
1645static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1646 struct btrfs_root *root,
1647 struct btrfs_path *path,
1648 struct btrfs_extent_inline_ref **ref_ret,
1649 u64 bytenr, u64 num_bytes, u64 parent,
1650 u64 root_objectid, u64 owner, u64 offset)
1651{
1652 int ret;
1653
1654 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1655 bytenr, num_bytes, parent,
1656 root_objectid, owner, offset, 0);
1657 if (ret != -ENOENT)
1658 return ret;
1659
David Sterbab3b4aa72011-04-21 01:20:15 +02001660 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001661 *ref_ret = NULL;
1662
1663 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1664 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1665 root_objectid);
1666 } else {
1667 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1668 root_objectid, owner, offset);
1669 }
1670 return ret;
1671}
1672
1673/*
1674 * helper to update/remove inline back ref
1675 */
1676static noinline_for_stack
Jeff Mahoney143bede2012-03-01 14:56:26 +01001677void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1678 struct btrfs_root *root,
1679 struct btrfs_path *path,
1680 struct btrfs_extent_inline_ref *iref,
1681 int refs_to_mod,
1682 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683{
1684 struct extent_buffer *leaf;
1685 struct btrfs_extent_item *ei;
1686 struct btrfs_extent_data_ref *dref = NULL;
1687 struct btrfs_shared_data_ref *sref = NULL;
1688 unsigned long ptr;
1689 unsigned long end;
1690 u32 item_size;
1691 int size;
1692 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001693 u64 refs;
1694
1695 leaf = path->nodes[0];
1696 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1697 refs = btrfs_extent_refs(leaf, ei);
1698 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1699 refs += refs_to_mod;
1700 btrfs_set_extent_refs(leaf, ei, refs);
1701 if (extent_op)
1702 __run_delayed_extent_op(extent_op, leaf, ei);
1703
1704 type = btrfs_extent_inline_ref_type(leaf, iref);
1705
1706 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1707 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1708 refs = btrfs_extent_data_ref_count(leaf, dref);
1709 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1710 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1711 refs = btrfs_shared_data_ref_count(leaf, sref);
1712 } else {
1713 refs = 1;
1714 BUG_ON(refs_to_mod != -1);
1715 }
1716
1717 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1718 refs += refs_to_mod;
1719
1720 if (refs > 0) {
1721 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1722 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1723 else
1724 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1725 } else {
1726 size = btrfs_extent_inline_ref_size(type);
1727 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1728 ptr = (unsigned long)iref;
1729 end = (unsigned long)ei + item_size;
1730 if (ptr + size < end)
1731 memmove_extent_buffer(leaf, ptr, ptr + size,
1732 end - ptr - size);
1733 item_size -= size;
Jeff Mahoney143bede2012-03-01 14:56:26 +01001734 btrfs_truncate_item(trans, root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735 }
1736 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001737}
1738
1739static noinline_for_stack
1740int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1741 struct btrfs_root *root,
1742 struct btrfs_path *path,
1743 u64 bytenr, u64 num_bytes, u64 parent,
1744 u64 root_objectid, u64 owner,
1745 u64 offset, int refs_to_add,
1746 struct btrfs_delayed_extent_op *extent_op)
1747{
1748 struct btrfs_extent_inline_ref *iref;
1749 int ret;
1750
1751 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1752 bytenr, num_bytes, parent,
1753 root_objectid, owner, offset, 1);
1754 if (ret == 0) {
1755 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001756 update_inline_extent_backref(trans, root, path, iref,
1757 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001758 } else if (ret == -ENOENT) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001759 setup_inline_extent_backref(trans, root, path, iref, parent,
1760 root_objectid, owner, offset,
1761 refs_to_add, extent_op);
1762 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001763 }
1764 return ret;
1765}
1766
1767static int insert_extent_backref(struct btrfs_trans_handle *trans,
1768 struct btrfs_root *root,
1769 struct btrfs_path *path,
1770 u64 bytenr, u64 parent, u64 root_objectid,
1771 u64 owner, u64 offset, int refs_to_add)
1772{
1773 int ret;
1774 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1775 BUG_ON(refs_to_add != 1);
1776 ret = insert_tree_block_ref(trans, root, path, bytenr,
1777 parent, root_objectid);
1778 } else {
1779 ret = insert_extent_data_ref(trans, root, path, bytenr,
1780 parent, root_objectid,
1781 owner, offset, refs_to_add);
1782 }
1783 return ret;
1784}
1785
1786static int remove_extent_backref(struct btrfs_trans_handle *trans,
1787 struct btrfs_root *root,
1788 struct btrfs_path *path,
1789 struct btrfs_extent_inline_ref *iref,
1790 int refs_to_drop, int is_data)
1791{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001792 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001793
1794 BUG_ON(!is_data && refs_to_drop != 1);
1795 if (iref) {
Jeff Mahoney143bede2012-03-01 14:56:26 +01001796 update_inline_extent_backref(trans, root, path, iref,
1797 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001798 } else if (is_data) {
1799 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1800 } else {
1801 ret = btrfs_del_item(trans, root, path);
1802 }
1803 return ret;
1804}
1805
Li Dongyang5378e602011-03-24 10:24:27 +00001806static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001807 u64 start, u64 len)
1808{
Li Dongyang5378e602011-03-24 10:24:27 +00001809 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001810}
Chris Mason15916de2008-11-19 21:17:22 -05001811
Liu Hui1f3c79a2009-01-05 15:57:51 -05001812static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001813 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001814{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001815 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001816 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001817 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001818
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001819
Liu Hui1f3c79a2009-01-05 15:57:51 -05001820 /* Tell the block device(s) that the sectors can be discarded */
Li Dongyang5378e602011-03-24 10:24:27 +00001821 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001822 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001823 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001824 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001825 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001826 int i;
1827
Liu Hui1f3c79a2009-01-05 15:57:51 -05001828
Jan Schmidta1d3c472011-08-04 17:15:33 +02001829 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001830 if (!stripe->dev->can_discard)
1831 continue;
1832
Li Dongyang5378e602011-03-24 10:24:27 +00001833 ret = btrfs_issue_discard(stripe->dev->bdev,
1834 stripe->physical,
1835 stripe->length);
1836 if (!ret)
1837 discarded_bytes += stripe->length;
1838 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001839 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001840
1841 /*
1842 * Just in case we get back EOPNOTSUPP for some reason,
1843 * just ignore the return value so we don't screw up
1844 * people calling discard_extent.
1845 */
1846 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001847 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001848 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001849 }
Li Dongyang5378e602011-03-24 10:24:27 +00001850
1851 if (actual_bytes)
1852 *actual_bytes = discarded_bytes;
1853
Liu Hui1f3c79a2009-01-05 15:57:51 -05001854
1855 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001856}
1857
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001858/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001859int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1860 struct btrfs_root *root,
1861 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001862 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001863{
1864 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001865 struct btrfs_fs_info *fs_info = root->fs_info;
1866
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001867 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1868 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001869
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001871 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1872 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001874 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001875 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001876 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1877 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001879 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001880 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001881 return ret;
1882}
1883
Chris Mason925baed2008-06-25 16:01:30 -04001884static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001885 struct btrfs_root *root,
1886 u64 bytenr, u64 num_bytes,
1887 u64 parent, u64 root_objectid,
1888 u64 owner, u64 offset, int refs_to_add,
1889 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001890{
Chris Mason5caf2a02007-04-02 11:20:42 -04001891 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001893 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 u64 refs;
1895 int ret;
1896 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001897
Chris Mason5caf2a02007-04-02 11:20:42 -04001898 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001899 if (!path)
1900 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001901
Chris Mason3c12ac72008-04-21 12:01:38 -04001902 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001903 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001904 /* this will setup the path even if it fails to insert the back ref */
1905 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1906 path, bytenr, num_bytes, parent,
1907 root_objectid, owner, offset,
1908 refs_to_add, extent_op);
1909 if (ret == 0)
1910 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001911
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001912 if (ret != -EAGAIN) {
1913 err = ret;
1914 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001915 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001916
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001917 leaf = path->nodes[0];
1918 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1919 refs = btrfs_extent_refs(leaf, item);
1920 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1921 if (extent_op)
1922 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001923
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001924 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001925 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001926
Chris Mason3c12ac72008-04-21 12:01:38 -04001927 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001928 path->leave_spinning = 1;
1929
Chris Mason56bec292009-03-13 10:10:06 -04001930 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001931 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001932 path, bytenr, parent, root_objectid,
1933 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001934 if (ret)
1935 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001936out:
Chris Mason74493f72007-12-11 09:25:06 -05001937 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001938 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001939}
1940
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001941static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1942 struct btrfs_root *root,
1943 struct btrfs_delayed_ref_node *node,
1944 struct btrfs_delayed_extent_op *extent_op,
1945 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04001946{
Chris Mason56bec292009-03-13 10:10:06 -04001947 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001948 struct btrfs_delayed_data_ref *ref;
1949 struct btrfs_key ins;
1950 u64 parent = 0;
1951 u64 ref_root = 0;
1952 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04001953
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001954 ins.objectid = node->bytenr;
1955 ins.offset = node->num_bytes;
1956 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04001957
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001958 ref = btrfs_delayed_node_to_data_ref(node);
1959 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1960 parent = ref->parent;
1961 else
1962 ref_root = ref->root;
1963
1964 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1965 if (extent_op) {
1966 BUG_ON(extent_op->update_key);
1967 flags |= extent_op->flags_to_set;
1968 }
1969 ret = alloc_reserved_file_extent(trans, root,
1970 parent, ref_root, flags,
1971 ref->objectid, ref->offset,
1972 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001973 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1974 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1975 node->num_bytes, parent,
1976 ref_root, ref->objectid,
1977 ref->offset, node->ref_mod,
1978 extent_op);
1979 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1980 ret = __btrfs_free_extent(trans, root, node->bytenr,
1981 node->num_bytes, parent,
1982 ref_root, ref->objectid,
1983 ref->offset, node->ref_mod,
1984 extent_op);
1985 } else {
1986 BUG();
1987 }
Chris Mason56bec292009-03-13 10:10:06 -04001988 return ret;
1989}
1990
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001991static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1992 struct extent_buffer *leaf,
1993 struct btrfs_extent_item *ei)
1994{
1995 u64 flags = btrfs_extent_flags(leaf, ei);
1996 if (extent_op->update_flags) {
1997 flags |= extent_op->flags_to_set;
1998 btrfs_set_extent_flags(leaf, ei, flags);
1999 }
2000
2001 if (extent_op->update_key) {
2002 struct btrfs_tree_block_info *bi;
2003 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2004 bi = (struct btrfs_tree_block_info *)(ei + 1);
2005 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2006 }
2007}
2008
2009static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2010 struct btrfs_root *root,
2011 struct btrfs_delayed_ref_node *node,
2012 struct btrfs_delayed_extent_op *extent_op)
2013{
2014 struct btrfs_key key;
2015 struct btrfs_path *path;
2016 struct btrfs_extent_item *ei;
2017 struct extent_buffer *leaf;
2018 u32 item_size;
2019 int ret;
2020 int err = 0;
2021
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002022 if (trans->aborted)
2023 return 0;
2024
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002025 path = btrfs_alloc_path();
2026 if (!path)
2027 return -ENOMEM;
2028
2029 key.objectid = node->bytenr;
2030 key.type = BTRFS_EXTENT_ITEM_KEY;
2031 key.offset = node->num_bytes;
2032
2033 path->reada = 1;
2034 path->leave_spinning = 1;
2035 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2036 path, 0, 1);
2037 if (ret < 0) {
2038 err = ret;
2039 goto out;
2040 }
2041 if (ret > 0) {
2042 err = -EIO;
2043 goto out;
2044 }
2045
2046 leaf = path->nodes[0];
2047 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2048#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2049 if (item_size < sizeof(*ei)) {
2050 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2051 path, (u64)-1, 0);
2052 if (ret < 0) {
2053 err = ret;
2054 goto out;
2055 }
2056 leaf = path->nodes[0];
2057 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2058 }
2059#endif
2060 BUG_ON(item_size < sizeof(*ei));
2061 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2062 __run_delayed_extent_op(extent_op, leaf, ei);
2063
2064 btrfs_mark_buffer_dirty(leaf);
2065out:
2066 btrfs_free_path(path);
2067 return err;
2068}
2069
2070static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2071 struct btrfs_root *root,
2072 struct btrfs_delayed_ref_node *node,
2073 struct btrfs_delayed_extent_op *extent_op,
2074 int insert_reserved)
2075{
2076 int ret = 0;
2077 struct btrfs_delayed_tree_ref *ref;
2078 struct btrfs_key ins;
2079 u64 parent = 0;
2080 u64 ref_root = 0;
2081
2082 ins.objectid = node->bytenr;
2083 ins.offset = node->num_bytes;
2084 ins.type = BTRFS_EXTENT_ITEM_KEY;
2085
2086 ref = btrfs_delayed_node_to_tree_ref(node);
2087 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2088 parent = ref->parent;
2089 else
2090 ref_root = ref->root;
2091
2092 BUG_ON(node->ref_mod != 1);
2093 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2094 BUG_ON(!extent_op || !extent_op->update_flags ||
2095 !extent_op->update_key);
2096 ret = alloc_reserved_tree_block(trans, root,
2097 parent, ref_root,
2098 extent_op->flags_to_set,
2099 &extent_op->key,
2100 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002101 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2102 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2103 node->num_bytes, parent, ref_root,
2104 ref->level, 0, 1, extent_op);
2105 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2106 ret = __btrfs_free_extent(trans, root, node->bytenr,
2107 node->num_bytes, parent, ref_root,
2108 ref->level, 0, 1, extent_op);
2109 } else {
2110 BUG();
2111 }
2112 return ret;
2113}
2114
Chris Mason56bec292009-03-13 10:10:06 -04002115/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2117 struct btrfs_root *root,
2118 struct btrfs_delayed_ref_node *node,
2119 struct btrfs_delayed_extent_op *extent_op,
2120 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002121{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002122 int ret = 0;
2123
2124 if (trans->aborted)
2125 return 0;
2126
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002127 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002128 struct btrfs_delayed_ref_head *head;
2129 /*
2130 * we've hit the end of the chain and we were supposed
2131 * to insert this extent into the tree. But, it got
2132 * deleted before we ever needed to insert it, so all
2133 * we have to do is clean up the accounting
2134 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002135 BUG_ON(extent_op);
2136 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002137 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002138 btrfs_pin_extent(root, node->bytenr,
2139 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002140 if (head->is_data) {
2141 ret = btrfs_del_csums(trans, root,
2142 node->bytenr,
2143 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002144 }
Chris Mason56bec292009-03-13 10:10:06 -04002145 }
Chris Mason56bec292009-03-13 10:10:06 -04002146 mutex_unlock(&head->mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002147 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002148 }
Josef Bacikeb099672009-02-12 09:27:38 -05002149
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002150 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2151 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2152 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2153 insert_reserved);
2154 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2155 node->type == BTRFS_SHARED_DATA_REF_KEY)
2156 ret = run_delayed_data_ref(trans, root, node, extent_op,
2157 insert_reserved);
2158 else
2159 BUG();
2160 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002161}
2162
Chris Mason56bec292009-03-13 10:10:06 -04002163static noinline struct btrfs_delayed_ref_node *
2164select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002165{
Chris Mason56bec292009-03-13 10:10:06 -04002166 struct rb_node *node;
2167 struct btrfs_delayed_ref_node *ref;
2168 int action = BTRFS_ADD_DELAYED_REF;
2169again:
2170 /*
2171 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2172 * this prevents ref count from going down to zero when
2173 * there still are pending delayed ref.
2174 */
2175 node = rb_prev(&head->node.rb_node);
2176 while (1) {
2177 if (!node)
2178 break;
2179 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2180 rb_node);
2181 if (ref->bytenr != head->node.bytenr)
2182 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002183 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002184 return ref;
2185 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002186 }
Chris Mason56bec292009-03-13 10:10:06 -04002187 if (action == BTRFS_ADD_DELAYED_REF) {
2188 action = BTRFS_DROP_DELAYED_REF;
2189 goto again;
2190 }
2191 return NULL;
2192}
2193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002194/*
2195 * Returns 0 on success or if called with an already aborted transaction.
2196 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2197 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002198static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2199 struct btrfs_root *root,
2200 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002201{
Chris Mason56bec292009-03-13 10:10:06 -04002202 struct btrfs_delayed_ref_root *delayed_refs;
2203 struct btrfs_delayed_ref_node *ref;
2204 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002205 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002206 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002207 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002208 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002209 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002210
2211 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002212 while (1) {
2213 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002214 /* pick a new head ref from the cluster list */
2215 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002216 break;
Chris Mason56bec292009-03-13 10:10:06 -04002217
Chris Masonc3e69d52009-03-13 10:17:05 -04002218 locked_ref = list_entry(cluster->next,
2219 struct btrfs_delayed_ref_head, cluster);
2220
2221 /* grab the lock that says we are going to process
2222 * all the refs for this head */
2223 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2224
2225 /*
2226 * we may have dropped the spin lock to get the head
2227 * mutex lock, and that might have given someone else
2228 * time to free the head. If that's true, it has been
2229 * removed from our list and we can move on.
2230 */
2231 if (ret == -EAGAIN) {
2232 locked_ref = NULL;
2233 count++;
2234 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002235 }
2236 }
2237
2238 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002239 * We need to try and merge add/drops of the same ref since we
2240 * can run into issues with relocate dropping the implicit ref
2241 * and then it being added back again before the drop can
2242 * finish. If we merged anything we need to re-loop so we can
2243 * get a good ref.
2244 */
2245 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2246 locked_ref);
2247
2248 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002249 * locked_ref is the head node, so we have to go one
2250 * node back for any delayed ref updates
2251 */
2252 ref = select_delayed_ref(locked_ref);
2253
2254 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002255 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002256 /*
2257 * there are still refs with lower seq numbers in the
2258 * process of being added. Don't run this ref yet.
2259 */
2260 list_del_init(&locked_ref->cluster);
2261 mutex_unlock(&locked_ref->mutex);
2262 locked_ref = NULL;
2263 delayed_refs->num_heads_ready++;
2264 spin_unlock(&delayed_refs->lock);
2265 cond_resched();
2266 spin_lock(&delayed_refs->lock);
2267 continue;
2268 }
2269
2270 /*
Chris Mason56bec292009-03-13 10:10:06 -04002271 * record the must insert reserved flag before we
2272 * drop the spin lock.
2273 */
2274 must_insert_reserved = locked_ref->must_insert_reserved;
2275 locked_ref->must_insert_reserved = 0;
2276
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002277 extent_op = locked_ref->extent_op;
2278 locked_ref->extent_op = NULL;
2279
Chris Mason56bec292009-03-13 10:10:06 -04002280 if (!ref) {
2281 /* All delayed refs have been processed, Go ahead
2282 * and send the head node to run_one_delayed_ref,
2283 * so that any accounting fixes can happen
2284 */
2285 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286
2287 if (extent_op && must_insert_reserved) {
2288 kfree(extent_op);
2289 extent_op = NULL;
2290 }
2291
2292 if (extent_op) {
2293 spin_unlock(&delayed_refs->lock);
2294
2295 ret = run_delayed_extent_op(trans, root,
2296 ref, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002297 kfree(extent_op);
2298
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002299 if (ret) {
Liu Bo37c41462012-11-05 12:42:08 +00002300 list_del_init(&locked_ref->cluster);
2301 mutex_unlock(&locked_ref->mutex);
2302
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002303 printk(KERN_DEBUG "btrfs: run_delayed_extent_op returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002304 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002305 return ret;
2306 }
2307
Chris Mason203bf282012-01-06 15:23:57 -05002308 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002309 }
2310
Chris Masonc3e69d52009-03-13 10:17:05 -04002311 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -04002312 locked_ref = NULL;
2313 }
2314
2315 ref->in_tree = 0;
2316 rb_erase(&ref->rb_node, &delayed_refs->root);
2317 delayed_refs->num_entries--;
Arne Jansen22cd2e72012-08-09 00:16:53 -06002318 if (locked_ref) {
2319 /*
2320 * when we play the delayed ref, also correct the
2321 * ref_mod on head
2322 */
2323 switch (ref->action) {
2324 case BTRFS_ADD_DELAYED_REF:
2325 case BTRFS_ADD_DELAYED_EXTENT:
2326 locked_ref->node.ref_mod -= ref->ref_mod;
2327 break;
2328 case BTRFS_DROP_DELAYED_REF:
2329 locked_ref->node.ref_mod += ref->ref_mod;
2330 break;
2331 default:
2332 WARN_ON(1);
2333 }
2334 }
Chris Mason56bec292009-03-13 10:10:06 -04002335 spin_unlock(&delayed_refs->lock);
2336
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002337 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002338 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002339
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002340 btrfs_put_delayed_ref(ref);
2341 kfree(extent_op);
Chris Masonc3e69d52009-03-13 10:17:05 -04002342 count++;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002343
2344 if (ret) {
Liu Bo37c41462012-11-05 12:42:08 +00002345 if (locked_ref) {
2346 list_del_init(&locked_ref->cluster);
2347 mutex_unlock(&locked_ref->mutex);
2348 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002349 printk(KERN_DEBUG "btrfs: run_one_delayed_ref returned %d\n", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002350 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002351 return ret;
2352 }
2353
Chris Mason203bf282012-01-06 15:23:57 -05002354next:
Chris Mason1887be62009-03-13 10:11:24 -04002355 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002356 spin_lock(&delayed_refs->lock);
2357 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002358 return count;
2359}
2360
Arne Jansen709c0482011-09-12 12:22:57 +02002361#ifdef SCRAMBLE_DELAYED_REFS
2362/*
2363 * Normally delayed refs get processed in ascending bytenr order. This
2364 * correlates in most cases to the order added. To expose dependencies on this
2365 * order, we start to process the tree in the middle instead of the beginning
2366 */
2367static u64 find_middle(struct rb_root *root)
2368{
2369 struct rb_node *n = root->rb_node;
2370 struct btrfs_delayed_ref_node *entry;
2371 int alt = 1;
2372 u64 middle;
2373 u64 first = 0, last = 0;
2374
2375 n = rb_first(root);
2376 if (n) {
2377 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2378 first = entry->bytenr;
2379 }
2380 n = rb_last(root);
2381 if (n) {
2382 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2383 last = entry->bytenr;
2384 }
2385 n = root->rb_node;
2386
2387 while (n) {
2388 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2389 WARN_ON(!entry->in_tree);
2390
2391 middle = entry->bytenr;
2392
2393 if (alt)
2394 n = n->rb_left;
2395 else
2396 n = n->rb_right;
2397
2398 alt = 1 - alt;
2399 }
2400 return middle;
2401}
2402#endif
2403
Arne Jansenbed92ea2012-06-28 18:03:02 +02002404int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2405 struct btrfs_fs_info *fs_info)
2406{
2407 struct qgroup_update *qgroup_update;
2408 int ret = 0;
2409
2410 if (list_empty(&trans->qgroup_ref_list) !=
2411 !trans->delayed_ref_elem.seq) {
2412 /* list without seq or seq without list */
2413 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2414 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2415 trans->delayed_ref_elem.seq);
2416 BUG();
2417 }
2418
2419 if (!trans->delayed_ref_elem.seq)
2420 return 0;
2421
2422 while (!list_empty(&trans->qgroup_ref_list)) {
2423 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2424 struct qgroup_update, list);
2425 list_del(&qgroup_update->list);
2426 if (!ret)
2427 ret = btrfs_qgroup_account_ref(
2428 trans, fs_info, qgroup_update->node,
2429 qgroup_update->extent_op);
2430 kfree(qgroup_update);
2431 }
2432
2433 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2434
2435 return ret;
2436}
2437
Chris Masonc3e69d52009-03-13 10:17:05 -04002438/*
2439 * this starts processing the delayed reference count updates and
2440 * extent insertions we have queued up so far. count can be
2441 * 0, which means to process everything in the tree at the start
2442 * of the run (but not newly added entries), or it can be some target
2443 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002444 *
2445 * Returns 0 on success or if called with an aborted transaction
2446 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002447 */
2448int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2449 struct btrfs_root *root, unsigned long count)
2450{
2451 struct rb_node *node;
2452 struct btrfs_delayed_ref_root *delayed_refs;
2453 struct btrfs_delayed_ref_node *ref;
2454 struct list_head cluster;
2455 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002456 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002457 int run_all = count == (unsigned long)-1;
2458 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002459 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002460
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002461 /* We'll clean this up in btrfs_cleanup_transaction */
2462 if (trans->aborted)
2463 return 0;
2464
Chris Masonc3e69d52009-03-13 10:17:05 -04002465 if (root == root->fs_info->extent_root)
2466 root = root->fs_info->tree_root;
2467
Jan Schmidtedf39272012-06-28 18:04:55 +02002468 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2469
Chris Masonc3e69d52009-03-13 10:17:05 -04002470 delayed_refs = &trans->transaction->delayed_refs;
2471 INIT_LIST_HEAD(&cluster);
2472again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002473 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002474 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002475
Arne Jansen709c0482011-09-12 12:22:57 +02002476#ifdef SCRAMBLE_DELAYED_REFS
2477 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2478#endif
2479
Chris Masonc3e69d52009-03-13 10:17:05 -04002480 if (count == 0) {
2481 count = delayed_refs->num_entries * 2;
2482 run_most = 1;
2483 }
2484 while (1) {
2485 if (!(run_all || run_most) &&
2486 delayed_refs->num_heads_ready < 64)
2487 break;
2488
2489 /*
2490 * go find something we can process in the rbtree. We start at
2491 * the beginning of the tree, and then build a cluster
2492 * of refs to process starting at the first one we are able to
2493 * lock
2494 */
Jan Schmidta1686502011-12-12 16:10:07 +01002495 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002496 ret = btrfs_find_ref_cluster(trans, &cluster,
2497 delayed_refs->run_delayed_start);
2498 if (ret)
2499 break;
2500
2501 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002502 if (ret < 0) {
2503 spin_unlock(&delayed_refs->lock);
2504 btrfs_abort_transaction(trans, root, ret);
2505 return ret;
2506 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002507
2508 count -= min_t(unsigned long, ret, count);
2509
2510 if (count == 0)
2511 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002512
Arne Jansen1fa11e22012-08-06 14:18:51 -06002513 if (delayed_start >= delayed_refs->run_delayed_start) {
2514 if (loops == 0) {
2515 /*
2516 * btrfs_find_ref_cluster looped. let's do one
2517 * more cycle. if we don't run any delayed ref
2518 * during that cycle (because we can't because
2519 * all of them are blocked), bail out.
2520 */
2521 loops = 1;
2522 } else {
2523 /*
2524 * no runnable refs left, stop trying
2525 */
2526 BUG_ON(run_all);
2527 break;
2528 }
2529 }
2530 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002531 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002532 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002533 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002534 }
2535
Chris Mason56bec292009-03-13 10:10:06 -04002536 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002537 if (!list_empty(&trans->new_bgs)) {
2538 spin_unlock(&delayed_refs->lock);
2539 btrfs_create_pending_block_groups(trans, root);
2540 spin_lock(&delayed_refs->lock);
2541 }
2542
Chris Mason56bec292009-03-13 10:10:06 -04002543 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002544 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002545 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002546 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002547
2548 while (node) {
2549 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2550 rb_node);
2551 if (btrfs_delayed_ref_is_head(ref)) {
2552 struct btrfs_delayed_ref_head *head;
2553
2554 head = btrfs_delayed_node_to_head(ref);
2555 atomic_inc(&ref->refs);
2556
2557 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002558 /*
2559 * Mutex was contended, block until it's
2560 * released and try again
2561 */
Chris Mason56bec292009-03-13 10:10:06 -04002562 mutex_lock(&head->mutex);
2563 mutex_unlock(&head->mutex);
2564
2565 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002566 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002567 goto again;
2568 }
2569 node = rb_next(node);
2570 }
2571 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002572 schedule_timeout(1);
2573 goto again;
2574 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002575out:
Chris Masonc3e69d52009-03-13 10:17:05 -04002576 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002577 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002578 return 0;
2579}
2580
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002581int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2582 struct btrfs_root *root,
2583 u64 bytenr, u64 num_bytes, u64 flags,
2584 int is_data)
2585{
2586 struct btrfs_delayed_extent_op *extent_op;
2587 int ret;
2588
2589 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2590 if (!extent_op)
2591 return -ENOMEM;
2592
2593 extent_op->flags_to_set = flags;
2594 extent_op->update_flags = 1;
2595 extent_op->update_key = 0;
2596 extent_op->is_data = is_data ? 1 : 0;
2597
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002598 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2599 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002600 if (ret)
2601 kfree(extent_op);
2602 return ret;
2603}
2604
2605static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2606 struct btrfs_root *root,
2607 struct btrfs_path *path,
2608 u64 objectid, u64 offset, u64 bytenr)
2609{
2610 struct btrfs_delayed_ref_head *head;
2611 struct btrfs_delayed_ref_node *ref;
2612 struct btrfs_delayed_data_ref *data_ref;
2613 struct btrfs_delayed_ref_root *delayed_refs;
2614 struct rb_node *node;
2615 int ret = 0;
2616
2617 ret = -ENOENT;
2618 delayed_refs = &trans->transaction->delayed_refs;
2619 spin_lock(&delayed_refs->lock);
2620 head = btrfs_find_delayed_ref_head(trans, bytenr);
2621 if (!head)
2622 goto out;
2623
2624 if (!mutex_trylock(&head->mutex)) {
2625 atomic_inc(&head->node.refs);
2626 spin_unlock(&delayed_refs->lock);
2627
David Sterbab3b4aa72011-04-21 01:20:15 +02002628 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002629
David Sterba8cc33e52011-05-02 15:29:25 +02002630 /*
2631 * Mutex was contended, block until it's released and let
2632 * caller try again
2633 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002634 mutex_lock(&head->mutex);
2635 mutex_unlock(&head->mutex);
2636 btrfs_put_delayed_ref(&head->node);
2637 return -EAGAIN;
2638 }
2639
2640 node = rb_prev(&head->node.rb_node);
2641 if (!node)
2642 goto out_unlock;
2643
2644 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2645
2646 if (ref->bytenr != bytenr)
2647 goto out_unlock;
2648
2649 ret = 1;
2650 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2651 goto out_unlock;
2652
2653 data_ref = btrfs_delayed_node_to_data_ref(ref);
2654
2655 node = rb_prev(node);
2656 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002657 int seq = ref->seq;
2658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002659 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002660 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002661 goto out_unlock;
2662 }
2663
2664 if (data_ref->root != root->root_key.objectid ||
2665 data_ref->objectid != objectid || data_ref->offset != offset)
2666 goto out_unlock;
2667
2668 ret = 0;
2669out_unlock:
2670 mutex_unlock(&head->mutex);
2671out:
2672 spin_unlock(&delayed_refs->lock);
2673 return ret;
2674}
2675
2676static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2677 struct btrfs_root *root,
2678 struct btrfs_path *path,
2679 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002680{
2681 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002682 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002683 struct btrfs_extent_data_ref *ref;
2684 struct btrfs_extent_inline_ref *iref;
2685 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002686 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002687 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002688 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002689
Chris Masonbe20aa92007-12-17 20:14:01 -05002690 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002691 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002692 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002693
Chris Masonbe20aa92007-12-17 20:14:01 -05002694 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2695 if (ret < 0)
2696 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002697 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002698
2699 ret = -ENOENT;
2700 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002701 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002702
Zheng Yan31840ae2008-09-23 13:14:14 -04002703 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002704 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002705 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002706
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002707 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002708 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002709
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002710 ret = 1;
2711 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2712#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2713 if (item_size < sizeof(*ei)) {
2714 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2715 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002716 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002717#endif
2718 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2719
2720 if (item_size != sizeof(*ei) +
2721 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2722 goto out;
2723
2724 if (btrfs_extent_generation(leaf, ei) <=
2725 btrfs_root_last_snapshot(&root->root_item))
2726 goto out;
2727
2728 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2729 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2730 BTRFS_EXTENT_DATA_REF_KEY)
2731 goto out;
2732
2733 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2734 if (btrfs_extent_refs(leaf, ei) !=
2735 btrfs_extent_data_ref_count(leaf, ref) ||
2736 btrfs_extent_data_ref_root(leaf, ref) !=
2737 root->root_key.objectid ||
2738 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2739 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2740 goto out;
2741
Yan Zhengf321e492008-07-30 09:26:11 -04002742 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002743out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002744 return ret;
2745}
2746
2747int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2748 struct btrfs_root *root,
2749 u64 objectid, u64 offset, u64 bytenr)
2750{
2751 struct btrfs_path *path;
2752 int ret;
2753 int ret2;
2754
2755 path = btrfs_alloc_path();
2756 if (!path)
2757 return -ENOENT;
2758
2759 do {
2760 ret = check_committed_ref(trans, root, path, objectid,
2761 offset, bytenr);
2762 if (ret && ret != -ENOENT)
2763 goto out;
2764
2765 ret2 = check_delayed_ref(trans, root, path, objectid,
2766 offset, bytenr);
2767 } while (ret2 == -EAGAIN);
2768
2769 if (ret2 && ret2 != -ENOENT) {
2770 ret = ret2;
2771 goto out;
2772 }
2773
2774 if (ret != -ENOENT || ret2 != -ENOENT)
2775 ret = 0;
2776out:
Yan Zhengf321e492008-07-30 09:26:11 -04002777 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002778 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2779 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002780 return ret;
2781}
2782
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002783static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002784 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002785 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002786 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002787{
2788 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002789 u64 num_bytes;
2790 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002791 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002792 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002793 struct btrfs_key key;
2794 struct btrfs_file_extent_item *fi;
2795 int i;
2796 int level;
2797 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002798 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002799 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002800
2801 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002802 nritems = btrfs_header_nritems(buf);
2803 level = btrfs_header_level(buf);
2804
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002805 if (!root->ref_cows && level == 0)
2806 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002807
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002808 if (inc)
2809 process_func = btrfs_inc_extent_ref;
2810 else
2811 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002812
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002813 if (full_backref)
2814 parent = buf->start;
2815 else
2816 parent = 0;
2817
Zheng Yan31840ae2008-09-23 13:14:14 -04002818 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002819 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002820 btrfs_item_key_to_cpu(buf, &key, i);
2821 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002822 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002823 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002824 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002825 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002826 BTRFS_FILE_EXTENT_INLINE)
2827 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002828 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2829 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002830 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002831
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002832 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2833 key.offset -= btrfs_file_extent_offset(buf, fi);
2834 ret = process_func(trans, root, bytenr, num_bytes,
2835 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002836 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002837 if (ret)
2838 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002839 } else {
2840 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002841 num_bytes = btrfs_level_size(root, level - 1);
2842 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002843 parent, ref_root, level - 1, 0,
2844 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002845 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002846 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002847 }
2848 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002849 return 0;
2850fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002851 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002852}
2853
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002854int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002855 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002856{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002857 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002858}
Zheng Yan31840ae2008-09-23 13:14:14 -04002859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002860int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002861 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002862{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002863 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002864}
2865
Chris Mason9078a3e2007-04-26 16:46:15 -04002866static int write_one_cache_group(struct btrfs_trans_handle *trans,
2867 struct btrfs_root *root,
2868 struct btrfs_path *path,
2869 struct btrfs_block_group_cache *cache)
2870{
2871 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002872 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002873 unsigned long bi;
2874 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04002875
Chris Mason9078a3e2007-04-26 16:46:15 -04002876 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002877 if (ret < 0)
2878 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002879 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04002880
2881 leaf = path->nodes[0];
2882 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2883 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2884 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002885 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04002886fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002887 if (ret) {
2888 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04002889 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002890 }
Chris Mason9078a3e2007-04-26 16:46:15 -04002891 return 0;
2892
2893}
2894
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002895static struct btrfs_block_group_cache *
2896next_block_group(struct btrfs_root *root,
2897 struct btrfs_block_group_cache *cache)
2898{
2899 struct rb_node *node;
2900 spin_lock(&root->fs_info->block_group_cache_lock);
2901 node = rb_next(&cache->cache_node);
2902 btrfs_put_block_group(cache);
2903 if (node) {
2904 cache = rb_entry(node, struct btrfs_block_group_cache,
2905 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00002906 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04002907 } else
2908 cache = NULL;
2909 spin_unlock(&root->fs_info->block_group_cache_lock);
2910 return cache;
2911}
2912
Josef Bacik0af3d002010-06-21 14:48:16 -04002913static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2914 struct btrfs_trans_handle *trans,
2915 struct btrfs_path *path)
2916{
2917 struct btrfs_root *root = block_group->fs_info->tree_root;
2918 struct inode *inode = NULL;
2919 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05002920 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04002921 int num_pages = 0;
2922 int retries = 0;
2923 int ret = 0;
2924
2925 /*
2926 * If this block group is smaller than 100 megs don't bother caching the
2927 * block group.
2928 */
2929 if (block_group->key.offset < (100 * 1024 * 1024)) {
2930 spin_lock(&block_group->lock);
2931 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2932 spin_unlock(&block_group->lock);
2933 return 0;
2934 }
2935
2936again:
2937 inode = lookup_free_space_inode(root, block_group, path);
2938 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2939 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02002940 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04002941 goto out;
2942 }
2943
2944 if (IS_ERR(inode)) {
2945 BUG_ON(retries);
2946 retries++;
2947
2948 if (block_group->ro)
2949 goto out_free;
2950
2951 ret = create_free_space_inode(root, trans, block_group, path);
2952 if (ret)
2953 goto out_free;
2954 goto again;
2955 }
2956
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002957 /* We've already setup this transaction, go ahead and exit */
2958 if (block_group->cache_generation == trans->transid &&
2959 i_size_read(inode)) {
2960 dcs = BTRFS_DC_SETUP;
2961 goto out_put;
2962 }
2963
Josef Bacik0af3d002010-06-21 14:48:16 -04002964 /*
2965 * We want to set the generation to 0, that way if anything goes wrong
2966 * from here on out we know not to trust this cache when we load up next
2967 * time.
2968 */
2969 BTRFS_I(inode)->generation = 0;
2970 ret = btrfs_update_inode(trans, root, inode);
2971 WARN_ON(ret);
2972
2973 if (i_size_read(inode) > 0) {
2974 ret = btrfs_truncate_free_space_cache(root, trans, path,
2975 inode);
2976 if (ret)
2977 goto out_put;
2978 }
2979
2980 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06002981 if (block_group->cached != BTRFS_CACHE_FINISHED ||
2982 !btrfs_test_opt(root, SPACE_CACHE)) {
2983 /*
2984 * don't bother trying to write stuff out _if_
2985 * a) we're not cached,
2986 * b) we're with nospace_cache mount option.
2987 */
Josef Bacik2b209822010-12-03 13:17:53 -05002988 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04002989 spin_unlock(&block_group->lock);
2990 goto out_put;
2991 }
2992 spin_unlock(&block_group->lock);
2993
Josef Bacik6fc823b2012-08-06 13:46:38 -06002994 /*
2995 * Try to preallocate enough space based on how big the block group is.
2996 * Keep in mind this has to include any pinned space which could end up
2997 * taking up quite a bit since it's not folded into the other space
2998 * cache.
2999 */
3000 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003001 if (!num_pages)
3002 num_pages = 1;
3003
Josef Bacik0af3d002010-06-21 14:48:16 -04003004 num_pages *= 16;
3005 num_pages *= PAGE_CACHE_SIZE;
3006
3007 ret = btrfs_check_data_free_space(inode, num_pages);
3008 if (ret)
3009 goto out_put;
3010
3011 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3012 num_pages, num_pages,
3013 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003014 if (!ret)
3015 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003016 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003017
Josef Bacik0af3d002010-06-21 14:48:16 -04003018out_put:
3019 iput(inode);
3020out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003021 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003022out:
3023 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003024 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003025 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003026 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003027 spin_unlock(&block_group->lock);
3028
3029 return ret;
3030}
3031
Chris Mason96b51792007-10-15 16:15:19 -04003032int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3033 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003034{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003035 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003036 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003037 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003038 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003039
3040 path = btrfs_alloc_path();
3041 if (!path)
3042 return -ENOMEM;
3043
Josef Bacik0af3d002010-06-21 14:48:16 -04003044again:
3045 while (1) {
3046 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3047 while (cache) {
3048 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3049 break;
3050 cache = next_block_group(root, cache);
3051 }
3052 if (!cache) {
3053 if (last == 0)
3054 break;
3055 last = 0;
3056 continue;
3057 }
3058 err = cache_save_setup(cache, trans, path);
3059 last = cache->key.objectid + cache->key.offset;
3060 btrfs_put_block_group(cache);
3061 }
3062
Chris Masond3977122009-01-05 21:25:51 -05003063 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003064 if (last == 0) {
3065 err = btrfs_run_delayed_refs(trans, root,
3066 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003067 if (err) /* File system offline */
3068 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003069 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003070
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003071 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3072 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003073 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3074 btrfs_put_block_group(cache);
3075 goto again;
3076 }
3077
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003078 if (cache->dirty)
3079 break;
3080 cache = next_block_group(root, cache);
3081 }
3082 if (!cache) {
3083 if (last == 0)
3084 break;
3085 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003086 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003087 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003088
Josef Bacik0cb59c92010-07-02 12:14:14 -04003089 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3090 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003091 cache->dirty = 0;
3092 last = cache->key.objectid + cache->key.offset;
3093
3094 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003095 if (err) /* File system offline */
3096 goto out;
3097
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003098 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003099 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003100
Josef Bacik0cb59c92010-07-02 12:14:14 -04003101 while (1) {
3102 /*
3103 * I don't think this is needed since we're just marking our
3104 * preallocated extent as written, but just in case it can't
3105 * hurt.
3106 */
3107 if (last == 0) {
3108 err = btrfs_run_delayed_refs(trans, root,
3109 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003110 if (err) /* File system offline */
3111 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003112 }
3113
3114 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3115 while (cache) {
3116 /*
3117 * Really this shouldn't happen, but it could if we
3118 * couldn't write the entire preallocated extent and
3119 * splitting the extent resulted in a new block.
3120 */
3121 if (cache->dirty) {
3122 btrfs_put_block_group(cache);
3123 goto again;
3124 }
3125 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3126 break;
3127 cache = next_block_group(root, cache);
3128 }
3129 if (!cache) {
3130 if (last == 0)
3131 break;
3132 last = 0;
3133 continue;
3134 }
3135
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003136 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003137
3138 /*
3139 * If we didn't have an error then the cache state is still
3140 * NEED_WRITE, so we can set it to WRITTEN.
3141 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003142 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003143 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3144 last = cache->key.objectid + cache->key.offset;
3145 btrfs_put_block_group(cache);
3146 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003147out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003148
Chris Mason9078a3e2007-04-26 16:46:15 -04003149 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003150 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003151}
3152
Yan Zhengd2fb3432008-12-11 16:30:39 -05003153int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3154{
3155 struct btrfs_block_group_cache *block_group;
3156 int readonly = 0;
3157
3158 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3159 if (!block_group || block_group->ro)
3160 readonly = 1;
3161 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003162 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003163 return readonly;
3164}
3165
Chris Mason593060d2008-03-25 16:50:33 -04003166static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3167 u64 total_bytes, u64 bytes_used,
3168 struct btrfs_space_info **space_info)
3169{
3170 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003171 int i;
3172 int factor;
3173
3174 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3175 BTRFS_BLOCK_GROUP_RAID10))
3176 factor = 2;
3177 else
3178 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003179
3180 found = __find_space_info(info, flags);
3181 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003182 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003183 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003184 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003185 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003186 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003187 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003188 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003189 *space_info = found;
3190 return 0;
3191 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003192 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003193 if (!found)
3194 return -ENOMEM;
3195
Yan, Zhengb742bb82010-05-16 10:46:24 -04003196 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3197 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003198 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003199 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003200 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003201 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003202 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003203 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003204 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003205 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003206 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003207 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003208 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003209 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003210 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003211 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003212 found->flush = 0;
3213 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003214 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003215 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003216 if (flags & BTRFS_BLOCK_GROUP_DATA)
3217 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003218 return 0;
3219}
3220
Chris Mason8790d502008-04-03 16:29:03 -04003221static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3222{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003223 u64 extra_flags = chunk_to_extended(flags) &
3224 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003225
3226 if (flags & BTRFS_BLOCK_GROUP_DATA)
3227 fs_info->avail_data_alloc_bits |= extra_flags;
3228 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3229 fs_info->avail_metadata_alloc_bits |= extra_flags;
3230 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3231 fs_info->avail_system_alloc_bits |= extra_flags;
Chris Mason8790d502008-04-03 16:29:03 -04003232}
Chris Mason593060d2008-03-25 16:50:33 -04003233
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003234/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003235 * returns target flags in extended format or 0 if restripe for this
3236 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003237 *
3238 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003239 */
3240static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3241{
3242 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3243 u64 target = 0;
3244
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003245 if (!bctl)
3246 return 0;
3247
3248 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3249 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3250 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3251 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3252 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3253 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3254 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3255 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3256 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3257 }
3258
3259 return target;
3260}
3261
3262/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003263 * @flags: available profiles in extended format (see ctree.h)
3264 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003265 * Returns reduced profile in chunk format. If profile changing is in
3266 * progress (either running or paused) picks the target profile (if it's
3267 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003268 */
Yan Zheng2b820322008-11-17 21:11:30 -05003269u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003270{
Chris Masoncd02dca2010-12-13 14:56:23 -05003271 /*
3272 * we add in the count of missing devices because we want
3273 * to make sure that any RAID levels on a degraded FS
3274 * continue to be honored.
3275 */
3276 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3277 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003278 u64 target;
Chris Masona061fc82008-05-07 11:43:44 -04003279
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003280 /*
3281 * see if restripe for this chunk_type is in progress, if so
3282 * try to reduce to the target profile
3283 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003284 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003285 target = get_restripe_target(root->fs_info, flags);
3286 if (target) {
3287 /* pick target profile only if it's already available */
3288 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003289 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003290 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003291 }
3292 }
3293 spin_unlock(&root->fs_info->balance_lock);
3294
Chris Masona061fc82008-05-07 11:43:44 -04003295 if (num_devices == 1)
3296 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3297 if (num_devices < 4)
3298 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3299
Chris Masonec44a352008-04-28 15:29:52 -04003300 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3301 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04003302 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04003303 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04003304 }
Chris Masonec44a352008-04-28 15:29:52 -04003305
3306 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04003307 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04003308 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04003309 }
Chris Masonec44a352008-04-28 15:29:52 -04003310
3311 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3312 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3313 (flags & BTRFS_BLOCK_GROUP_RAID10) |
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003314 (flags & BTRFS_BLOCK_GROUP_DUP))) {
Chris Masonec44a352008-04-28 15:29:52 -04003315 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003316 }
3317
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003318 return extended_to_chunk(flags);
Chris Masonec44a352008-04-28 15:29:52 -04003319}
3320
Yan, Zhengb742bb82010-05-16 10:46:24 -04003321static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003322{
Yan, Zhengb742bb82010-05-16 10:46:24 -04003323 if (flags & BTRFS_BLOCK_GROUP_DATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003324 flags |= root->fs_info->avail_data_alloc_bits;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003325 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003326 flags |= root->fs_info->avail_system_alloc_bits;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003327 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003328 flags |= root->fs_info->avail_metadata_alloc_bits;
3329
Yan, Zhengb742bb82010-05-16 10:46:24 -04003330 return btrfs_reduce_alloc_profile(root, flags);
3331}
Josef Bacik6a632092009-02-20 11:00:09 -05003332
Miao Xie6d07bce2011-01-05 10:07:31 +00003333u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003334{
3335 u64 flags;
Josef Bacik6a632092009-02-20 11:00:09 -05003336
Yan, Zhengb742bb82010-05-16 10:46:24 -04003337 if (data)
3338 flags = BTRFS_BLOCK_GROUP_DATA;
3339 else if (root == root->fs_info->chunk_root)
3340 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3341 else
3342 flags = BTRFS_BLOCK_GROUP_METADATA;
3343
3344 return get_alloc_profile(root, flags);
Josef Bacik6a632092009-02-20 11:00:09 -05003345}
3346
Josef Bacik6a632092009-02-20 11:00:09 -05003347/*
3348 * This will check the space that the inode allocates from to make sure we have
3349 * enough space for bytes.
3350 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003351int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003352{
3353 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003354 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003355 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003356 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003357 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003358
3359 /* make sure bytes are sectorsize aligned */
3360 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3361
Li Zefan82d59022011-04-20 10:33:24 +08003362 if (root == root->fs_info->tree_root ||
3363 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003364 alloc_chunk = 0;
3365 committed = 1;
3366 }
3367
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003368 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003369 if (!data_sinfo)
3370 goto alloc;
3371
Josef Bacik6a632092009-02-20 11:00:09 -05003372again:
3373 /* make sure we have enough space to handle the data first */
3374 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003375 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3376 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3377 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003378
3379 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003380 struct btrfs_trans_handle *trans;
3381
Josef Bacik6a632092009-02-20 11:00:09 -05003382 /*
3383 * if we don't have enough free bytes in this space then we need
3384 * to alloc a new chunk.
3385 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003386 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003387 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003388
Chris Mason0e4f8f82011-04-15 16:05:44 -04003389 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003390 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003391alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003392 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003393 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003394 if (IS_ERR(trans))
3395 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003396
3397 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003398 alloc_target,
3399 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003400 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003401 if (ret < 0) {
3402 if (ret != -ENOSPC)
3403 return ret;
3404 else
3405 goto commit_trans;
3406 }
Chris Mason33b4d472009-09-22 14:45:50 -04003407
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003408 if (!data_sinfo)
3409 data_sinfo = fs_info->data_sinfo;
3410
Josef Bacik6a632092009-02-20 11:00:09 -05003411 goto again;
3412 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003413
3414 /*
3415 * If we have less pinned bytes than we want to allocate then
3416 * don't bother committing the transaction, it won't help us.
3417 */
3418 if (data_sinfo->bytes_pinned < bytes)
3419 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003420 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003421
3422 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003423commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003424 if (!committed &&
3425 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003426 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003427 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003428 if (IS_ERR(trans))
3429 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003430 ret = btrfs_commit_transaction(trans, root);
3431 if (ret)
3432 return ret;
3433 goto again;
3434 }
3435
Josef Bacik6a632092009-02-20 11:00:09 -05003436 return -ENOSPC;
3437 }
3438 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003439 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003440 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003441 spin_unlock(&data_sinfo->lock);
3442
Josef Bacik9ed74f22009-09-11 16:12:44 -04003443 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003444}
3445
3446/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003447 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003448 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003449void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003450{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003451 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003452 struct btrfs_space_info *data_sinfo;
3453
3454 /* make sure bytes are sectorsize aligned */
3455 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3456
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003457 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003458 spin_lock(&data_sinfo->lock);
3459 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003460 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003461 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003462 spin_unlock(&data_sinfo->lock);
3463}
3464
Josef Bacik97e728d2009-04-21 17:40:57 -04003465static void force_metadata_allocation(struct btrfs_fs_info *info)
3466{
3467 struct list_head *head = &info->space_info;
3468 struct btrfs_space_info *found;
3469
3470 rcu_read_lock();
3471 list_for_each_entry_rcu(found, head, list) {
3472 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003473 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003474 }
3475 rcu_read_unlock();
3476}
3477
Chris Masone5bc2452010-10-26 13:37:56 -04003478static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003479 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003480{
Josef Bacikfb25e912011-07-26 17:00:46 -04003481 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003482 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003483 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003484 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003485
Chris Mason0e4f8f82011-04-15 16:05:44 -04003486 if (force == CHUNK_ALLOC_FORCE)
3487 return 1;
3488
3489 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003490 * We need to take into account the global rsv because for all intents
3491 * and purposes it's used space. Don't worry about locking the
3492 * global_rsv, it doesn't change except when the transaction commits.
3493 */
Josef Bacik54338b52012-08-14 16:20:52 -04003494 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3495 num_allocated += global_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04003496
3497 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003498 * in limited mode, we want to have some free space up to
3499 * about 1% of the FS size.
3500 */
3501 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003502 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003503 thresh = max_t(u64, 64 * 1024 * 1024,
3504 div_factor_fine(thresh, 1));
3505
3506 if (num_bytes - num_allocated < thresh)
3507 return 1;
3508 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003509
Josef Bacik698d0082012-09-12 14:08:47 -04003510 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003511 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003512 return 1;
3513}
3514
Liu Bo15d1ff82012-03-29 09:57:44 -04003515static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3516{
3517 u64 num_dev;
3518
3519 if (type & BTRFS_BLOCK_GROUP_RAID10 ||
3520 type & BTRFS_BLOCK_GROUP_RAID0)
3521 num_dev = root->fs_info->fs_devices->rw_devices;
3522 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3523 num_dev = 2;
3524 else
3525 num_dev = 1; /* DUP or single */
3526
3527 /* metadata for updaing devices and chunk tree */
3528 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3529}
3530
3531static void check_system_chunk(struct btrfs_trans_handle *trans,
3532 struct btrfs_root *root, u64 type)
3533{
3534 struct btrfs_space_info *info;
3535 u64 left;
3536 u64 thresh;
3537
3538 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3539 spin_lock(&info->lock);
3540 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3541 info->bytes_reserved - info->bytes_readonly;
3542 spin_unlock(&info->lock);
3543
3544 thresh = get_system_chunk_thresh(root, type);
3545 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3546 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3547 left, thresh, type);
3548 dump_space_info(info, 0, 0);
3549 }
3550
3551 if (left < thresh) {
3552 u64 flags;
3553
3554 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3555 btrfs_alloc_chunk(trans, root, flags);
3556 }
3557}
3558
Chris Mason6324fbf2008-03-24 15:01:59 -04003559static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003560 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003561{
3562 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003563 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003564 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003565 int ret = 0;
3566
Chris Mason6324fbf2008-03-24 15:01:59 -04003567 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003568 if (!space_info) {
3569 ret = update_space_info(extent_root->fs_info, flags,
3570 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003571 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003572 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003573 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003574
Josef Bacik6d741192011-04-11 20:20:11 -04003575again:
Josef Bacik25179202008-10-29 14:49:05 -04003576 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003577 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003578 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003579 if (space_info->full) {
3580 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003581 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003582 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003583
Josef Bacik698d0082012-09-12 14:08:47 -04003584 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003585 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003586 return 0;
3587 } else if (space_info->chunk_alloc) {
3588 wait_for_alloc = 1;
3589 } else {
3590 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003591 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003592
Josef Bacik25179202008-10-29 14:49:05 -04003593 spin_unlock(&space_info->lock);
3594
Josef Bacik6d741192011-04-11 20:20:11 -04003595 mutex_lock(&fs_info->chunk_mutex);
3596
3597 /*
3598 * The chunk_mutex is held throughout the entirety of a chunk
3599 * allocation, so once we've acquired the chunk_mutex we know that the
3600 * other guy is done and we need to recheck and see if we should
3601 * allocate.
3602 */
3603 if (wait_for_alloc) {
3604 mutex_unlock(&fs_info->chunk_mutex);
3605 wait_for_alloc = 0;
3606 goto again;
3607 }
3608
Josef Bacik97e728d2009-04-21 17:40:57 -04003609 /*
Josef Bacik67377732010-09-16 16:19:09 -04003610 * If we have mixed data/metadata chunks we want to make sure we keep
3611 * allocating mixed chunks instead of individual chunks.
3612 */
3613 if (btrfs_mixed_space_info(space_info))
3614 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3615
3616 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003617 * if we're doing a data chunk, go ahead and make sure that
3618 * we keep a reasonable number of metadata chunks allocated in the
3619 * FS as well.
3620 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003621 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003622 fs_info->data_chunk_allocations++;
3623 if (!(fs_info->data_chunk_allocations %
3624 fs_info->metadata_ratio))
3625 force_metadata_allocation(fs_info);
3626 }
3627
Liu Bo15d1ff82012-03-29 09:57:44 -04003628 /*
3629 * Check if we have enough space in SYSTEM chunk because we may need
3630 * to update devices.
3631 */
3632 check_system_chunk(trans, extent_root, flags);
3633
Yan Zheng2b820322008-11-17 21:11:30 -05003634 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003635 if (ret < 0 && ret != -ENOSPC)
3636 goto out;
3637
Josef Bacik9ed74f22009-09-11 16:12:44 -04003638 spin_lock(&space_info->lock);
Chris Masond3977122009-01-05 21:25:51 -05003639 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003640 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003641 else
3642 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003643
Chris Mason0e4f8f82011-04-15 16:05:44 -04003644 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003645 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003646 spin_unlock(&space_info->lock);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003647out:
Dan Carpentera25c75d2012-04-18 09:59:29 +03003648 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003649 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003650}
3651
Josef Bacika80c8dc2012-09-06 16:59:33 -04003652static int can_overcommit(struct btrfs_root *root,
3653 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003654 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003655{
3656 u64 profile = btrfs_get_alloc_profile(root, 0);
3657 u64 avail;
3658 u64 used;
3659
3660 used = space_info->bytes_used + space_info->bytes_reserved +
3661 space_info->bytes_pinned + space_info->bytes_readonly +
3662 space_info->bytes_may_use;
3663
3664 spin_lock(&root->fs_info->free_chunk_lock);
3665 avail = root->fs_info->free_chunk_space;
3666 spin_unlock(&root->fs_info->free_chunk_lock);
3667
3668 /*
3669 * If we have dup, raid1 or raid10 then only half of the free
3670 * space is actually useable.
3671 */
3672 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3673 BTRFS_BLOCK_GROUP_RAID1 |
3674 BTRFS_BLOCK_GROUP_RAID10))
3675 avail >>= 1;
3676
3677 /*
Miao Xie561c2942012-10-16 11:32:18 +00003678 * If we aren't flushing all things, let us overcommit up to
3679 * 1/2th of the space. If we can flush, don't let us overcommit
3680 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003681 */
Miao Xie08e007d2012-10-16 11:33:38 +00003682 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003683 avail >>= 3;
3684 else
3685 avail >>= 1;
3686
3687 if (used + bytes < space_info->total_bytes + avail)
3688 return 1;
3689 return 0;
3690}
3691
Yan, Zheng5da9d012010-05-16 10:46:25 -04003692/*
3693 * shrink metadata reservation for delalloc
3694 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003695static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3696 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003697{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003698 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003699 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003700 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003701 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003702 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003703 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003704 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003705 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003706 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003707
Josef Bacik663350a2011-11-03 22:54:25 -04003708 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003709 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003710 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003711
3712 smp_mb();
Josef Bacikf4c738c2012-07-02 17:10:51 -04003713 delalloc_bytes = root->fs_info->delalloc_bytes;
3714 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003715 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003716 return;
Liu Bo6bbe3a92012-09-14 02:58:07 -06003717 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003718 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003719 }
3720
Josef Bacikf4c738c2012-07-02 17:10:51 -04003721 while (delalloc_bytes && loops < 3) {
3722 max_reclaim = min(delalloc_bytes, to_reclaim);
3723 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Curt Wohlgemuth0e175a12011-10-07 21:54:10 -06003724 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages,
Josef Bacikf4c738c2012-07-02 17:10:51 -04003725 WB_REASON_FS_FREE_SPACE);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003726
Josef Bacikdea31f52012-09-06 16:47:00 -04003727 /*
3728 * We need to wait for the async pages to actually start before
3729 * we do anything.
3730 */
3731 wait_event(root->fs_info->async_submit_wait,
3732 !atomic_read(&root->fs_info->async_delalloc_pages));
3733
Miao Xie08e007d2012-10-16 11:33:38 +00003734 if (!trans)
3735 flush = BTRFS_RESERVE_FLUSH_ALL;
3736 else
3737 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003738 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003739 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003740 spin_unlock(&space_info->lock);
3741 break;
3742 }
Josef Bacik0019f102010-10-15 15:18:40 -04003743 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003744
Chris Mason36e39c42011-03-12 07:08:42 -05003745 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003746 if (wait_ordered && !trans) {
Liu Bo6bbe3a92012-09-14 02:58:07 -06003747 btrfs_wait_ordered_extents(root, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003748 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003749 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003750 if (time_left)
3751 break;
3752 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003753 smp_mb();
3754 delalloc_bytes = root->fs_info->delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003755 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003756}
3757
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003758/**
Josef Bacik663350a2011-11-03 22:54:25 -04003759 * maybe_commit_transaction - possibly commit the transaction if its ok to
3760 * @root - the root we're allocating for
3761 * @bytes - the number of bytes we want to reserve
3762 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003763 *
Josef Bacik663350a2011-11-03 22:54:25 -04003764 * This will check to make sure that committing the transaction will actually
3765 * get us somewhere and then commit the transaction if it does. Otherwise it
3766 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003767 */
Josef Bacik663350a2011-11-03 22:54:25 -04003768static int may_commit_transaction(struct btrfs_root *root,
3769 struct btrfs_space_info *space_info,
3770 u64 bytes, int force)
3771{
3772 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3773 struct btrfs_trans_handle *trans;
3774
3775 trans = (struct btrfs_trans_handle *)current->journal_info;
3776 if (trans)
3777 return -EAGAIN;
3778
3779 if (force)
3780 goto commit;
3781
3782 /* See if there is enough pinned space to make this reservation */
3783 spin_lock(&space_info->lock);
3784 if (space_info->bytes_pinned >= bytes) {
3785 spin_unlock(&space_info->lock);
3786 goto commit;
3787 }
3788 spin_unlock(&space_info->lock);
3789
3790 /*
3791 * See if there is some space in the delayed insertion reservation for
3792 * this reservation.
3793 */
3794 if (space_info != delayed_rsv->space_info)
3795 return -ENOSPC;
3796
Liu Bod9b02182012-02-16 18:34:39 +08003797 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003798 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003799 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04003800 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003801 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003802 return -ENOSPC;
3803 }
3804 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08003805 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04003806
3807commit:
3808 trans = btrfs_join_transaction(root);
3809 if (IS_ERR(trans))
3810 return -ENOSPC;
3811
3812 return btrfs_commit_transaction(trans, root);
3813}
3814
Josef Bacik96c3f432012-06-21 14:05:49 -04003815enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04003816 FLUSH_DELAYED_ITEMS_NR = 1,
3817 FLUSH_DELAYED_ITEMS = 2,
3818 FLUSH_DELALLOC = 3,
3819 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04003820 ALLOC_CHUNK = 5,
3821 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04003822};
3823
3824static int flush_space(struct btrfs_root *root,
3825 struct btrfs_space_info *space_info, u64 num_bytes,
3826 u64 orig_bytes, int state)
3827{
3828 struct btrfs_trans_handle *trans;
3829 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003830 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04003831
3832 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04003833 case FLUSH_DELAYED_ITEMS_NR:
3834 case FLUSH_DELAYED_ITEMS:
3835 if (state == FLUSH_DELAYED_ITEMS_NR) {
3836 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
3837
3838 nr = (int)div64_u64(num_bytes, bytes);
3839 if (!nr)
3840 nr = 1;
3841 nr *= 2;
3842 } else {
3843 nr = -1;
3844 }
3845 trans = btrfs_join_transaction(root);
3846 if (IS_ERR(trans)) {
3847 ret = PTR_ERR(trans);
3848 break;
3849 }
3850 ret = btrfs_run_delayed_items_nr(trans, root, nr);
3851 btrfs_end_transaction(trans, root);
3852 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003853 case FLUSH_DELALLOC:
3854 case FLUSH_DELALLOC_WAIT:
3855 shrink_delalloc(root, num_bytes, orig_bytes,
3856 state == FLUSH_DELALLOC_WAIT);
3857 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04003858 case ALLOC_CHUNK:
3859 trans = btrfs_join_transaction(root);
3860 if (IS_ERR(trans)) {
3861 ret = PTR_ERR(trans);
3862 break;
3863 }
3864 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04003865 btrfs_get_alloc_profile(root, 0),
3866 CHUNK_ALLOC_NO_FORCE);
3867 btrfs_end_transaction(trans, root);
3868 if (ret == -ENOSPC)
3869 ret = 0;
3870 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04003871 case COMMIT_TRANS:
3872 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3873 break;
3874 default:
3875 ret = -ENOSPC;
3876 break;
3877 }
3878
3879 return ret;
3880}
Josef Bacik663350a2011-11-03 22:54:25 -04003881/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003882 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
3883 * @root - the root we're allocating for
3884 * @block_rsv - the block_rsv we're allocating for
3885 * @orig_bytes - the number of bytes we want
3886 * @flush - wether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003887 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003888 * This will reserve orgi_bytes number of bytes from the space info associated
3889 * with the block_rsv. If there is not enough space it will make an attempt to
3890 * flush out space to make room. It will do this by flushing delalloc if
3891 * possible or committing the transaction. If flush is 0 then no attempts to
3892 * regain reservations will be made and this will fail if there is not enough
3893 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003894 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003895static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003896 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00003897 u64 orig_bytes,
3898 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04003899{
3900 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04003901 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003902 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04003903 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003904 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003905 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003906
3907again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003908 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003909 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003910 /*
Miao Xie08e007d2012-10-16 11:33:38 +00003911 * We only want to wait if somebody other than us is flushing and we
3912 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003913 */
Miao Xie08e007d2012-10-16 11:33:38 +00003914 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
3915 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003916 spin_unlock(&space_info->lock);
3917 /*
3918 * If we have a trans handle we can't wait because the flusher
3919 * may have to commit the transaction, which would mean we would
3920 * deadlock since we are waiting for the flusher to finish, but
3921 * hold the current transaction open.
3922 */
Josef Bacik663350a2011-11-03 22:54:25 -04003923 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003924 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02003925 ret = wait_event_killable(space_info->wait, !space_info->flush);
3926 /* Must have been killed, return */
3927 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003928 return -EINTR;
3929
3930 spin_lock(&space_info->lock);
3931 }
3932
3933 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04003934 used = space_info->bytes_used + space_info->bytes_reserved +
3935 space_info->bytes_pinned + space_info->bytes_readonly +
3936 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003937
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003938 /*
3939 * The idea here is that we've not already over-reserved the block group
3940 * then we can go ahead and save our reservation first and then start
3941 * flushing if we need to. Otherwise if we've already overcommitted
3942 * lets start flushing stuff first and then come back and try to make
3943 * our reservation.
3944 */
Josef Bacik2bf64752011-09-26 17:12:22 -04003945 if (used <= space_info->total_bytes) {
3946 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04003947 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003948 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04003949 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003950 ret = 0;
3951 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003952 /*
3953 * Ok set num_bytes to orig_bytes since we aren't
3954 * overocmmitted, this way we only try and reclaim what
3955 * we need.
3956 */
3957 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003958 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003959 } else {
3960 /*
3961 * Ok we're over committed, set num_bytes to the overcommitted
3962 * amount plus the amount of bytes that we need for this
3963 * reservation.
3964 */
Josef Bacik2bf64752011-09-26 17:12:22 -04003965 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04003966 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003967 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003968
Josef Bacik44734ed2012-09-28 16:04:19 -04003969 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
3970 space_info->bytes_may_use += orig_bytes;
3971 trace_btrfs_space_reservation(root->fs_info, "space_info",
3972 space_info->flags, orig_bytes,
3973 1);
3974 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04003975 }
3976
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003977 /*
3978 * Couldn't make our reservation, save our place so while we're trying
3979 * to reclaim space we can actually use it instead of somebody else
3980 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00003981 *
3982 * We make the other tasks wait for the flush only when we can flush
3983 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003984 */
Miao Xie08e007d2012-10-16 11:33:38 +00003985 if (ret && flush == BTRFS_RESERVE_FLUSH_ALL) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003986 flushing = true;
3987 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003988 }
3989
Yan, Zhengf0486c62010-05-16 10:46:25 -04003990 spin_unlock(&space_info->lock);
3991
Miao Xie08e007d2012-10-16 11:33:38 +00003992 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003993 goto out;
3994
Josef Bacik96c3f432012-06-21 14:05:49 -04003995 ret = flush_space(root, space_info, num_bytes, orig_bytes,
3996 flush_state);
3997 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00003998
3999 /*
4000 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4001 * would happen. So skip delalloc flush.
4002 */
4003 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4004 (flush_state == FLUSH_DELALLOC ||
4005 flush_state == FLUSH_DELALLOC_WAIT))
4006 flush_state = ALLOC_CHUNK;
4007
Josef Bacik96c3f432012-06-21 14:05:49 -04004008 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004009 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004010 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4011 flush_state < COMMIT_TRANS)
4012 goto again;
4013 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4014 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004015 goto again;
4016
4017out:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004018 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004019 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004020 space_info->flush = 0;
4021 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004022 spin_unlock(&space_info->lock);
4023 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004024 return ret;
4025}
4026
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004027static struct btrfs_block_rsv *get_block_rsv(
4028 const struct btrfs_trans_handle *trans,
4029 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004030{
Josef Bacik4c13d752011-08-30 11:31:29 -04004031 struct btrfs_block_rsv *block_rsv = NULL;
4032
Josef Bacik0e721102012-06-26 16:13:18 -04004033 if (root->ref_cows)
4034 block_rsv = trans->block_rsv;
4035
4036 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004037 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004038
4039 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004040 block_rsv = root->block_rsv;
4041
4042 if (!block_rsv)
4043 block_rsv = &root->fs_info->empty_block_rsv;
4044
4045 return block_rsv;
4046}
4047
4048static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4049 u64 num_bytes)
4050{
4051 int ret = -ENOSPC;
4052 spin_lock(&block_rsv->lock);
4053 if (block_rsv->reserved >= num_bytes) {
4054 block_rsv->reserved -= num_bytes;
4055 if (block_rsv->reserved < block_rsv->size)
4056 block_rsv->full = 0;
4057 ret = 0;
4058 }
4059 spin_unlock(&block_rsv->lock);
4060 return ret;
4061}
4062
4063static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4064 u64 num_bytes, int update_size)
4065{
4066 spin_lock(&block_rsv->lock);
4067 block_rsv->reserved += num_bytes;
4068 if (update_size)
4069 block_rsv->size += num_bytes;
4070 else if (block_rsv->reserved >= block_rsv->size)
4071 block_rsv->full = 1;
4072 spin_unlock(&block_rsv->lock);
4073}
4074
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004075static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4076 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004077 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004078{
4079 struct btrfs_space_info *space_info = block_rsv->space_info;
4080
4081 spin_lock(&block_rsv->lock);
4082 if (num_bytes == (u64)-1)
4083 num_bytes = block_rsv->size;
4084 block_rsv->size -= num_bytes;
4085 if (block_rsv->reserved >= block_rsv->size) {
4086 num_bytes = block_rsv->reserved - block_rsv->size;
4087 block_rsv->reserved = block_rsv->size;
4088 block_rsv->full = 1;
4089 } else {
4090 num_bytes = 0;
4091 }
4092 spin_unlock(&block_rsv->lock);
4093
4094 if (num_bytes > 0) {
4095 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004096 spin_lock(&dest->lock);
4097 if (!dest->full) {
4098 u64 bytes_to_add;
4099
4100 bytes_to_add = dest->size - dest->reserved;
4101 bytes_to_add = min(num_bytes, bytes_to_add);
4102 dest->reserved += bytes_to_add;
4103 if (dest->reserved >= dest->size)
4104 dest->full = 1;
4105 num_bytes -= bytes_to_add;
4106 }
4107 spin_unlock(&dest->lock);
4108 }
4109 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004110 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004111 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004112 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004113 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004114 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004115 spin_unlock(&space_info->lock);
4116 }
4117 }
4118}
4119
4120static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4121 struct btrfs_block_rsv *dst, u64 num_bytes)
4122{
4123 int ret;
4124
4125 ret = block_rsv_use_bytes(src, num_bytes);
4126 if (ret)
4127 return ret;
4128
4129 block_rsv_add_bytes(dst, num_bytes, 1);
4130 return 0;
4131}
4132
Miao Xie66d8f3d2012-09-06 04:02:28 -06004133void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004134{
4135 memset(rsv, 0, sizeof(*rsv));
4136 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004137 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004138}
4139
Miao Xie66d8f3d2012-09-06 04:02:28 -06004140struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4141 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004142{
4143 struct btrfs_block_rsv *block_rsv;
4144 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004145
4146 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4147 if (!block_rsv)
4148 return NULL;
4149
Miao Xie66d8f3d2012-09-06 04:02:28 -06004150 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004151 block_rsv->space_info = __find_space_info(fs_info,
4152 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004153 return block_rsv;
4154}
4155
4156void btrfs_free_block_rsv(struct btrfs_root *root,
4157 struct btrfs_block_rsv *rsv)
4158{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004159 if (!rsv)
4160 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004161 btrfs_block_rsv_release(root, rsv, (u64)-1);
4162 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004163}
4164
Miao Xie08e007d2012-10-16 11:33:38 +00004165int btrfs_block_rsv_add(struct btrfs_root *root,
4166 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4167 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004168{
4169 int ret;
4170
4171 if (num_bytes == 0)
4172 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004173
Miao Xie61b520a2011-11-10 20:45:05 -05004174 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004175 if (!ret) {
4176 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4177 return 0;
4178 }
4179
Yan, Zhengf0486c62010-05-16 10:46:25 -04004180 return ret;
4181}
4182
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004183int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004184 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004185{
4186 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004187 int ret = -ENOSPC;
4188
4189 if (!block_rsv)
4190 return 0;
4191
4192 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004193 num_bytes = div_factor(block_rsv->size, min_factor);
4194 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004195 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004196 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004197
Josef Bacik36ba0222011-10-18 12:15:48 -04004198 return ret;
4199}
4200
Miao Xie08e007d2012-10-16 11:33:38 +00004201int btrfs_block_rsv_refill(struct btrfs_root *root,
4202 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4203 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004204{
4205 u64 num_bytes = 0;
4206 int ret = -ENOSPC;
4207
4208 if (!block_rsv)
4209 return 0;
4210
4211 spin_lock(&block_rsv->lock);
4212 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004213 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004214 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004215 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004216 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004217 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004218
Yan, Zhengf0486c62010-05-16 10:46:25 -04004219 if (!ret)
4220 return 0;
4221
Miao Xieaa38a712011-11-18 17:43:00 +08004222 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004223 if (!ret) {
4224 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004225 return 0;
4226 }
4227
Josef Bacik13553e52011-08-08 13:33:21 -04004228 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004229}
4230
4231int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4232 struct btrfs_block_rsv *dst_rsv,
4233 u64 num_bytes)
4234{
4235 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4236}
4237
4238void btrfs_block_rsv_release(struct btrfs_root *root,
4239 struct btrfs_block_rsv *block_rsv,
4240 u64 num_bytes)
4241{
4242 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4243 if (global_rsv->full || global_rsv == block_rsv ||
4244 block_rsv->space_info != global_rsv->space_info)
4245 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004246 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4247 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004248}
4249
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004250/*
4251 * helper to calculate size of global block reservation.
4252 * the desired value is sum of space used by extent tree,
4253 * checksum tree and root tree
4254 */
4255static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4256{
4257 struct btrfs_space_info *sinfo;
4258 u64 num_bytes;
4259 u64 meta_used;
4260 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004261 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004262
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004263 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4264 spin_lock(&sinfo->lock);
4265 data_used = sinfo->bytes_used;
4266 spin_unlock(&sinfo->lock);
4267
4268 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4269 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004270 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4271 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004272 meta_used = sinfo->bytes_used;
4273 spin_unlock(&sinfo->lock);
4274
4275 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4276 csum_size * 2;
4277 num_bytes += div64_u64(data_used + meta_used, 50);
4278
4279 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004280 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004281
4282 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4283}
4284
4285static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4286{
4287 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4288 struct btrfs_space_info *sinfo = block_rsv->space_info;
4289 u64 num_bytes;
4290
4291 num_bytes = calc_global_metadata_size(fs_info);
4292
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004293 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004294 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004295
4296 block_rsv->size = num_bytes;
4297
4298 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004299 sinfo->bytes_reserved + sinfo->bytes_readonly +
4300 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004301
4302 if (sinfo->total_bytes > num_bytes) {
4303 num_bytes = sinfo->total_bytes - num_bytes;
4304 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004305 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004306 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004307 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004308 }
4309
4310 if (block_rsv->reserved >= block_rsv->size) {
4311 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004312 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004313 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004314 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004315 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004316 block_rsv->reserved = block_rsv->size;
4317 block_rsv->full = 1;
4318 }
David Sterba182608c2011-05-05 13:13:16 +02004319
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004320 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004321 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004322}
4323
Yan, Zhengf0486c62010-05-16 10:46:25 -04004324static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4325{
4326 struct btrfs_space_info *space_info;
4327
4328 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4329 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004330
4331 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004332 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004333 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004334 fs_info->trans_block_rsv.space_info = space_info;
4335 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004336 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004337
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004338 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4339 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4340 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4341 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004342 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004343
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004344 update_global_block_rsv(fs_info);
4345}
4346
4347static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4348{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004349 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4350 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004351 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4352 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4353 WARN_ON(fs_info->trans_block_rsv.size > 0);
4354 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4355 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4356 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004357 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4358 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004359}
4360
Yan, Zhenga22285a2010-05-16 10:48:46 -04004361void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4362 struct btrfs_root *root)
4363{
Josef Bacik0e721102012-06-26 16:13:18 -04004364 if (!trans->block_rsv)
4365 return;
4366
Yan, Zhenga22285a2010-05-16 10:48:46 -04004367 if (!trans->bytes_reserved)
4368 return;
4369
Chris Masone77266e2012-02-24 10:39:05 -05004370 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004371 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004372 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004373 trans->bytes_reserved = 0;
4374}
4375
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004376/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004377int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4378 struct inode *inode)
4379{
4380 struct btrfs_root *root = BTRFS_I(inode)->root;
4381 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4382 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4383
4384 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004385 * We need to hold space in order to delete our orphan item once we've
4386 * added it, so this takes the reservation so we can release it later
4387 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004388 */
Chris Masonff5714c2011-05-28 07:00:39 -04004389 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004390 trace_btrfs_space_reservation(root->fs_info, "orphan",
4391 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004392 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4393}
4394
4395void btrfs_orphan_release_metadata(struct inode *inode)
4396{
4397 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004398 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004399 trace_btrfs_space_reservation(root->fs_info, "orphan",
4400 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004401 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4402}
4403
Yan, Zhenga22285a2010-05-16 10:48:46 -04004404int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4405 struct btrfs_pending_snapshot *pending)
4406{
4407 struct btrfs_root *root = pending->root;
4408 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4409 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4410 /*
Miao Xie48c03c42012-09-06 04:03:56 -06004411 * two for root back/forward refs, two for directory entries,
4412 * one for root of the snapshot and one for parent inode.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004413 */
Miao Xie48c03c42012-09-06 04:03:56 -06004414 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004415 dst_rsv->space_info = src_rsv->space_info;
4416 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4417}
4418
Josef Bacik7709cde2011-08-04 10:25:02 -04004419/**
4420 * drop_outstanding_extent - drop an outstanding extent
4421 * @inode: the inode we're dropping the extent for
4422 *
4423 * This is called when we are freeing up an outstanding extent, either called
4424 * after an error or after an extent is written. This will return the number of
4425 * reserved extents that need to be freed. This must be called with
4426 * BTRFS_I(inode)->lock held.
4427 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004428static unsigned drop_outstanding_extent(struct inode *inode)
4429{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004430 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004431 unsigned dropped_extents = 0;
4432
Josef Bacik9e0baf62011-07-15 15:16:44 +00004433 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4434 BTRFS_I(inode)->outstanding_extents--;
4435
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004436 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004437 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4438 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004439 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004440
Josef Bacik9e0baf62011-07-15 15:16:44 +00004441 /*
4442 * If we have more or the same amount of outsanding extents than we have
4443 * reserved then we need to leave the reserved extents count alone.
4444 */
4445 if (BTRFS_I(inode)->outstanding_extents >=
4446 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004447 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004448
4449 dropped_extents = BTRFS_I(inode)->reserved_extents -
4450 BTRFS_I(inode)->outstanding_extents;
4451 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004452 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004453}
4454
Josef Bacik7709cde2011-08-04 10:25:02 -04004455/**
4456 * calc_csum_metadata_size - return the amount of metada space that must be
4457 * reserved/free'd for the given bytes.
4458 * @inode: the inode we're manipulating
4459 * @num_bytes: the number of bytes in question
4460 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4461 *
4462 * This adjusts the number of csum_bytes in the inode and then returns the
4463 * correct amount of metadata that must either be reserved or freed. We
4464 * calculate how many checksums we can fit into one leaf and then divide the
4465 * number of bytes that will need to be checksumed by this value to figure out
4466 * how many checksums will be required. If we are adding bytes then the number
4467 * may go up and we will return the number of additional bytes that must be
4468 * reserved. If it is going down we will return the number of bytes that must
4469 * be freed.
4470 *
4471 * This must be called with BTRFS_I(inode)->lock held.
4472 */
4473static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4474 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004475{
Josef Bacik7709cde2011-08-04 10:25:02 -04004476 struct btrfs_root *root = BTRFS_I(inode)->root;
4477 u64 csum_size;
4478 int num_csums_per_leaf;
4479 int num_csums;
4480 int old_csums;
4481
4482 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4483 BTRFS_I(inode)->csum_bytes == 0)
4484 return 0;
4485
4486 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4487 if (reserve)
4488 BTRFS_I(inode)->csum_bytes += num_bytes;
4489 else
4490 BTRFS_I(inode)->csum_bytes -= num_bytes;
4491 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4492 num_csums_per_leaf = (int)div64_u64(csum_size,
4493 sizeof(struct btrfs_csum_item) +
4494 sizeof(struct btrfs_disk_key));
4495 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4496 num_csums = num_csums + num_csums_per_leaf - 1;
4497 num_csums = num_csums / num_csums_per_leaf;
4498
4499 old_csums = old_csums + num_csums_per_leaf - 1;
4500 old_csums = old_csums / num_csums_per_leaf;
4501
4502 /* No change, no need to reserve more */
4503 if (old_csums == num_csums)
4504 return 0;
4505
4506 if (reserve)
4507 return btrfs_calc_trans_metadata_size(root,
4508 num_csums - old_csums);
4509
4510 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004511}
4512
4513int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4514{
4515 struct btrfs_root *root = BTRFS_I(inode)->root;
4516 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004517 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004518 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004519 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004520 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004521 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004522 int ret;
4523
Josef Bacik660d3f62011-12-09 11:18:51 -05004524 /* Need to be holding the i_mutex here if we aren't free space cache */
Liu Bo83eea1f2012-07-10 05:28:39 -06004525 if (btrfs_is_free_space_inode(inode))
Miao Xie08e007d2012-10-16 11:33:38 +00004526 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc09544e2011-08-30 10:19:10 -04004527
Miao Xie08e007d2012-10-16 11:33:38 +00004528 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4529 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004530 schedule_timeout(1);
4531
Josef Bacikf2486792012-01-13 12:09:22 -05004532 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004533 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004534
Josef Bacik9e0baf62011-07-15 15:16:44 +00004535 spin_lock(&BTRFS_I(inode)->lock);
4536 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004537
Josef Bacik9e0baf62011-07-15 15:16:44 +00004538 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004539 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004540 nr_extents = BTRFS_I(inode)->outstanding_extents -
4541 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004542
4543 /*
4544 * Add an item to reserve for updating the inode when we complete the
4545 * delalloc io.
4546 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004547 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4548 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004549 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004550 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004551 }
4552
4553 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004554 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004555 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004556 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004557
Arne Jansenc5567232011-09-14 15:44:05 +02004558 if (root->fs_info->quota_enabled) {
4559 ret = btrfs_qgroup_reserve(root, num_bytes +
4560 nr_extents * root->leafsize);
Dan Carpenter55e591f2012-07-30 02:15:15 -06004561 if (ret) {
4562 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Arne Jansenc5567232011-09-14 15:44:05 +02004563 return ret;
Dan Carpenter55e591f2012-07-30 02:15:15 -06004564 }
Arne Jansenc5567232011-09-14 15:44:05 +02004565 }
4566
Josef Bacik36ba0222011-10-18 12:15:48 -04004567 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004568 if (ret) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004569 u64 to_free = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004570 unsigned dropped;
Josef Bacik7ed49f12011-08-19 15:45:52 -04004571
Josef Bacik7709cde2011-08-04 10:25:02 -04004572 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004573 dropped = drop_outstanding_extent(inode);
Josef Bacik7ed49f12011-08-19 15:45:52 -04004574 /*
Josef Bacik660d3f62011-12-09 11:18:51 -05004575 * If the inodes csum_bytes is the same as the original
4576 * csum_bytes then we know we haven't raced with any free()ers
4577 * so we can just reduce our inodes csum bytes and carry on.
4578 * Otherwise we have to do the normal free thing to account for
4579 * the case that the free side didn't free up its reserve
4580 * because of this outstanding reservation.
Josef Bacik7ed49f12011-08-19 15:45:52 -04004581 */
Josef Bacik660d3f62011-12-09 11:18:51 -05004582 if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4583 calc_csum_metadata_size(inode, num_bytes, 0);
4584 else
4585 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4586 spin_unlock(&BTRFS_I(inode)->lock);
4587 if (dropped)
4588 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4589
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004590 if (to_free) {
Josef Bacik7ed49f12011-08-19 15:45:52 -04004591 btrfs_block_rsv_release(root, block_rsv, to_free);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004592 trace_btrfs_space_reservation(root->fs_info,
4593 "delalloc",
4594 btrfs_ino(inode),
4595 to_free, 0);
4596 }
Josef Bacikf2486792012-01-13 12:09:22 -05004597 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004598 return ret;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004599 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004600
Josef Bacik660d3f62011-12-09 11:18:51 -05004601 spin_lock(&BTRFS_I(inode)->lock);
4602 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004603 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4604 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004605 nr_extents--;
4606 }
4607 BTRFS_I(inode)->reserved_extents += nr_extents;
4608 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikf2486792012-01-13 12:09:22 -05004609 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004610
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004611 if (to_reserve)
4612 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4613 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004614 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4615
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004616 return 0;
4617}
4618
Josef Bacik7709cde2011-08-04 10:25:02 -04004619/**
4620 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4621 * @inode: the inode to release the reservation for
4622 * @num_bytes: the number of bytes we're releasing
4623 *
4624 * This will release the metadata reservation for an inode. This can be called
4625 * once we complete IO for a given set of bytes to release their metadata
4626 * reservations.
4627 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004628void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4629{
4630 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004631 u64 to_free = 0;
4632 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004633
4634 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004635 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004636 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004637
Josef Bacik7709cde2011-08-04 10:25:02 -04004638 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4639 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004640 if (dropped > 0)
4641 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004642
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004643 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4644 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004645 if (root->fs_info->quota_enabled) {
4646 btrfs_qgroup_free(root, num_bytes +
4647 dropped * root->leafsize);
4648 }
4649
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004650 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4651 to_free);
4652}
4653
Josef Bacik7709cde2011-08-04 10:25:02 -04004654/**
4655 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4656 * @inode: inode we're writing to
4657 * @num_bytes: the number of bytes we want to allocate
4658 *
4659 * This will do the following things
4660 *
4661 * o reserve space in the data space info for num_bytes
4662 * o reserve space in the metadata space info based on number of outstanding
4663 * extents and how much csums will be needed
4664 * o add to the inodes ->delalloc_bytes
4665 * o add it to the fs_info's delalloc inodes list.
4666 *
4667 * This will return 0 for success and -ENOSPC if there is no space left.
4668 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004669int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4670{
4671 int ret;
4672
4673 ret = btrfs_check_data_free_space(inode, num_bytes);
4674 if (ret)
4675 return ret;
4676
4677 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4678 if (ret) {
4679 btrfs_free_reserved_data_space(inode, num_bytes);
4680 return ret;
4681 }
4682
4683 return 0;
4684}
4685
Josef Bacik7709cde2011-08-04 10:25:02 -04004686/**
4687 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4688 * @inode: inode we're releasing space for
4689 * @num_bytes: the number of bytes we want to free up
4690 *
4691 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4692 * called in the case that we don't need the metadata AND data reservations
4693 * anymore. So if there is an error or we insert an inline extent.
4694 *
4695 * This function will release the metadata space that was not used and will
4696 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4697 * list if there are no delalloc bytes left.
4698 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004699void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4700{
4701 btrfs_delalloc_release_metadata(inode, num_bytes);
4702 btrfs_free_reserved_data_space(inode, num_bytes);
4703}
4704
Chris Mason9078a3e2007-04-26 16:46:15 -04004705static int update_block_group(struct btrfs_trans_handle *trans,
4706 struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004707 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04004708{
Josef Bacik0af3d002010-06-21 14:48:16 -04004709 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04004710 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04004711 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004712 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04004713 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04004714 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04004715
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004716 /* block accounting for super block */
4717 spin_lock(&info->delalloc_lock);
David Sterba6c417612011-04-13 15:41:04 +02004718 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004719 if (alloc)
4720 old_val += num_bytes;
4721 else
4722 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02004723 btrfs_set_super_bytes_used(info->super_copy, old_val);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004724 spin_unlock(&info->delalloc_lock);
4725
Chris Masond3977122009-01-05 21:25:51 -05004726 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04004727 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004728 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004729 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004730 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4731 BTRFS_BLOCK_GROUP_RAID1 |
4732 BTRFS_BLOCK_GROUP_RAID10))
4733 factor = 2;
4734 else
4735 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04004736 /*
4737 * If this block group has free space cache written out, we
4738 * need to make sure to load it if we are removing space. This
4739 * is because we need the unpinning stage to actually add the
4740 * space back to the block group, otherwise we will leak space.
4741 */
4742 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Josef Bacikb8399de2010-12-08 09:15:11 -05004743 cache_block_group(cache, trans, NULL, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04004744
Chris Masondb945352007-10-15 16:15:53 -04004745 byte_in_group = bytenr - cache->key.objectid;
4746 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04004747
Josef Bacik25179202008-10-29 14:49:05 -04004748 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004749 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04004750
Josef Bacik73bc1872011-10-03 14:07:49 -04004751 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04004752 cache->disk_cache_state < BTRFS_DC_CLEAR)
4753 cache->disk_cache_state = BTRFS_DC_CLEAR;
4754
Josef Bacik0f9dd462008-09-23 13:14:11 -04004755 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04004756 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04004757 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04004758 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04004759 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004760 btrfs_set_block_group_used(&cache->item, old_val);
4761 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04004762 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004763 cache->space_info->bytes_used += num_bytes;
4764 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004765 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004766 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004767 } else {
Chris Masondb945352007-10-15 16:15:53 -04004768 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04004769 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004770 cache->pinned += num_bytes;
4771 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004772 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004773 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04004774 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004775 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004776
Yan, Zhengf0486c62010-05-16 10:46:25 -04004777 set_extent_dirty(info->pinned_extents,
4778 bytenr, bytenr + num_bytes - 1,
4779 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004780 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04004781 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004782 total -= num_bytes;
4783 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004784 }
4785 return 0;
4786}
Chris Mason6324fbf2008-03-24 15:01:59 -04004787
Chris Masona061fc82008-05-07 11:43:44 -04004788static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4789{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004790 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004791 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004792
4793 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4794 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004795 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004796
Yan Zhengd2fb3432008-12-11 16:30:39 -05004797 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004798 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004799
4800 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004801}
4802
Yan, Zhengf0486c62010-05-16 10:46:25 -04004803static int pin_down_extent(struct btrfs_root *root,
4804 struct btrfs_block_group_cache *cache,
4805 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004806{
Yan Zheng11833d62009-09-11 16:11:19 -04004807 spin_lock(&cache->space_info->lock);
4808 spin_lock(&cache->lock);
4809 cache->pinned += num_bytes;
4810 cache->space_info->bytes_pinned += num_bytes;
4811 if (reserved) {
4812 cache->reserved -= num_bytes;
4813 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004814 }
Yan Zheng11833d62009-09-11 16:11:19 -04004815 spin_unlock(&cache->lock);
4816 spin_unlock(&cache->space_info->lock);
4817
Yan, Zhengf0486c62010-05-16 10:46:25 -04004818 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4819 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004820 return 0;
4821}
Chris Mason9078a3e2007-04-26 16:46:15 -04004822
Yan, Zhengf0486c62010-05-16 10:46:25 -04004823/*
4824 * this function must be called within transaction
4825 */
4826int btrfs_pin_extent(struct btrfs_root *root,
4827 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004828{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004829 struct btrfs_block_group_cache *cache;
4830
4831 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004832 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04004833
4834 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4835
4836 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004837 return 0;
4838}
Zheng Yane8569812008-09-26 10:05:48 -04004839
Yan, Zhengf0486c62010-05-16 10:46:25 -04004840/*
Chris Masone688b7252011-10-31 20:52:39 -04004841 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04004842 */
Chris Masone688b7252011-10-31 20:52:39 -04004843int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
4844 struct btrfs_root *root,
4845 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004846{
Chris Masone688b7252011-10-31 20:52:39 -04004847 struct btrfs_block_group_cache *cache;
4848
4849 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004850 BUG_ON(!cache); /* Logic error */
Chris Masone688b7252011-10-31 20:52:39 -04004851
4852 /*
4853 * pull in the free space cache (if any) so that our pin
4854 * removes the free space from the cache. We have load_only set
4855 * to one because the slow code to read in the free extents does check
4856 * the pinned extents.
4857 */
4858 cache_block_group(cache, trans, root, 1);
4859
4860 pin_down_extent(root, cache, bytenr, num_bytes, 0);
4861
4862 /* remove us from the free space cache (if we're there at all) */
4863 btrfs_remove_free_space(cache, bytenr, num_bytes);
4864 btrfs_put_block_group(cache);
4865 return 0;
4866}
4867
Josef Bacikfb25e912011-07-26 17:00:46 -04004868/**
4869 * btrfs_update_reserved_bytes - update the block_group and space info counters
4870 * @cache: The cache we are manipulating
4871 * @num_bytes: The number of bytes in question
4872 * @reserve: One of the reservation enums
4873 *
4874 * This is called by the allocator when it reserves space, or by somebody who is
4875 * freeing space that was never actually used on disk. For example if you
4876 * reserve some space for a new leaf in transaction A and before transaction A
4877 * commits you free that leaf, you call this with reserve set to 0 in order to
4878 * clear the reservation.
4879 *
4880 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4881 * ENOSPC accounting. For data we handle the reservation through clearing the
4882 * delalloc bits in the io_tree. We have to do this since we could end up
4883 * allocating less disk space for the amount of data we have reserved in the
4884 * case of compression.
4885 *
4886 * If this is a reservation and the block group has become read only we cannot
4887 * make the reservation and return -EAGAIN, otherwise this function always
4888 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04004889 */
Josef Bacikfb25e912011-07-26 17:00:46 -04004890static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4891 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004892{
Josef Bacikfb25e912011-07-26 17:00:46 -04004893 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004894 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004895
Josef Bacikfb25e912011-07-26 17:00:46 -04004896 spin_lock(&space_info->lock);
4897 spin_lock(&cache->lock);
4898 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004899 if (cache->ro) {
4900 ret = -EAGAIN;
4901 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04004902 cache->reserved += num_bytes;
4903 space_info->bytes_reserved += num_bytes;
4904 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004905 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004906 "space_info", space_info->flags,
4907 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04004908 space_info->bytes_may_use -= num_bytes;
4909 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004910 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004911 } else {
4912 if (cache->ro)
4913 space_info->bytes_readonly += num_bytes;
4914 cache->reserved -= num_bytes;
4915 space_info->bytes_reserved -= num_bytes;
4916 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004917 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004918 spin_unlock(&cache->lock);
4919 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004920 return ret;
4921}
4922
Jeff Mahoney143bede2012-03-01 14:56:26 +01004923void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04004924 struct btrfs_root *root)
4925{
4926 struct btrfs_fs_info *fs_info = root->fs_info;
4927 struct btrfs_caching_control *next;
4928 struct btrfs_caching_control *caching_ctl;
4929 struct btrfs_block_group_cache *cache;
4930
4931 down_write(&fs_info->extent_commit_sem);
4932
4933 list_for_each_entry_safe(caching_ctl, next,
4934 &fs_info->caching_block_groups, list) {
4935 cache = caching_ctl->block_group;
4936 if (block_group_cache_done(cache)) {
4937 cache->last_byte_to_unpin = (u64)-1;
4938 list_del_init(&caching_ctl->list);
4939 put_caching_control(caching_ctl);
4940 } else {
4941 cache->last_byte_to_unpin = caching_ctl->progress;
4942 }
4943 }
4944
4945 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4946 fs_info->pinned_extents = &fs_info->freed_extents[1];
4947 else
4948 fs_info->pinned_extents = &fs_info->freed_extents[0];
4949
4950 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004951
4952 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004953}
4954
4955static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4956{
4957 struct btrfs_fs_info *fs_info = root->fs_info;
4958 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04004959 struct btrfs_space_info *space_info;
4960 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04004961 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04004962 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04004963
4964 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04004965 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04004966 if (!cache ||
4967 start >= cache->key.objectid + cache->key.offset) {
4968 if (cache)
4969 btrfs_put_block_group(cache);
4970 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004971 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04004972 }
4973
4974 len = cache->key.objectid + cache->key.offset - start;
4975 len = min(len, end + 1 - start);
4976
4977 if (start < cache->last_byte_to_unpin) {
4978 len = min(len, cache->last_byte_to_unpin - start);
4979 btrfs_add_free_space(cache, start, len);
4980 }
Josef Bacik25179202008-10-29 14:49:05 -04004981
Yan, Zhengf0486c62010-05-16 10:46:25 -04004982 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04004983 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004984
Josef Bacik7b398f82012-10-22 15:52:28 -04004985 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004986 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004987 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04004988 space_info->bytes_pinned -= len;
4989 if (cache->ro) {
4990 space_info->bytes_readonly += len;
4991 readonly = true;
4992 }
Josef Bacik25179202008-10-29 14:49:05 -04004993 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04004994 if (!readonly && global_rsv->space_info == space_info) {
4995 spin_lock(&global_rsv->lock);
4996 if (!global_rsv->full) {
4997 len = min(len, global_rsv->size -
4998 global_rsv->reserved);
4999 global_rsv->reserved += len;
5000 space_info->bytes_may_use += len;
5001 if (global_rsv->reserved >= global_rsv->size)
5002 global_rsv->full = 1;
5003 }
5004 spin_unlock(&global_rsv->lock);
5005 }
5006 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005007 }
5008
5009 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005010 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005011 return 0;
5012}
5013
5014int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005015 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005016{
Yan Zheng11833d62009-09-11 16:11:19 -04005017 struct btrfs_fs_info *fs_info = root->fs_info;
5018 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005019 u64 start;
5020 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005021 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005022
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005023 if (trans->aborted)
5024 return 0;
5025
Yan Zheng11833d62009-09-11 16:11:19 -04005026 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5027 unpin = &fs_info->freed_extents[1];
5028 else
5029 unpin = &fs_info->freed_extents[0];
5030
Chris Masond3977122009-01-05 21:25:51 -05005031 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005032 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005033 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005034 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005035 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005036
Li Dongyang5378e602011-03-24 10:24:27 +00005037 if (btrfs_test_opt(root, DISCARD))
5038 ret = btrfs_discard_extent(root, start,
5039 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005040
Chris Mason1a5bc162007-10-15 16:15:26 -04005041 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005042 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005043 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005044 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005045
Chris Masone20d96d2007-03-22 12:13:20 -04005046 return 0;
5047}
5048
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005049static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5050 struct btrfs_root *root,
5051 u64 bytenr, u64 num_bytes, u64 parent,
5052 u64 root_objectid, u64 owner_objectid,
5053 u64 owner_offset, int refs_to_drop,
5054 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005055{
Chris Masone2fa7222007-03-12 16:22:34 -04005056 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005057 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005058 struct btrfs_fs_info *info = root->fs_info;
5059 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005060 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005061 struct btrfs_extent_item *ei;
5062 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005063 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005064 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005065 int extent_slot = 0;
5066 int found_extent = 0;
5067 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005068 u32 item_size;
5069 u64 refs;
Chris Mason037e6392007-03-07 11:50:24 -05005070
Chris Mason5caf2a02007-04-02 11:20:42 -04005071 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005072 if (!path)
5073 return -ENOMEM;
5074
Chris Mason3c12ac72008-04-21 12:01:38 -04005075 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005076 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005077
5078 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5079 BUG_ON(!is_data && refs_to_drop != 1);
5080
5081 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5082 bytenr, num_bytes, parent,
5083 root_objectid, owner_objectid,
5084 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005085 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005086 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005087 while (extent_slot >= 0) {
5088 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005089 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005090 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005091 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005092 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5093 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005094 found_extent = 1;
5095 break;
5096 }
5097 if (path->slots[0] - extent_slot > 5)
5098 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005099 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005100 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005101#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5102 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5103 if (found_extent && item_size < sizeof(*ei))
5104 found_extent = 0;
5105#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005106 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005107 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005108 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005109 NULL, refs_to_drop,
5110 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005111 if (ret) {
5112 btrfs_abort_transaction(trans, extent_root, ret);
5113 goto out;
5114 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005115 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005116 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005117
5118 key.objectid = bytenr;
5119 key.type = BTRFS_EXTENT_ITEM_KEY;
5120 key.offset = num_bytes;
5121
Zheng Yan31840ae2008-09-23 13:14:14 -04005122 ret = btrfs_search_slot(trans, extent_root,
5123 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005124 if (ret) {
5125 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05005126 ", was looking for %llu\n", ret,
5127 (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005128 if (ret > 0)
5129 btrfs_print_leaf(extent_root,
5130 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005131 }
David Sterba005d6422012-09-18 07:52:32 -06005132 if (ret < 0) {
5133 btrfs_abort_transaction(trans, extent_root, ret);
5134 goto out;
5135 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005136 extent_slot = path->slots[0];
5137 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005138 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005139 btrfs_print_leaf(extent_root, path->nodes[0]);
5140 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05005141 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005142 "parent %llu root %llu owner %llu offset %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05005143 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04005144 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05005145 (unsigned long long)root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005146 (unsigned long long)owner_objectid,
5147 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005148 } else {
David Sterba005d6422012-09-18 07:52:32 -06005149 btrfs_abort_transaction(trans, extent_root, ret);
5150 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005151 }
Chris Mason5f39d392007-10-15 16:14:19 -04005152
5153 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005154 item_size = btrfs_item_size_nr(leaf, extent_slot);
5155#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5156 if (item_size < sizeof(*ei)) {
5157 BUG_ON(found_extent || extent_slot != path->slots[0]);
5158 ret = convert_extent_item_v0(trans, extent_root, path,
5159 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005160 if (ret < 0) {
5161 btrfs_abort_transaction(trans, extent_root, ret);
5162 goto out;
5163 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005164
David Sterbab3b4aa72011-04-21 01:20:15 +02005165 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005166 path->leave_spinning = 1;
5167
5168 key.objectid = bytenr;
5169 key.type = BTRFS_EXTENT_ITEM_KEY;
5170 key.offset = num_bytes;
5171
5172 ret = btrfs_search_slot(trans, extent_root, &key, path,
5173 -1, 1);
5174 if (ret) {
5175 printk(KERN_ERR "umm, got %d back from search"
5176 ", was looking for %llu\n", ret,
5177 (unsigned long long)bytenr);
5178 btrfs_print_leaf(extent_root, path->nodes[0]);
5179 }
David Sterba005d6422012-09-18 07:52:32 -06005180 if (ret < 0) {
5181 btrfs_abort_transaction(trans, extent_root, ret);
5182 goto out;
5183 }
5184
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005185 extent_slot = path->slots[0];
5186 leaf = path->nodes[0];
5187 item_size = btrfs_item_size_nr(leaf, extent_slot);
5188 }
5189#endif
5190 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005191 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005192 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005193 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5194 struct btrfs_tree_block_info *bi;
5195 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5196 bi = (struct btrfs_tree_block_info *)(ei + 1);
5197 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005198 }
5199
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005200 refs = btrfs_extent_refs(leaf, ei);
5201 BUG_ON(refs < refs_to_drop);
5202 refs -= refs_to_drop;
5203
5204 if (refs > 0) {
5205 if (extent_op)
5206 __run_delayed_extent_op(extent_op, leaf, ei);
5207 /*
5208 * In the case of inline back ref, reference count will
5209 * be updated by remove_extent_backref
5210 */
5211 if (iref) {
5212 BUG_ON(!found_extent);
5213 } else {
5214 btrfs_set_extent_refs(leaf, ei, refs);
5215 btrfs_mark_buffer_dirty(leaf);
5216 }
5217 if (found_extent) {
5218 ret = remove_extent_backref(trans, extent_root, path,
5219 iref, refs_to_drop,
5220 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005221 if (ret) {
5222 btrfs_abort_transaction(trans, extent_root, ret);
5223 goto out;
5224 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005225 }
5226 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005227 if (found_extent) {
5228 BUG_ON(is_data && refs_to_drop !=
5229 extent_data_ref_count(root, path, iref));
5230 if (iref) {
5231 BUG_ON(path->slots[0] != extent_slot);
5232 } else {
5233 BUG_ON(path->slots[0] != extent_slot + 1);
5234 path->slots[0] = extent_slot;
5235 num_to_del = 2;
5236 }
Chris Mason78fae272007-03-25 11:35:08 -04005237 }
Chris Masonb9473432009-03-13 11:00:37 -04005238
Chris Mason952fcca2008-02-18 16:33:44 -05005239 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5240 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005241 if (ret) {
5242 btrfs_abort_transaction(trans, extent_root, ret);
5243 goto out;
5244 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005245 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005246
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005247 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005248 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005249 if (ret) {
5250 btrfs_abort_transaction(trans, extent_root, ret);
5251 goto out;
5252 }
Chris Mason459931e2008-12-10 09:10:46 -05005253 }
5254
Yan, Zhengf0486c62010-05-16 10:46:25 -04005255 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005256 if (ret) {
5257 btrfs_abort_transaction(trans, extent_root, ret);
5258 goto out;
5259 }
Chris Masona28ec192007-03-06 20:08:01 -05005260 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005261out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005262 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005263 return ret;
5264}
5265
5266/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005267 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005268 * delayed ref for that extent as well. This searches the delayed ref tree for
5269 * a given extent, and if there are no other delayed refs to be processed, it
5270 * removes it from the tree.
5271 */
5272static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5273 struct btrfs_root *root, u64 bytenr)
5274{
5275 struct btrfs_delayed_ref_head *head;
5276 struct btrfs_delayed_ref_root *delayed_refs;
5277 struct btrfs_delayed_ref_node *ref;
5278 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005279 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005280
5281 delayed_refs = &trans->transaction->delayed_refs;
5282 spin_lock(&delayed_refs->lock);
5283 head = btrfs_find_delayed_ref_head(trans, bytenr);
5284 if (!head)
5285 goto out;
5286
5287 node = rb_prev(&head->node.rb_node);
5288 if (!node)
5289 goto out;
5290
5291 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5292
5293 /* there are still entries for this ref, we can't drop it */
5294 if (ref->bytenr == bytenr)
5295 goto out;
5296
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005297 if (head->extent_op) {
5298 if (!head->must_insert_reserved)
5299 goto out;
5300 kfree(head->extent_op);
5301 head->extent_op = NULL;
5302 }
5303
Chris Mason1887be62009-03-13 10:11:24 -04005304 /*
5305 * waiting for the lock here would deadlock. If someone else has it
5306 * locked they are already in the process of dropping it anyway
5307 */
5308 if (!mutex_trylock(&head->mutex))
5309 goto out;
5310
5311 /*
5312 * at this point we have a head with no other entries. Go
5313 * ahead and process it.
5314 */
5315 head->node.in_tree = 0;
5316 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005317
Chris Mason1887be62009-03-13 10:11:24 -04005318 delayed_refs->num_entries--;
5319
5320 /*
5321 * we don't take a ref on the node because we're removing it from the
5322 * tree, so we just steal the ref the tree was holding.
5323 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005324 delayed_refs->num_heads--;
5325 if (list_empty(&head->cluster))
5326 delayed_refs->num_heads_ready--;
5327
5328 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005329 spin_unlock(&delayed_refs->lock);
5330
Yan, Zhengf0486c62010-05-16 10:46:25 -04005331 BUG_ON(head->extent_op);
5332 if (head->must_insert_reserved)
5333 ret = 1;
5334
5335 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005336 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005337 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005338out:
5339 spin_unlock(&delayed_refs->lock);
5340 return 0;
5341}
5342
Yan, Zhengf0486c62010-05-16 10:46:25 -04005343void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5344 struct btrfs_root *root,
5345 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005346 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005347{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005348 struct btrfs_block_group_cache *cache = NULL;
5349 int ret;
5350
5351 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005352 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5353 buf->start, buf->len,
5354 parent, root->root_key.objectid,
5355 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005356 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005357 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005358 }
5359
5360 if (!last_ref)
5361 return;
5362
Yan, Zhengf0486c62010-05-16 10:46:25 -04005363 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005364
5365 if (btrfs_header_generation(buf) == trans->transid) {
5366 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5367 ret = check_ref_cleanup(trans, root, buf->start);
5368 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005369 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005370 }
5371
5372 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5373 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005374 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005375 }
5376
5377 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5378
5379 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005380 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005381 }
5382out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005383 /*
5384 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5385 * anymore.
5386 */
5387 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005388 btrfs_put_block_group(cache);
5389}
5390
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005391/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005392int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5393 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5394 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005395{
5396 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005397 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005398
Chris Mason56bec292009-03-13 10:10:06 -04005399 /*
5400 * tree log blocks never actually go into the extent allocation
5401 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005402 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005403 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5404 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005405 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005406 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005407 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005408 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005409 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5410 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005411 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005412 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005413 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005414 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5415 num_bytes,
5416 parent, root_objectid, owner,
5417 offset, BTRFS_DROP_DELAYED_REF,
5418 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005419 }
Chris Mason925baed2008-06-25 16:01:30 -04005420 return ret;
5421}
5422
Chris Mason87ee04e2007-11-30 11:30:34 -05005423static u64 stripe_align(struct btrfs_root *root, u64 val)
5424{
5425 u64 mask = ((u64)root->stripesize - 1);
5426 u64 ret = (val + mask) & ~mask;
5427 return ret;
5428}
5429
Chris Masonfec577f2007-02-26 10:40:21 -05005430/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005431 * when we wait for progress in the block group caching, its because
5432 * our allocation attempt failed at least once. So, we must sleep
5433 * and let some progress happen before we try again.
5434 *
5435 * This function will sleep at least once waiting for new free space to
5436 * show up, and then it will check the block group free space numbers
5437 * for our min num_bytes. Another option is to have it go ahead
5438 * and look in the rbtree for a free extent of a given size, but this
5439 * is a good start.
5440 */
5441static noinline int
5442wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5443 u64 num_bytes)
5444{
Yan Zheng11833d62009-09-11 16:11:19 -04005445 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005446 DEFINE_WAIT(wait);
5447
Yan Zheng11833d62009-09-11 16:11:19 -04005448 caching_ctl = get_caching_control(cache);
5449 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005450 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005451
Yan Zheng11833d62009-09-11 16:11:19 -04005452 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005453 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005454
5455 put_caching_control(caching_ctl);
5456 return 0;
5457}
5458
5459static noinline int
5460wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5461{
5462 struct btrfs_caching_control *caching_ctl;
5463 DEFINE_WAIT(wait);
5464
5465 caching_ctl = get_caching_control(cache);
5466 if (!caching_ctl)
5467 return 0;
5468
5469 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5470
5471 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005472 return 0;
5473}
5474
Ilya Dryomov7738a532012-03-27 17:09:17 +03005475static int __get_block_group_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005476{
5477 int index;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005478
5479 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005480 index = 0;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005481 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005482 index = 1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005483 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005484 index = 2;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005485 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Yan, Zhengb742bb82010-05-16 10:46:24 -04005486 index = 3;
5487 else
5488 index = 4;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005489
Yan, Zhengb742bb82010-05-16 10:46:24 -04005490 return index;
5491}
5492
Ilya Dryomov7738a532012-03-27 17:09:17 +03005493static int get_block_group_index(struct btrfs_block_group_cache *cache)
5494{
5495 return __get_block_group_index(cache->flags);
5496}
5497
Josef Bacik817d52f2009-07-13 21:29:25 -04005498enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005499 LOOP_CACHING_NOWAIT = 0,
5500 LOOP_CACHING_WAIT = 1,
5501 LOOP_ALLOC_CHUNK = 2,
5502 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005503};
5504
5505/*
Chris Masonfec577f2007-02-26 10:40:21 -05005506 * walks the btree of allocated extents and find a hole of a given size.
5507 * The key ins is changed to record the hole:
5508 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005509 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005510 * ins->offset == number of blocks
5511 * Any available blocks before search_start are skipped.
5512 */
Chris Masond3977122009-01-05 21:25:51 -05005513static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005514 struct btrfs_root *orig_root,
5515 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005516 u64 hint_byte, struct btrfs_key *ins,
Ilya Dryomove0f54062011-06-18 20:26:38 +00005517 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005518{
Josef Bacik80eb2342008-10-29 14:49:05 -04005519 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005520 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005521 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005522 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005523 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005524 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005525 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005526 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005527 int loop = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005528 int index = 0;
Josef Bacikfb25e912011-07-26 17:00:46 -04005529 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5530 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005531 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005532 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005533 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005534 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08005535 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005536
Chris Masondb945352007-10-15 16:15:53 -04005537 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04005538 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04005539 ins->objectid = 0;
5540 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005541
Josef Bacik3f7de032011-11-10 08:29:20 -05005542 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5543
Josef Bacik2552d172009-04-03 10:14:19 -04005544 space_info = __find_space_info(root->fs_info, data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005545 if (!space_info) {
Ilya Dryomove0f54062011-06-18 20:26:38 +00005546 printk(KERN_ERR "No space info for %llu\n", data);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005547 return -ENOSPC;
5548 }
Josef Bacik2552d172009-04-03 10:14:19 -04005549
Josef Bacik67377732010-09-16 16:19:09 -04005550 /*
5551 * If the space info is for both data and metadata it means we have a
5552 * small filesystem and we can't use the clustering stuff.
5553 */
5554 if (btrfs_mixed_space_info(space_info))
5555 use_cluster = false;
5556
Josef Bacik67377732010-09-16 16:19:09 -04005557 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005558 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05005559 if (!btrfs_test_opt(root, SSD))
5560 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04005561 }
5562
Josef Bacik67377732010-09-16 16:19:09 -04005563 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5564 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005565 last_ptr = &root->fs_info->data_alloc_cluster;
5566 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005567
Chris Mason239b14b2008-03-24 15:02:07 -04005568 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005569 spin_lock(&last_ptr->lock);
5570 if (last_ptr->block_group)
5571 hint_byte = last_ptr->window_start;
5572 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005573 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005574
Chris Masona061fc82008-05-07 11:43:44 -04005575 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04005576 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04005577
Josef Bacik817d52f2009-07-13 21:29:25 -04005578 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005579 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005580
Josef Bacik2552d172009-04-03 10:14:19 -04005581 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04005582 block_group = btrfs_lookup_block_group(root->fs_info,
5583 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005584 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04005585 /*
5586 * we don't want to use the block group if it doesn't match our
5587 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05005588 *
5589 * However if we are re-searching with an ideal block group
5590 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04005591 */
5592 if (block_group && block_group_bits(block_group, data) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005593 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005594 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005595 if (list_empty(&block_group->list) ||
5596 block_group->ro) {
5597 /*
5598 * someone is removing this block group,
5599 * we can't jump into the have_block_group
5600 * target because our list pointers are not
5601 * valid
5602 */
5603 btrfs_put_block_group(block_group);
5604 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05005605 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005606 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04005607 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005608 }
Josef Bacik2552d172009-04-03 10:14:19 -04005609 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005610 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005611 }
Chris Mason42e70e72008-11-07 18:17:11 -05005612 }
Josef Bacik2552d172009-04-03 10:14:19 -04005613search:
Miao Xie60d2adb2011-09-09 17:34:35 +08005614 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04005615 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005616 list_for_each_entry(block_group, &space_info->block_groups[index],
5617 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04005618 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04005619 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05005620
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005621 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00005622 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005623 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005624
Chris Mason83a50de2010-12-13 15:06:46 -05005625 /*
5626 * this can happen if we end up cycling through all the
5627 * raid types, but we want to make sure we only allocate
5628 * for the proper type.
5629 */
5630 if (!block_group_bits(block_group, data)) {
5631 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5632 BTRFS_BLOCK_GROUP_RAID1 |
5633 BTRFS_BLOCK_GROUP_RAID10;
5634
5635 /*
5636 * if they asked for extra copies and this block group
5637 * doesn't provide them, bail. This does allow us to
5638 * fill raid0 from raid1.
5639 */
5640 if ((data & extra) && !(block_group->flags & extra))
5641 goto loop;
5642 }
5643
Josef Bacik2552d172009-04-03 10:14:19 -04005644have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05005645 cached = block_group_cache_done(block_group);
5646 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05005647 found_uncached_bg = true;
Josef Bacikb8399de2010-12-08 09:15:11 -05005648 ret = cache_block_group(block_group, trans,
Josef Bacik285ff5a2012-01-13 15:27:45 -05005649 orig_root, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005650 BUG_ON(ret < 0);
5651 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005652 }
5653
Josef Bacikea6a4782008-11-20 12:16:16 -05005654 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04005655 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005656
Josef Bacik0a243252009-09-11 16:11:20 -04005657 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005658 * Ok we want to try and use the cluster allocator, so
5659 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005660 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005661 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005662 /*
5663 * the refill lock keeps out other
5664 * people trying to start a new cluster
5665 */
5666 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005667 used_block_group = last_ptr->block_group;
5668 if (used_block_group != block_group &&
5669 (!used_block_group ||
5670 used_block_group->ro ||
5671 !block_group_bits(used_block_group, data))) {
5672 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04005673 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005674 }
Chris Mason44fb5512009-06-04 15:34:51 -04005675
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005676 if (used_block_group != block_group)
5677 btrfs_get_block_group(used_block_group);
5678
5679 offset = btrfs_alloc_from_cluster(used_block_group,
5680 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005681 if (offset) {
5682 /* we have a block, we're done */
5683 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005684 trace_btrfs_reserve_extent_cluster(root,
5685 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005686 goto checks;
5687 }
5688
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005689 WARN_ON(last_ptr->block_group != used_block_group);
5690 if (used_block_group != block_group) {
5691 btrfs_put_block_group(used_block_group);
5692 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005693 }
Chris Mason44fb5512009-06-04 15:34:51 -04005694refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005695 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005696 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
5697 * set up a new clusters, so lets just skip it
5698 * and let the allocator find whatever block
5699 * it can find. If we reach this point, we
5700 * will have tried the cluster allocator
5701 * plenty of times and not have found
5702 * anything, so we are likely way too
5703 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005704 * anything.
5705 *
5706 * However, if the cluster is taken from the
5707 * current block group, release the cluster
5708 * first, so that we stand a better chance of
5709 * succeeding in the unclustered
5710 * allocation. */
5711 if (loop >= LOOP_NO_EMPTY_SIZE &&
5712 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005713 spin_unlock(&last_ptr->refill_lock);
5714 goto unclustered_alloc;
5715 }
5716
Chris Masonfa9c0d792009-04-03 09:47:43 -04005717 /*
5718 * this cluster didn't work out, free it and
5719 * start over
5720 */
5721 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5722
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005723 if (loop >= LOOP_NO_EMPTY_SIZE) {
5724 spin_unlock(&last_ptr->refill_lock);
5725 goto unclustered_alloc;
5726 }
5727
Chris Masonfa9c0d792009-04-03 09:47:43 -04005728 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04005729 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005730 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05005731 search_start, num_bytes,
Chris Masonfa9c0d792009-04-03 09:47:43 -04005732 empty_cluster + empty_size);
5733 if (ret == 0) {
5734 /*
5735 * now pull our allocation out of this
5736 * cluster
5737 */
5738 offset = btrfs_alloc_from_cluster(block_group,
5739 last_ptr, num_bytes,
5740 search_start);
5741 if (offset) {
5742 /* we found one, proceed */
5743 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05005744 trace_btrfs_reserve_extent_cluster(root,
5745 block_group, search_start,
5746 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005747 goto checks;
5748 }
Josef Bacik0a243252009-09-11 16:11:20 -04005749 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5750 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005751 spin_unlock(&last_ptr->refill_lock);
5752
Josef Bacik0a243252009-09-11 16:11:20 -04005753 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005754 wait_block_group_cache_progress(block_group,
5755 num_bytes + empty_cluster + empty_size);
5756 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005757 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005758
Chris Masonfa9c0d792009-04-03 09:47:43 -04005759 /*
5760 * at this point we either didn't find a cluster
5761 * or we weren't able to allocate a block from our
5762 * cluster. Free the cluster we've been trying
5763 * to use, and go to the next block group
5764 */
Josef Bacik0a243252009-09-11 16:11:20 -04005765 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005766 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04005767 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005768 }
5769
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005770unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02005771 spin_lock(&block_group->free_space_ctl->tree_lock);
5772 if (cached &&
5773 block_group->free_space_ctl->free_space <
5774 num_bytes + empty_cluster + empty_size) {
5775 spin_unlock(&block_group->free_space_ctl->tree_lock);
5776 goto loop;
5777 }
5778 spin_unlock(&block_group->free_space_ctl->tree_lock);
5779
Josef Bacik6226cb02009-04-03 10:14:18 -04005780 offset = btrfs_find_space_for_alloc(block_group, search_start,
5781 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005782 /*
5783 * If we didn't find a chunk, and we haven't failed on this
5784 * block group before, and this block group is in the middle of
5785 * caching and we are ok with waiting, then go ahead and wait
5786 * for progress to be made, and set failed_alloc to true.
5787 *
5788 * If failed_alloc is true then we've already waited on this
5789 * block group once and should move on to the next block group.
5790 */
5791 if (!offset && !failed_alloc && !cached &&
5792 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005793 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005794 num_bytes + empty_size);
5795 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04005796 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005797 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08005798 if (!cached)
5799 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005800 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04005801 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005802checks:
Josef Bacik6226cb02009-04-03 10:14:18 -04005803 search_start = stripe_align(root, offset);
Chris Masone37c9e62007-05-09 20:13:14 -04005804
Josef Bacik2552d172009-04-03 10:14:19 -04005805 /* move on to the next group */
5806 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005807 used_block_group->key.objectid + used_block_group->key.offset) {
5808 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005809 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005810 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005811
Josef Bacik6226cb02009-04-03 10:14:18 -04005812 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005813 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04005814 search_start - offset);
5815 BUG_ON(offset > search_start);
5816
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005817 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04005818 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005819 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005820 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005821 goto loop;
5822 }
Yan Zheng11833d62009-09-11 16:11:19 -04005823
Josef Bacik2552d172009-04-03 10:14:19 -04005824 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005825 ins->objectid = search_start;
5826 ins->offset = num_bytes;
5827
Josef Bacik3f7de032011-11-10 08:29:20 -05005828 trace_btrfs_reserve_extent(orig_root, block_group,
5829 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005830 if (used_block_group != block_group)
5831 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04005832 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005833 break;
5834loop:
Josef Bacik0a243252009-09-11 16:11:20 -04005835 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005836 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005837 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005838 if (used_block_group != block_group)
5839 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04005840 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005841 }
5842 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005843
Miao Xie60d2adb2011-09-09 17:34:35 +08005844 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
5845 goto search;
5846
Yan, Zhengb742bb82010-05-16 10:46:24 -04005847 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5848 goto search;
5849
Josef Bacik285ff5a2012-01-13 15:27:45 -05005850 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05005851 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5852 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04005853 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5854 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5855 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5856 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04005857 */
Josef Bacik723bda22011-05-27 16:11:38 -04005858 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04005859 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04005860 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04005861 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik698d0082012-09-12 14:08:47 -04005862 ret = do_chunk_alloc(trans, root, data,
Josef Bacikea658ba2012-09-11 16:57:25 -04005863 CHUNK_ALLOC_FORCE);
5864 /*
5865 * Do not bail out on ENOSPC since we
5866 * can do more things.
5867 */
5868 if (ret < 0 && ret != -ENOSPC) {
5869 btrfs_abort_transaction(trans,
5870 root, ret);
5871 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04005872 }
Josef Bacik723bda22011-05-27 16:11:38 -04005873 }
5874
5875 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005876 empty_size = 0;
5877 empty_cluster = 0;
5878 }
Chris Mason42e70e72008-11-07 18:17:11 -05005879
Josef Bacik723bda22011-05-27 16:11:38 -04005880 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04005881 } else if (!ins->objectid) {
5882 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04005883 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04005884 ret = 0;
5885 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005886out:
Chris Mason0b86a832008-03-24 15:01:56 -04005887
Chris Mason0f70abe2007-02-28 16:46:22 -05005888 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005889}
Chris Masonec44a352008-04-28 15:29:52 -04005890
Josef Bacik9ed74f22009-09-11 16:12:44 -04005891static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5892 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04005893{
5894 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005895 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005896
Josef Bacik9ed74f22009-09-11 16:12:44 -04005897 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04005898 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
5899 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05005900 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04005901 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005902 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05005903 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005904 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5905 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07005906 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005907 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005908 (unsigned long long)info->bytes_pinned,
5909 (unsigned long long)info->bytes_reserved,
5910 (unsigned long long)info->bytes_may_use,
5911 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005912 spin_unlock(&info->lock);
5913
5914 if (!dump_block_groups)
5915 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005916
Josef Bacik80eb2342008-10-29 14:49:05 -04005917 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04005918again:
5919 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04005920 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06005921 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05005922 (unsigned long long)cache->key.objectid,
5923 (unsigned long long)cache->key.offset,
5924 (unsigned long long)btrfs_block_group_used(&cache->item),
5925 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06005926 (unsigned long long)cache->reserved,
5927 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04005928 btrfs_dump_free_space(cache, bytes);
5929 spin_unlock(&cache->lock);
5930 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04005931 if (++index < BTRFS_NR_RAID_TYPES)
5932 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04005933 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005934}
Zheng Yane8569812008-09-26 10:05:48 -04005935
Yan Zheng11833d62009-09-11 16:11:19 -04005936int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5937 struct btrfs_root *root,
5938 u64 num_bytes, u64 min_alloc_size,
5939 u64 empty_size, u64 hint_byte,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005940 struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05005941{
Miao Xie9e622d62012-01-26 15:01:12 -05005942 bool final_tried = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005943 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04005944
Josef Bacik6a632092009-02-20 11:00:09 -05005945 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04005946again:
Chris Masondb945352007-10-15 16:15:53 -04005947 WARN_ON(num_bytes < root->sectorsize);
5948 ret = find_free_extent(trans, root, num_bytes, empty_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005949 hint_byte, ins, data);
Chris Mason3b951512008-04-17 11:29:12 -04005950
Miao Xie9e622d62012-01-26 15:01:12 -05005951 if (ret == -ENOSPC) {
5952 if (!final_tried) {
5953 num_bytes = num_bytes >> 1;
5954 num_bytes = num_bytes & ~(root->sectorsize - 1);
5955 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05005956 if (num_bytes == min_alloc_size)
5957 final_tried = true;
5958 goto again;
5959 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
5960 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005961
Miao Xie9e622d62012-01-26 15:01:12 -05005962 sinfo = __find_space_info(root->fs_info, data);
5963 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5964 "wanted %llu\n", (unsigned long long)data,
5965 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01005966 if (sinfo)
5967 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05005968 }
Chris Mason925baed2008-06-25 16:01:30 -04005969 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005970
liubo1abe9b82011-03-24 11:18:59 +00005971 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5972
Josef Bacik0f9dd462008-09-23 13:14:11 -04005973 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005974}
5975
Chris Masone688b7252011-10-31 20:52:39 -04005976static int __btrfs_free_reserved_extent(struct btrfs_root *root,
5977 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04005978{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005979 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005980 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005981
Josef Bacik0f9dd462008-09-23 13:14:11 -04005982 cache = btrfs_lookup_block_group(root->fs_info, start);
5983 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05005984 printk(KERN_ERR "Unable to find block group for %llu\n",
5985 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005986 return -ENOSPC;
5987 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005988
Li Dongyang5378e602011-03-24 10:24:27 +00005989 if (btrfs_test_opt(root, DISCARD))
5990 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005991
Chris Masone688b7252011-10-31 20:52:39 -04005992 if (pin)
5993 pin_down_extent(root, cache, start, len, 1);
5994 else {
5995 btrfs_add_free_space(cache, start, len);
5996 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
5997 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005998 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04005999
liubo1abe9b82011-03-24 11:18:59 +00006000 trace_btrfs_reserved_extent_free(root, start, len);
6001
Chris Masone6dcd2d2008-07-17 12:53:50 -04006002 return ret;
6003}
6004
Chris Masone688b7252011-10-31 20:52:39 -04006005int btrfs_free_reserved_extent(struct btrfs_root *root,
6006 u64 start, u64 len)
6007{
6008 return __btrfs_free_reserved_extent(root, start, len, 0);
6009}
6010
6011int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6012 u64 start, u64 len)
6013{
6014 return __btrfs_free_reserved_extent(root, start, len, 1);
6015}
6016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006017static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6018 struct btrfs_root *root,
6019 u64 parent, u64 root_objectid,
6020 u64 flags, u64 owner, u64 offset,
6021 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006022{
6023 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006024 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006025 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006026 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006027 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006028 struct extent_buffer *leaf;
6029 int type;
6030 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006031
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006032 if (parent > 0)
6033 type = BTRFS_SHARED_DATA_REF_KEY;
6034 else
6035 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006037 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006038
6039 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006040 if (!path)
6041 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006042
Chris Masonb9473432009-03-13 11:00:37 -04006043 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006044 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6045 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006046 if (ret) {
6047 btrfs_free_path(path);
6048 return ret;
6049 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006050
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006051 leaf = path->nodes[0];
6052 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006053 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006054 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6055 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6056 btrfs_set_extent_flags(leaf, extent_item,
6057 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006058
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006059 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6060 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6061 if (parent > 0) {
6062 struct btrfs_shared_data_ref *ref;
6063 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6064 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6065 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6066 } else {
6067 struct btrfs_extent_data_ref *ref;
6068 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6069 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6070 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6071 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6072 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6073 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006074
6075 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006076 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006077
Yan, Zhengf0486c62010-05-16 10:46:25 -04006078 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006079 if (ret) { /* -ENOENT, logic error */
Chris Masond3977122009-01-05 21:25:51 -05006080 printk(KERN_ERR "btrfs update block group failed for %llu "
6081 "%llu\n", (unsigned long long)ins->objectid,
6082 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006083 BUG();
6084 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006085 return ret;
6086}
6087
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006088static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6089 struct btrfs_root *root,
6090 u64 parent, u64 root_objectid,
6091 u64 flags, struct btrfs_disk_key *key,
6092 int level, struct btrfs_key *ins)
6093{
6094 int ret;
6095 struct btrfs_fs_info *fs_info = root->fs_info;
6096 struct btrfs_extent_item *extent_item;
6097 struct btrfs_tree_block_info *block_info;
6098 struct btrfs_extent_inline_ref *iref;
6099 struct btrfs_path *path;
6100 struct extent_buffer *leaf;
6101 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
6102
6103 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006104 if (!path)
6105 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006106
6107 path->leave_spinning = 1;
6108 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6109 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006110 if (ret) {
6111 btrfs_free_path(path);
6112 return ret;
6113 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006114
6115 leaf = path->nodes[0];
6116 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6117 struct btrfs_extent_item);
6118 btrfs_set_extent_refs(leaf, extent_item, 1);
6119 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6120 btrfs_set_extent_flags(leaf, extent_item,
6121 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6122 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6123
6124 btrfs_set_tree_block_key(leaf, block_info, key);
6125 btrfs_set_tree_block_level(leaf, block_info, level);
6126
6127 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6128 if (parent > 0) {
6129 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6130 btrfs_set_extent_inline_ref_type(leaf, iref,
6131 BTRFS_SHARED_BLOCK_REF_KEY);
6132 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6133 } else {
6134 btrfs_set_extent_inline_ref_type(leaf, iref,
6135 BTRFS_TREE_BLOCK_REF_KEY);
6136 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6137 }
6138
6139 btrfs_mark_buffer_dirty(leaf);
6140 btrfs_free_path(path);
6141
Yan, Zhengf0486c62010-05-16 10:46:25 -04006142 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006143 if (ret) { /* -ENOENT, logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006144 printk(KERN_ERR "btrfs update block group failed for %llu "
6145 "%llu\n", (unsigned long long)ins->objectid,
6146 (unsigned long long)ins->offset);
6147 BUG();
6148 }
6149 return ret;
6150}
6151
6152int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6153 struct btrfs_root *root,
6154 u64 root_objectid, u64 owner,
6155 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006156{
6157 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006158
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006159 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006160
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006161 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6162 ins->offset, 0,
6163 root_objectid, owner, offset,
6164 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006165 return ret;
6166}
Chris Masone02119d2008-09-05 16:13:11 -04006167
6168/*
6169 * this is used by the tree logging recovery code. It records that
6170 * an extent has been allocated and makes sure to clear the free
6171 * space cache bits as well
6172 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006173int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6174 struct btrfs_root *root,
6175 u64 root_objectid, u64 owner, u64 offset,
6176 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006177{
6178 int ret;
6179 struct btrfs_block_group_cache *block_group;
Yan Zheng11833d62009-09-11 16:11:19 -04006180 struct btrfs_caching_control *caching_ctl;
6181 u64 start = ins->objectid;
6182 u64 num_bytes = ins->offset;
Chris Masone02119d2008-09-05 16:13:11 -04006183
Chris Masone02119d2008-09-05 16:13:11 -04006184 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikb8399de2010-12-08 09:15:11 -05006185 cache_block_group(block_group, trans, NULL, 0);
Yan Zheng11833d62009-09-11 16:11:19 -04006186 caching_ctl = get_caching_control(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006187
Yan Zheng11833d62009-09-11 16:11:19 -04006188 if (!caching_ctl) {
6189 BUG_ON(!block_group_cache_done(block_group));
6190 ret = btrfs_remove_free_space(block_group, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006191 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006192 } else {
6193 mutex_lock(&caching_ctl->mutex);
6194
6195 if (start >= caching_ctl->progress) {
6196 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006197 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006198 } else if (start + num_bytes <= caching_ctl->progress) {
6199 ret = btrfs_remove_free_space(block_group,
6200 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006201 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006202 } else {
6203 num_bytes = caching_ctl->progress - start;
6204 ret = btrfs_remove_free_space(block_group,
6205 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006206 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006207
6208 start = caching_ctl->progress;
6209 num_bytes = ins->objectid + ins->offset -
6210 caching_ctl->progress;
6211 ret = add_excluded_extent(root, start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006212 BUG_ON(ret); /* -ENOMEM */
Yan Zheng11833d62009-09-11 16:11:19 -04006213 }
6214
6215 mutex_unlock(&caching_ctl->mutex);
6216 put_caching_control(caching_ctl);
6217 }
6218
Josef Bacikfb25e912011-07-26 17:00:46 -04006219 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6220 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006221 BUG_ON(ret); /* logic error */
Chris Masonfa9c0d792009-04-03 09:47:43 -04006222 btrfs_put_block_group(block_group);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006223 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6224 0, owner, offset, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04006225 return ret;
6226}
6227
Chris Mason65b51a02008-08-01 15:11:20 -04006228struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6229 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05006230 u64 bytenr, u32 blocksize,
6231 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006232{
6233 struct extent_buffer *buf;
6234
6235 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6236 if (!buf)
6237 return ERR_PTR(-ENOMEM);
6238 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006239 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006240 btrfs_tree_lock(buf);
6241 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006242 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006243
6244 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006245 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006246
Chris Masond0c803c2008-09-11 16:17:57 -04006247 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006248 /*
6249 * we allow two log transactions at a time, use different
6250 * EXENT bit to differentiate dirty pages.
6251 */
6252 if (root->log_transid % 2 == 0)
6253 set_extent_dirty(&root->dirty_log_pages, buf->start,
6254 buf->start + buf->len - 1, GFP_NOFS);
6255 else
6256 set_extent_new(&root->dirty_log_pages, buf->start,
6257 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006258 } else {
6259 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6260 buf->start + buf->len - 1, GFP_NOFS);
6261 }
Chris Mason65b51a02008-08-01 15:11:20 -04006262 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006263 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006264 return buf;
6265}
6266
Yan, Zhengf0486c62010-05-16 10:46:25 -04006267static struct btrfs_block_rsv *
6268use_block_rsv(struct btrfs_trans_handle *trans,
6269 struct btrfs_root *root, u32 blocksize)
6270{
6271 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006272 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006273 int ret;
6274
6275 block_rsv = get_block_rsv(trans, root);
6276
6277 if (block_rsv->size == 0) {
Miao Xie08e007d2012-10-16 11:33:38 +00006278 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6279 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006280 /*
6281 * If we couldn't reserve metadata bytes try and use some from
6282 * the global reserve.
6283 */
6284 if (ret && block_rsv != global_rsv) {
6285 ret = block_rsv_use_bytes(global_rsv, blocksize);
6286 if (!ret)
6287 return global_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006288 return ERR_PTR(ret);
Josef Bacik68a82272011-01-24 21:43:20 +00006289 } else if (ret) {
6290 return ERR_PTR(ret);
6291 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006292 return block_rsv;
6293 }
6294
6295 ret = block_rsv_use_bytes(block_rsv, blocksize);
6296 if (!ret)
6297 return block_rsv;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006298 if (ret && !block_rsv->failfast) {
David Sterbadff51cd2011-06-14 12:52:17 +02006299 static DEFINE_RATELIMIT_STATE(_rs,
6300 DEFAULT_RATELIMIT_INTERVAL,
6301 /*DEFAULT_RATELIMIT_BURST*/ 2);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006302 if (__ratelimit(&_rs))
6303 WARN(1, KERN_DEBUG "btrfs: block rsv returned %d\n",
6304 ret);
Miao Xie08e007d2012-10-16 11:33:38 +00006305 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6306 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik68a82272011-01-24 21:43:20 +00006307 if (!ret) {
Josef Bacik68a82272011-01-24 21:43:20 +00006308 return block_rsv;
6309 } else if (ret && block_rsv != global_rsv) {
6310 ret = block_rsv_use_bytes(global_rsv, blocksize);
6311 if (!ret)
6312 return global_rsv;
6313 }
6314 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006315
Yan, Zhengf0486c62010-05-16 10:46:25 -04006316 return ERR_PTR(-ENOSPC);
6317}
6318
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006319static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6320 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006321{
6322 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006323 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006324}
6325
Chris Masonfec577f2007-02-26 10:40:21 -05006326/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006327 * finds a free extent and does all the dirty work required for allocation
6328 * returns the key for the extent through ins, and a tree buffer for
6329 * the first block of the extent through buf.
6330 *
Chris Masonfec577f2007-02-26 10:40:21 -05006331 * returns the tree buffer or NULL.
6332 */
Chris Mason5f39d392007-10-15 16:14:19 -04006333struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006334 struct btrfs_root *root, u32 blocksize,
6335 u64 parent, u64 root_objectid,
6336 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006337 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006338{
Chris Masone2fa7222007-03-12 16:22:34 -04006339 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006340 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006341 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006342 u64 flags = 0;
6343 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006344
Yan, Zhengf0486c62010-05-16 10:46:25 -04006345
6346 block_rsv = use_block_rsv(trans, root, blocksize);
6347 if (IS_ERR(block_rsv))
6348 return ERR_CAST(block_rsv);
6349
6350 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006351 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006352 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006353 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006354 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006355 }
Chris Mason55c69072008-01-09 15:55:33 -05006356
Chris Mason4008c042009-02-12 14:09:45 -05006357 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6358 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006359 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006360
6361 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6362 if (parent == 0)
6363 parent = ins.objectid;
6364 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6365 } else
6366 BUG_ON(parent > 0);
6367
6368 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6369 struct btrfs_delayed_extent_op *extent_op;
6370 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006371 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006372 if (key)
6373 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6374 else
6375 memset(&extent_op->key, 0, sizeof(extent_op->key));
6376 extent_op->flags_to_set = flags;
6377 extent_op->update_key = 1;
6378 extent_op->update_flags = 1;
6379 extent_op->is_data = 0;
6380
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006381 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6382 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006383 ins.offset, parent, root_objectid,
6384 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006385 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006386 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006387 }
Chris Masonfec577f2007-02-26 10:40:21 -05006388 return buf;
6389}
Chris Masona28ec192007-03-06 20:08:01 -05006390
Yan Zheng2c47e6052009-06-27 21:07:35 -04006391struct walk_control {
6392 u64 refs[BTRFS_MAX_LEVEL];
6393 u64 flags[BTRFS_MAX_LEVEL];
6394 struct btrfs_key update_progress;
6395 int stage;
6396 int level;
6397 int shared_level;
6398 int update_ref;
6399 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006400 int reada_slot;
6401 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006402 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006403};
6404
6405#define DROP_REFERENCE 1
6406#define UPDATE_BACKREF 2
6407
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006408static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6409 struct btrfs_root *root,
6410 struct walk_control *wc,
6411 struct btrfs_path *path)
6412{
6413 u64 bytenr;
6414 u64 generation;
6415 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006416 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006417 u32 nritems;
6418 u32 blocksize;
6419 struct btrfs_key key;
6420 struct extent_buffer *eb;
6421 int ret;
6422 int slot;
6423 int nread = 0;
6424
6425 if (path->slots[wc->level] < wc->reada_slot) {
6426 wc->reada_count = wc->reada_count * 2 / 3;
6427 wc->reada_count = max(wc->reada_count, 2);
6428 } else {
6429 wc->reada_count = wc->reada_count * 3 / 2;
6430 wc->reada_count = min_t(int, wc->reada_count,
6431 BTRFS_NODEPTRS_PER_BLOCK(root));
6432 }
6433
6434 eb = path->nodes[wc->level];
6435 nritems = btrfs_header_nritems(eb);
6436 blocksize = btrfs_level_size(root, wc->level - 1);
6437
6438 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6439 if (nread >= wc->reada_count)
6440 break;
6441
6442 cond_resched();
6443 bytenr = btrfs_node_blockptr(eb, slot);
6444 generation = btrfs_node_ptr_generation(eb, slot);
6445
6446 if (slot == path->slots[wc->level])
6447 goto reada;
6448
6449 if (wc->stage == UPDATE_BACKREF &&
6450 generation <= root->root_key.offset)
6451 continue;
6452
Yan, Zheng94fcca92009-10-09 09:25:16 -04006453 /* We don't lock the tree block, it's OK to be racy here */
6454 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6455 &refs, &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006456 /* We don't care about errors in readahead. */
6457 if (ret < 0)
6458 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006459 BUG_ON(refs == 0);
6460
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006461 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006462 if (refs == 1)
6463 goto reada;
6464
Yan, Zheng94fcca92009-10-09 09:25:16 -04006465 if (wc->level == 1 &&
6466 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6467 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006468 if (!wc->update_ref ||
6469 generation <= root->root_key.offset)
6470 continue;
6471 btrfs_node_key_to_cpu(eb, &key, slot);
6472 ret = btrfs_comp_cpu_keys(&key,
6473 &wc->update_progress);
6474 if (ret < 0)
6475 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006476 } else {
6477 if (wc->level == 1 &&
6478 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6479 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006480 }
6481reada:
6482 ret = readahead_tree_block(root, bytenr, blocksize,
6483 generation);
6484 if (ret)
6485 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006486 nread++;
6487 }
6488 wc->reada_slot = slot;
6489}
6490
Chris Mason9aca1d52007-03-13 11:09:37 -04006491/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006492 * hepler to process tree block while walking down the tree.
6493 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006494 * when wc->stage == UPDATE_BACKREF, this function updates
6495 * back refs for pointers in the block.
6496 *
6497 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006498 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006499static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6500 struct btrfs_root *root,
6501 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006502 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006503{
6504 int level = wc->level;
6505 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006506 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6507 int ret;
6508
6509 if (wc->stage == UPDATE_BACKREF &&
6510 btrfs_header_owner(eb) != root->root_key.objectid)
6511 return 1;
6512
6513 /*
6514 * when reference count of tree block is 1, it won't increase
6515 * again. once full backref flag is set, we never clear it.
6516 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006517 if (lookup_info &&
6518 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6519 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006520 BUG_ON(!path->locks[level]);
6521 ret = btrfs_lookup_extent_info(trans, root,
6522 eb->start, eb->len,
6523 &wc->refs[level],
6524 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006525 BUG_ON(ret == -ENOMEM);
6526 if (ret)
6527 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006528 BUG_ON(wc->refs[level] == 0);
6529 }
6530
Yan Zheng2c47e6052009-06-27 21:07:35 -04006531 if (wc->stage == DROP_REFERENCE) {
6532 if (wc->refs[level] > 1)
6533 return 1;
6534
6535 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006536 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006537 path->locks[level] = 0;
6538 }
6539 return 0;
6540 }
6541
6542 /* wc->stage == UPDATE_BACKREF */
6543 if (!(wc->flags[level] & flag)) {
6544 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006545 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006546 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006547 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006548 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006549 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6550 eb->len, flag, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006551 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006552 wc->flags[level] |= flag;
6553 }
6554
6555 /*
6556 * the block is shared by multiple trees, so it's not good to
6557 * keep the tree lock
6558 */
6559 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04006560 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006561 path->locks[level] = 0;
6562 }
6563 return 0;
6564}
6565
6566/*
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006567 * hepler to process tree block pointer.
6568 *
6569 * when wc->stage == DROP_REFERENCE, this function checks
6570 * reference count of the block pointed to. if the block
6571 * is shared and we need update back refs for the subtree
6572 * rooted at the block, this function changes wc->stage to
6573 * UPDATE_BACKREF. if the block is shared and there is no
6574 * need to update back, this function drops the reference
6575 * to the block.
6576 *
6577 * NOTE: return value 1 means we should stop walking down.
6578 */
6579static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6580 struct btrfs_root *root,
6581 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006582 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006583{
6584 u64 bytenr;
6585 u64 generation;
6586 u64 parent;
6587 u32 blocksize;
6588 struct btrfs_key key;
6589 struct extent_buffer *next;
6590 int level = wc->level;
6591 int reada = 0;
6592 int ret = 0;
6593
6594 generation = btrfs_node_ptr_generation(path->nodes[level],
6595 path->slots[level]);
6596 /*
6597 * if the lower level block was created before the snapshot
6598 * was created, we know there is no need to update back refs
6599 * for the subtree
6600 */
6601 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04006602 generation <= root->root_key.offset) {
6603 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006604 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006605 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006606
6607 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6608 blocksize = btrfs_level_size(root, level - 1);
6609
6610 next = btrfs_find_tree_block(root, bytenr, blocksize);
6611 if (!next) {
6612 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00006613 if (!next)
6614 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006615 reada = 1;
6616 }
6617 btrfs_tree_lock(next);
6618 btrfs_set_lock_blocking(next);
6619
Yan, Zheng94fcca92009-10-09 09:25:16 -04006620 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6621 &wc->refs[level - 1],
6622 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006623 if (ret < 0) {
6624 btrfs_tree_unlock(next);
6625 return ret;
6626 }
6627
Yan, Zheng94fcca92009-10-09 09:25:16 -04006628 BUG_ON(wc->refs[level - 1] == 0);
6629 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006630
Yan, Zheng94fcca92009-10-09 09:25:16 -04006631 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006632 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006633 if (level == 1 &&
6634 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6635 goto skip;
6636
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006637 if (!wc->update_ref ||
6638 generation <= root->root_key.offset)
6639 goto skip;
6640
6641 btrfs_node_key_to_cpu(path->nodes[level], &key,
6642 path->slots[level]);
6643 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6644 if (ret < 0)
6645 goto skip;
6646
6647 wc->stage = UPDATE_BACKREF;
6648 wc->shared_level = level - 1;
6649 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006650 } else {
6651 if (level == 1 &&
6652 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6653 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006654 }
6655
Chris Masonb9fab912012-05-06 07:23:47 -04006656 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006657 btrfs_tree_unlock(next);
6658 free_extent_buffer(next);
6659 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006660 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006661 }
6662
6663 if (!next) {
6664 if (reada && level == 1)
6665 reada_walk_down(trans, root, wc, path);
6666 next = read_tree_block(root, bytenr, blocksize, generation);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006667 if (!next)
6668 return -EIO;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006669 btrfs_tree_lock(next);
6670 btrfs_set_lock_blocking(next);
6671 }
6672
6673 level--;
6674 BUG_ON(level != btrfs_header_level(next));
6675 path->nodes[level] = next;
6676 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006677 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006678 wc->level = level;
6679 if (wc->level == 1)
6680 wc->reada_slot = 0;
6681 return 0;
6682skip:
6683 wc->refs[level - 1] = 0;
6684 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006685 if (wc->stage == DROP_REFERENCE) {
6686 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6687 parent = path->nodes[level]->start;
6688 } else {
6689 BUG_ON(root->root_key.objectid !=
6690 btrfs_header_owner(path->nodes[level]));
6691 parent = 0;
6692 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006693
Yan, Zheng94fcca92009-10-09 09:25:16 -04006694 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006695 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006696 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006697 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006698 btrfs_tree_unlock(next);
6699 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04006700 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006701 return 1;
6702}
6703
6704/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006705 * hepler to process tree block while walking up the tree.
6706 *
6707 * when wc->stage == DROP_REFERENCE, this function drops
6708 * reference count on the block.
6709 *
6710 * when wc->stage == UPDATE_BACKREF, this function changes
6711 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6712 * to UPDATE_BACKREF previously while processing the block.
6713 *
6714 * NOTE: return value 1 means we should stop walking up.
6715 */
6716static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6717 struct btrfs_root *root,
6718 struct btrfs_path *path,
6719 struct walk_control *wc)
6720{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006721 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006722 int level = wc->level;
6723 struct extent_buffer *eb = path->nodes[level];
6724 u64 parent = 0;
6725
6726 if (wc->stage == UPDATE_BACKREF) {
6727 BUG_ON(wc->shared_level < level);
6728 if (level < wc->shared_level)
6729 goto out;
6730
Yan Zheng2c47e6052009-06-27 21:07:35 -04006731 ret = find_next_key(path, level + 1, &wc->update_progress);
6732 if (ret > 0)
6733 wc->update_ref = 0;
6734
6735 wc->stage = DROP_REFERENCE;
6736 wc->shared_level = -1;
6737 path->slots[level] = 0;
6738
6739 /*
6740 * check reference count again if the block isn't locked.
6741 * we should start walking down the tree again if reference
6742 * count is one.
6743 */
6744 if (!path->locks[level]) {
6745 BUG_ON(level == 0);
6746 btrfs_tree_lock(eb);
6747 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006748 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006749
6750 ret = btrfs_lookup_extent_info(trans, root,
6751 eb->start, eb->len,
6752 &wc->refs[level],
6753 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006754 if (ret < 0) {
6755 btrfs_tree_unlock_rw(eb, path->locks[level]);
6756 return ret;
6757 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006758 BUG_ON(wc->refs[level] == 0);
6759 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006760 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006761 return 1;
6762 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006763 }
6764 }
6765
6766 /* wc->stage == DROP_REFERENCE */
6767 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6768
6769 if (wc->refs[level] == 1) {
6770 if (level == 0) {
6771 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006772 ret = btrfs_dec_ref(trans, root, eb, 1,
6773 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006774 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006775 ret = btrfs_dec_ref(trans, root, eb, 0,
6776 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006777 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006778 }
6779 /* make block locked assertion in clean_tree_block happy */
6780 if (!path->locks[level] &&
6781 btrfs_header_generation(eb) == trans->transid) {
6782 btrfs_tree_lock(eb);
6783 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006784 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006785 }
6786 clean_tree_block(trans, root, eb);
6787 }
6788
6789 if (eb == root->node) {
6790 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6791 parent = eb->start;
6792 else
6793 BUG_ON(root->root_key.objectid !=
6794 btrfs_header_owner(eb));
6795 } else {
6796 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6797 parent = path->nodes[level + 1]->start;
6798 else
6799 BUG_ON(root->root_key.objectid !=
6800 btrfs_header_owner(path->nodes[level + 1]));
6801 }
6802
Jan Schmidt5581a512012-05-16 17:04:52 +02006803 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006804out:
6805 wc->refs[level] = 0;
6806 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006807 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006808}
6809
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006810static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6811 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006812 struct btrfs_path *path,
6813 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006814{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006815 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006816 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006817 int ret;
6818
Yan Zheng2c47e6052009-06-27 21:07:35 -04006819 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006820 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006821 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006822 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006823
Yan Zheng2c47e6052009-06-27 21:07:35 -04006824 if (level == 0)
6825 break;
6826
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006827 if (path->slots[level] >=
6828 btrfs_header_nritems(path->nodes[level]))
6829 break;
6830
Yan, Zheng94fcca92009-10-09 09:25:16 -04006831 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006832 if (ret > 0) {
6833 path->slots[level]++;
6834 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006835 } else if (ret < 0)
6836 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006837 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006838 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006839 return 0;
6840}
6841
Chris Masond3977122009-01-05 21:25:51 -05006842static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006843 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006844 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006845 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006846{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006847 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006848 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006849
Yan Zheng2c47e6052009-06-27 21:07:35 -04006850 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6851 while (level < max_level && path->nodes[level]) {
6852 wc->level = level;
6853 if (path->slots[level] + 1 <
6854 btrfs_header_nritems(path->nodes[level])) {
6855 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05006856 return 0;
6857 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006858 ret = walk_up_proc(trans, root, path, wc);
6859 if (ret > 0)
6860 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05006861
Yan Zheng2c47e6052009-06-27 21:07:35 -04006862 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006863 btrfs_tree_unlock_rw(path->nodes[level],
6864 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006865 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006866 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006867 free_extent_buffer(path->nodes[level]);
6868 path->nodes[level] = NULL;
6869 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006870 }
6871 }
6872 return 1;
6873}
6874
Chris Mason9aca1d52007-03-13 11:09:37 -04006875/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006876 * drop a subvolume tree.
6877 *
6878 * this function traverses the tree freeing any blocks that only
6879 * referenced by the tree.
6880 *
6881 * when a shared tree block is found. this function decreases its
6882 * reference count by one. if update_ref is true, this function
6883 * also make sure backrefs for the shared block and all lower level
6884 * blocks are properly updated.
Chris Mason9aca1d52007-03-13 11:09:37 -04006885 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04006886int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006887 struct btrfs_block_rsv *block_rsv, int update_ref,
6888 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05006889{
Chris Mason5caf2a02007-04-02 11:20:42 -04006890 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006891 struct btrfs_trans_handle *trans;
6892 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006893 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006894 struct walk_control *wc;
6895 struct btrfs_key key;
6896 int err = 0;
6897 int ret;
6898 int level;
Chris Mason20524f02007-03-10 06:35:47 -05006899
Chris Mason5caf2a02007-04-02 11:20:42 -04006900 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006901 if (!path) {
6902 err = -ENOMEM;
6903 goto out;
6904 }
Chris Mason20524f02007-03-10 06:35:47 -05006905
Yan Zheng2c47e6052009-06-27 21:07:35 -04006906 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07006907 if (!wc) {
6908 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006909 err = -ENOMEM;
6910 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07006911 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006912
Yan, Zhenga22285a2010-05-16 10:48:46 -04006913 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006914 if (IS_ERR(trans)) {
6915 err = PTR_ERR(trans);
6916 goto out_free;
6917 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006918
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006919 if (block_rsv)
6920 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006921
Chris Mason9f3a7422007-08-07 15:52:19 -04006922 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006923 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006924 path->nodes[level] = btrfs_lock_root_node(root);
6925 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006926 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006927 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006928 memset(&wc->update_progress, 0,
6929 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006930 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006931 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006932 memcpy(&wc->update_progress, &key,
6933 sizeof(wc->update_progress));
6934
Chris Mason6702ed42007-08-07 16:15:09 -04006935 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006936 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006937 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006938 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6939 path->lowest_level = 0;
6940 if (ret < 0) {
6941 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006942 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04006943 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006944 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006945
Chris Mason7d9eb122008-07-08 14:19:17 -04006946 /*
6947 * unlock our path, this is safe because only this
6948 * function is allowed to delete this snapshot
6949 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006950 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006951
Yan Zheng2c47e6052009-06-27 21:07:35 -04006952 level = btrfs_header_level(root->node);
6953 while (1) {
6954 btrfs_tree_lock(path->nodes[level]);
6955 btrfs_set_lock_blocking(path->nodes[level]);
6956
6957 ret = btrfs_lookup_extent_info(trans, root,
6958 path->nodes[level]->start,
6959 path->nodes[level]->len,
6960 &wc->refs[level],
6961 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006962 if (ret < 0) {
6963 err = ret;
6964 goto out_end_trans;
6965 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006966 BUG_ON(wc->refs[level] == 0);
6967
6968 if (level == root_item->drop_level)
6969 break;
6970
6971 btrfs_tree_unlock(path->nodes[level]);
6972 WARN_ON(wc->refs[level] != 1);
6973 level--;
6974 }
6975 }
6976
6977 wc->level = level;
6978 wc->shared_level = -1;
6979 wc->stage = DROP_REFERENCE;
6980 wc->update_ref = update_ref;
6981 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006982 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006983 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006984
6985 while (1) {
6986 ret = walk_down_tree(trans, root, path, wc);
6987 if (ret < 0) {
6988 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006989 break;
6990 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006991
6992 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6993 if (ret < 0) {
6994 err = ret;
6995 break;
6996 }
6997
6998 if (ret > 0) {
6999 BUG_ON(wc->stage != DROP_REFERENCE);
7000 break;
7001 }
7002
7003 if (wc->stage == DROP_REFERENCE) {
7004 level = wc->level;
7005 btrfs_node_key(path->nodes[level],
7006 &root_item->drop_progress,
7007 path->slots[level]);
7008 root_item->drop_level = level;
7009 }
7010
7011 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007012 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007013 ret = btrfs_update_root(trans, tree_root,
7014 &root->root_key,
7015 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007016 if (ret) {
7017 btrfs_abort_transaction(trans, tree_root, ret);
7018 err = ret;
7019 goto out_end_trans;
7020 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007021
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007022 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007023 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007024 if (IS_ERR(trans)) {
7025 err = PTR_ERR(trans);
7026 goto out_free;
7027 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007028 if (block_rsv)
7029 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007030 }
Chris Mason20524f02007-03-10 06:35:47 -05007031 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007032 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007033 if (err)
7034 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007035
7036 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007037 if (ret) {
7038 btrfs_abort_transaction(trans, tree_root, ret);
7039 goto out_end_trans;
7040 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007041
Yan, Zheng76dda932009-09-21 16:00:26 -04007042 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7043 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7044 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007045 if (ret < 0) {
7046 btrfs_abort_transaction(trans, tree_root, ret);
7047 err = ret;
7048 goto out_end_trans;
7049 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007050 /* if we fail to delete the orphan item this time
7051 * around, it'll get picked up the next time.
7052 *
7053 * The most common failure here is just -ENOENT.
7054 */
7055 btrfs_del_orphan_item(trans, tree_root,
7056 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007057 }
7058 }
7059
7060 if (root->in_radix) {
7061 btrfs_free_fs_root(tree_root->fs_info, root);
7062 } else {
7063 free_extent_buffer(root->node);
7064 free_extent_buffer(root->commit_root);
7065 kfree(root);
7066 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007067out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007068 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007069out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007070 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007071 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007072out:
7073 if (err)
7074 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007075 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007076}
Chris Mason9078a3e2007-04-26 16:46:15 -04007077
Yan Zheng2c47e6052009-06-27 21:07:35 -04007078/*
7079 * drop subtree rooted at tree block 'node'.
7080 *
7081 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007082 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007083 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007084int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7085 struct btrfs_root *root,
7086 struct extent_buffer *node,
7087 struct extent_buffer *parent)
7088{
7089 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007090 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007091 int level;
7092 int parent_level;
7093 int ret = 0;
7094 int wret;
7095
Yan Zheng2c47e6052009-06-27 21:07:35 -04007096 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7097
Yan Zhengf82d02d2008-10-29 14:49:05 -04007098 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007099 if (!path)
7100 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007101
Yan Zheng2c47e6052009-06-27 21:07:35 -04007102 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007103 if (!wc) {
7104 btrfs_free_path(path);
7105 return -ENOMEM;
7106 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007107
Chris Masonb9447ef2009-03-09 11:45:38 -04007108 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007109 parent_level = btrfs_header_level(parent);
7110 extent_buffer_get(parent);
7111 path->nodes[parent_level] = parent;
7112 path->slots[parent_level] = btrfs_header_nritems(parent);
7113
Chris Masonb9447ef2009-03-09 11:45:38 -04007114 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007115 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007116 path->nodes[level] = node;
7117 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007118 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007119
7120 wc->refs[parent_level] = 1;
7121 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7122 wc->level = level;
7123 wc->shared_level = -1;
7124 wc->stage = DROP_REFERENCE;
7125 wc->update_ref = 0;
7126 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007127 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007128 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007129
7130 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007131 wret = walk_down_tree(trans, root, path, wc);
7132 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007133 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007134 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007135 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007136
Yan Zheng2c47e6052009-06-27 21:07:35 -04007137 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007138 if (wret < 0)
7139 ret = wret;
7140 if (wret != 0)
7141 break;
7142 }
7143
Yan Zheng2c47e6052009-06-27 21:07:35 -04007144 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007145 btrfs_free_path(path);
7146 return ret;
7147}
7148
Chris Masonec44a352008-04-28 15:29:52 -04007149static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7150{
7151 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007152 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007153
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007154 /*
7155 * if restripe for this chunk_type is on pick target profile and
7156 * return, otherwise do the usual balance
7157 */
7158 stripped = get_restripe_target(root->fs_info, flags);
7159 if (stripped)
7160 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007161
Chris Masoncd02dca2010-12-13 14:56:23 -05007162 /*
7163 * we add in the count of missing devices because we want
7164 * to make sure that any RAID levels on a degraded FS
7165 * continue to be honored.
7166 */
7167 num_devices = root->fs_info->fs_devices->rw_devices +
7168 root->fs_info->fs_devices->missing_devices;
7169
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007170 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7171 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7172
Chris Masonec44a352008-04-28 15:29:52 -04007173 if (num_devices == 1) {
7174 stripped |= BTRFS_BLOCK_GROUP_DUP;
7175 stripped = flags & ~stripped;
7176
7177 /* turn raid0 into single device chunks */
7178 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7179 return stripped;
7180
7181 /* turn mirroring into duplication */
7182 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7183 BTRFS_BLOCK_GROUP_RAID10))
7184 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007185 } else {
7186 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007187 if (flags & stripped)
7188 return flags;
7189
7190 stripped |= BTRFS_BLOCK_GROUP_DUP;
7191 stripped = flags & ~stripped;
7192
7193 /* switch duplicated blocks with raid1 */
7194 if (flags & BTRFS_BLOCK_GROUP_DUP)
7195 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7196
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007197 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007198 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007199
Chris Masonec44a352008-04-28 15:29:52 -04007200 return flags;
7201}
7202
Miao Xie199c36e2011-07-15 10:34:36 +00007203static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007204{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007205 struct btrfs_space_info *sinfo = cache->space_info;
7206 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007207 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007208 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007209
Chris Masonc286ac42008-07-22 23:06:41 -04007210
Miao Xie199c36e2011-07-15 10:34:36 +00007211 /*
7212 * We need some metadata space and system metadata space for
7213 * allocating chunks in some corner cases until we force to set
7214 * it to be readonly.
7215 */
7216 if ((sinfo->flags &
7217 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7218 !force)
7219 min_allocable_bytes = 1 * 1024 * 1024;
7220 else
7221 min_allocable_bytes = 0;
7222
Yan, Zhengf0486c62010-05-16 10:46:25 -04007223 spin_lock(&sinfo->lock);
7224 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007225
7226 if (cache->ro) {
7227 ret = 0;
7228 goto out;
7229 }
7230
Yan, Zhengf0486c62010-05-16 10:46:25 -04007231 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7232 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007233
Yan, Zhengf0486c62010-05-16 10:46:25 -04007234 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007235 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7236 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007237 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007238 cache->ro = 1;
7239 ret = 0;
7240 }
WuBo61cfea92011-07-26 03:30:11 +00007241out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007242 spin_unlock(&cache->lock);
7243 spin_unlock(&sinfo->lock);
7244 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007245}
7246
Yan, Zhengf0486c62010-05-16 10:46:25 -04007247int btrfs_set_block_group_ro(struct btrfs_root *root,
7248 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007249
7250{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007251 struct btrfs_trans_handle *trans;
7252 u64 alloc_flags;
7253 int ret;
7254
7255 BUG_ON(cache->ro);
7256
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007257 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007258 if (IS_ERR(trans))
7259 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007260
7261 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007262 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007263 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007264 CHUNK_ALLOC_FORCE);
7265 if (ret < 0)
7266 goto out;
7267 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007268
Miao Xie199c36e2011-07-15 10:34:36 +00007269 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007270 if (!ret)
7271 goto out;
7272 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007273 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007274 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007275 if (ret < 0)
7276 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007277 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007278out:
7279 btrfs_end_transaction(trans, root);
7280 return ret;
7281}
7282
Chris Masonc87f08c2011-02-16 13:57:04 -05007283int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7284 struct btrfs_root *root, u64 type)
7285{
7286 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007287 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007288 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007289}
7290
Miao Xie6d07bce2011-01-05 10:07:31 +00007291/*
7292 * helper to account the unused space of all the readonly block group in the
7293 * list. takes mirrors into account.
7294 */
7295static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7296{
7297 struct btrfs_block_group_cache *block_group;
7298 u64 free_bytes = 0;
7299 int factor;
7300
7301 list_for_each_entry(block_group, groups_list, list) {
7302 spin_lock(&block_group->lock);
7303
7304 if (!block_group->ro) {
7305 spin_unlock(&block_group->lock);
7306 continue;
7307 }
7308
7309 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7310 BTRFS_BLOCK_GROUP_RAID10 |
7311 BTRFS_BLOCK_GROUP_DUP))
7312 factor = 2;
7313 else
7314 factor = 1;
7315
7316 free_bytes += (block_group->key.offset -
7317 btrfs_block_group_used(&block_group->item)) *
7318 factor;
7319
7320 spin_unlock(&block_group->lock);
7321 }
7322
7323 return free_bytes;
7324}
7325
7326/*
7327 * helper to account the unused space of all the readonly block group in the
7328 * space_info. takes mirrors into account.
7329 */
7330u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7331{
7332 int i;
7333 u64 free_bytes = 0;
7334
7335 spin_lock(&sinfo->lock);
7336
7337 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7338 if (!list_empty(&sinfo->block_groups[i]))
7339 free_bytes += __btrfs_get_ro_block_group_free_space(
7340 &sinfo->block_groups[i]);
7341
7342 spin_unlock(&sinfo->lock);
7343
7344 return free_bytes;
7345}
7346
Jeff Mahoney143bede2012-03-01 14:56:26 +01007347void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007348 struct btrfs_block_group_cache *cache)
7349{
7350 struct btrfs_space_info *sinfo = cache->space_info;
7351 u64 num_bytes;
7352
7353 BUG_ON(!cache->ro);
7354
7355 spin_lock(&sinfo->lock);
7356 spin_lock(&cache->lock);
7357 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7358 cache->bytes_super - btrfs_block_group_used(&cache->item);
7359 sinfo->bytes_readonly -= num_bytes;
7360 cache->ro = 0;
7361 spin_unlock(&cache->lock);
7362 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007363}
7364
Josef Bacikba1bf482009-09-11 16:11:19 -04007365/*
7366 * checks to see if its even possible to relocate this block group.
7367 *
7368 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7369 * ok to go ahead and try.
7370 */
7371int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007372{
Zheng Yan1a40e232008-09-26 10:09:34 -04007373 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007374 struct btrfs_space_info *space_info;
7375 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7376 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007377 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007378 u64 dev_min = 1;
7379 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007380 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007381 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007382 int full = 0;
7383 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007384
Josef Bacikba1bf482009-09-11 16:11:19 -04007385 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007386
Josef Bacikba1bf482009-09-11 16:11:19 -04007387 /* odd, couldn't find the block group, leave it alone */
7388 if (!block_group)
7389 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007390
liubocdcb7252011-08-03 10:15:25 +00007391 min_free = btrfs_block_group_used(&block_group->item);
7392
Josef Bacikba1bf482009-09-11 16:11:19 -04007393 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007394 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007395 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007396
Josef Bacikba1bf482009-09-11 16:11:19 -04007397 space_info = block_group->space_info;
7398 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007399
Josef Bacikba1bf482009-09-11 16:11:19 -04007400 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007401
Josef Bacikba1bf482009-09-11 16:11:19 -04007402 /*
7403 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007404 * relocate it unless we're able to allocate a new chunk below.
7405 *
7406 * Otherwise, we need to make sure we have room in the space to handle
7407 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007408 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007409 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007410 (space_info->bytes_used + space_info->bytes_reserved +
7411 space_info->bytes_pinned + space_info->bytes_readonly +
7412 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007413 spin_unlock(&space_info->lock);
7414 goto out;
7415 }
7416 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007417
Josef Bacikba1bf482009-09-11 16:11:19 -04007418 /*
7419 * ok we don't have enough space, but maybe we have free space on our
7420 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007421 * alloc devices and guess if we have enough space. if this block
7422 * group is going to be restriped, run checks against the target
7423 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007424 */
7425 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007426
liubocdcb7252011-08-03 10:15:25 +00007427 /*
7428 * index:
7429 * 0: raid10
7430 * 1: raid1
7431 * 2: dup
7432 * 3: raid0
7433 * 4: single
7434 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007435 target = get_restripe_target(root->fs_info, block_group->flags);
7436 if (target) {
7437 index = __get_block_group_index(extended_to_chunk(target));
7438 } else {
7439 /*
7440 * this is just a balance, so if we were marked as full
7441 * we know there is no space for a new chunk
7442 */
7443 if (full)
7444 goto out;
7445
7446 index = get_block_group_index(block_group);
7447 }
7448
liubocdcb7252011-08-03 10:15:25 +00007449 if (index == 0) {
7450 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007451 /* Divide by 2 */
7452 min_free >>= 1;
liubocdcb7252011-08-03 10:15:25 +00007453 } else if (index == 1) {
7454 dev_min = 2;
7455 } else if (index == 2) {
Josef Bacik6719db62011-08-20 08:29:51 -04007456 /* Multiply by 2 */
7457 min_free <<= 1;
liubocdcb7252011-08-03 10:15:25 +00007458 } else if (index == 3) {
7459 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007460 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007461 }
7462
Josef Bacikba1bf482009-09-11 16:11:19 -04007463 mutex_lock(&root->fs_info->chunk_mutex);
7464 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007465 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007466
Josef Bacikba1bf482009-09-11 16:11:19 -04007467 /*
7468 * check to make sure we can actually find a chunk with enough
7469 * space to fit our block group in.
7470 */
7471 if (device->total_bytes > device->bytes_used + min_free) {
Li Zefan125ccb02011-12-08 15:07:24 +08007472 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007473 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007474 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007475 dev_nr++;
7476
7477 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007478 break;
liubocdcb7252011-08-03 10:15:25 +00007479
Josef Bacikba1bf482009-09-11 16:11:19 -04007480 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007481 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007482 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007483 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007484out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007485 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007486 return ret;
7487}
7488
Christoph Hellwigb2950862008-12-02 09:54:17 -05007489static int find_first_block_group(struct btrfs_root *root,
7490 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007491{
Chris Mason925baed2008-06-25 16:01:30 -04007492 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007493 struct btrfs_key found_key;
7494 struct extent_buffer *leaf;
7495 int slot;
7496
7497 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7498 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007499 goto out;
7500
Chris Masond3977122009-01-05 21:25:51 -05007501 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007502 slot = path->slots[0];
7503 leaf = path->nodes[0];
7504 if (slot >= btrfs_header_nritems(leaf)) {
7505 ret = btrfs_next_leaf(root, path);
7506 if (ret == 0)
7507 continue;
7508 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007509 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007510 break;
7511 }
7512 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7513
7514 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007515 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7516 ret = 0;
7517 goto out;
7518 }
Chris Mason0b86a832008-03-24 15:01:56 -04007519 path->slots[0]++;
7520 }
Chris Mason925baed2008-06-25 16:01:30 -04007521out:
Chris Mason0b86a832008-03-24 15:01:56 -04007522 return ret;
7523}
7524
Josef Bacik0af3d002010-06-21 14:48:16 -04007525void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
7526{
7527 struct btrfs_block_group_cache *block_group;
7528 u64 last = 0;
7529
7530 while (1) {
7531 struct inode *inode;
7532
7533 block_group = btrfs_lookup_first_block_group(info, last);
7534 while (block_group) {
7535 spin_lock(&block_group->lock);
7536 if (block_group->iref)
7537 break;
7538 spin_unlock(&block_group->lock);
7539 block_group = next_block_group(info->tree_root,
7540 block_group);
7541 }
7542 if (!block_group) {
7543 if (last == 0)
7544 break;
7545 last = 0;
7546 continue;
7547 }
7548
7549 inode = block_group->inode;
7550 block_group->iref = 0;
7551 block_group->inode = NULL;
7552 spin_unlock(&block_group->lock);
7553 iput(inode);
7554 last = block_group->key.objectid + block_group->key.offset;
7555 btrfs_put_block_group(block_group);
7556 }
7557}
7558
Zheng Yan1a40e232008-09-26 10:09:34 -04007559int btrfs_free_block_groups(struct btrfs_fs_info *info)
7560{
7561 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007562 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007563 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007564 struct rb_node *n;
7565
Yan Zheng11833d62009-09-11 16:11:19 -04007566 down_write(&info->extent_commit_sem);
7567 while (!list_empty(&info->caching_block_groups)) {
7568 caching_ctl = list_entry(info->caching_block_groups.next,
7569 struct btrfs_caching_control, list);
7570 list_del(&caching_ctl->list);
7571 put_caching_control(caching_ctl);
7572 }
7573 up_write(&info->extent_commit_sem);
7574
Zheng Yan1a40e232008-09-26 10:09:34 -04007575 spin_lock(&info->block_group_cache_lock);
7576 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7577 block_group = rb_entry(n, struct btrfs_block_group_cache,
7578 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04007579 rb_erase(&block_group->cache_node,
7580 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04007581 spin_unlock(&info->block_group_cache_lock);
7582
Josef Bacik80eb2342008-10-29 14:49:05 -04007583 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007584 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007585 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007586
Josef Bacik817d52f2009-07-13 21:29:25 -04007587 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04007588 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04007589
Josef Bacik3c148742011-02-02 15:53:47 +00007590 /*
7591 * We haven't cached this block group, which means we could
7592 * possibly have excluded extents on this block group.
7593 */
7594 if (block_group->cached == BTRFS_CACHE_NO)
7595 free_excluded_extents(info->extent_root, block_group);
7596
Josef Bacik817d52f2009-07-13 21:29:25 -04007597 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00007598 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007599
7600 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007601 }
7602 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007603
7604 /* now that all the block groups are freed, go through and
7605 * free all the space_info structs. This is only called during
7606 * the final stages of unmount, and so we know nobody is
7607 * using them. We call synchronize_rcu() once before we start,
7608 * just to be on the safe side.
7609 */
7610 synchronize_rcu();
7611
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007612 release_global_block_rsv(info);
7613
Chris Mason4184ea72009-03-10 12:39:20 -04007614 while(!list_empty(&info->space_info)) {
7615 space_info = list_entry(info->space_info.next,
7616 struct btrfs_space_info,
7617 list);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007618 if (space_info->bytes_pinned > 0 ||
Josef Bacikfb25e912011-07-26 17:00:46 -04007619 space_info->bytes_reserved > 0 ||
7620 space_info->bytes_may_use > 0) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007621 WARN_ON(1);
7622 dump_space_info(space_info, 0, 0);
7623 }
Chris Mason4184ea72009-03-10 12:39:20 -04007624 list_del(&space_info->list);
7625 kfree(space_info);
7626 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007627 return 0;
7628}
7629
Yan, Zhengb742bb82010-05-16 10:46:24 -04007630static void __link_block_group(struct btrfs_space_info *space_info,
7631 struct btrfs_block_group_cache *cache)
7632{
7633 int index = get_block_group_index(cache);
7634
7635 down_write(&space_info->groups_sem);
7636 list_add_tail(&cache->list, &space_info->block_groups[index]);
7637 up_write(&space_info->groups_sem);
7638}
7639
Chris Mason9078a3e2007-04-26 16:46:15 -04007640int btrfs_read_block_groups(struct btrfs_root *root)
7641{
7642 struct btrfs_path *path;
7643 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007644 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04007645 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007646 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007647 struct btrfs_key key;
7648 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007649 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007650 int need_clear = 0;
7651 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04007652
Chris Masonbe744172007-05-06 10:15:01 -04007653 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04007654 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007655 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04007656 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04007657 path = btrfs_alloc_path();
7658 if (!path)
7659 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04007660 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04007661
David Sterba6c417612011-04-13 15:41:04 +02007662 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04007663 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02007664 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04007665 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04007666 if (btrfs_test_opt(root, CLEAR_CACHE))
7667 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007668
Chris Masond3977122009-01-05 21:25:51 -05007669 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007670 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007671 if (ret > 0)
7672 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007673 if (ret != 0)
7674 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04007675 leaf = path->nodes[0];
7676 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04007677 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04007678 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007679 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007680 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007681 }
Li Zefan34d52cb2011-03-29 13:46:06 +08007682 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7683 GFP_NOFS);
7684 if (!cache->free_space_ctl) {
7685 kfree(cache);
7686 ret = -ENOMEM;
7687 goto error;
7688 }
Chris Mason3e1ad542007-05-07 20:03:49 -04007689
Yan Zhengd2fb3432008-12-11 16:30:39 -05007690 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007691 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04007692 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007693 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007694 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04007695
Liu Bocf7c1ef2012-07-06 03:31:34 -06007696 if (need_clear) {
7697 /*
7698 * When we mount with old space cache, we need to
7699 * set BTRFS_DC_CLEAR and set dirty flag.
7700 *
7701 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7702 * truncate the old free space cache inode and
7703 * setup a new one.
7704 * b) Setting 'dirty flag' makes sure that we flush
7705 * the new space cache info onto disk.
7706 */
Josef Bacik0af3d002010-06-21 14:48:16 -04007707 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007708 if (btrfs_test_opt(root, SPACE_CACHE))
7709 cache->dirty = 1;
7710 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007711
Chris Mason5f39d392007-10-15 16:14:19 -04007712 read_extent_buffer(leaf, &cache->item,
7713 btrfs_item_ptr_offset(leaf, path->slots[0]),
7714 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04007715 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04007716
Chris Mason9078a3e2007-04-26 16:46:15 -04007717 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007718 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007719 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04007720 cache->sectorsize = root->sectorsize;
7721
Li Zefan34d52cb2011-03-29 13:46:06 +08007722 btrfs_init_free_space_ctl(cache);
7723
Josef Bacik817d52f2009-07-13 21:29:25 -04007724 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007725 * We need to exclude the super stripes now so that the space
7726 * info has super bytes accounted for, otherwise we'll think
7727 * we have more space than we actually do.
7728 */
7729 exclude_super_stripes(root, cache);
7730
7731 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007732 * check for two cases, either we are full, and therefore
7733 * don't need to bother with the caching work since we won't
7734 * find any space, or we are empty, and we can just add all
7735 * the space in and be done with it. This saves us _alot_ of
7736 * time, particularly in the full case.
7737 */
7738 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007739 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007740 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04007741 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007742 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007743 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007744 cache->cached = BTRFS_CACHE_FINISHED;
7745 add_new_free_space(cache, root->fs_info,
7746 found_key.objectid,
7747 found_key.objectid +
7748 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04007749 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007750 }
Chris Mason96b51792007-10-15 16:15:19 -04007751
Chris Mason6324fbf2008-03-24 15:01:59 -04007752 ret = update_space_info(info, cache->flags, found_key.offset,
7753 btrfs_block_group_used(&cache->item),
7754 &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007755 BUG_ON(ret); /* -ENOMEM */
Chris Mason6324fbf2008-03-24 15:01:59 -04007756 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007757 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007758 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007759 spin_unlock(&cache->space_info->lock);
7760
Yan, Zhengb742bb82010-05-16 10:46:24 -04007761 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007762
Josef Bacik0f9dd462008-09-23 13:14:11 -04007763 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007764 BUG_ON(ret); /* Logic error */
Chris Mason75ccf472008-09-30 19:24:06 -04007765
7766 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05007767 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00007768 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04007769 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007770
7771 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7772 if (!(get_alloc_profile(root, space_info->flags) &
7773 (BTRFS_BLOCK_GROUP_RAID10 |
7774 BTRFS_BLOCK_GROUP_RAID1 |
7775 BTRFS_BLOCK_GROUP_DUP)))
7776 continue;
7777 /*
7778 * avoid allocating from un-mirrored block group if there are
7779 * mirrored block groups.
7780 */
7781 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007782 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007783 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00007784 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007785 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007786
7787 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007788 ret = 0;
7789error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007790 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007791 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007792}
Chris Mason6324fbf2008-03-24 15:01:59 -04007793
Josef Bacikea658ba2012-09-11 16:57:25 -04007794void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
7795 struct btrfs_root *root)
7796{
7797 struct btrfs_block_group_cache *block_group, *tmp;
7798 struct btrfs_root *extent_root = root->fs_info->extent_root;
7799 struct btrfs_block_group_item item;
7800 struct btrfs_key key;
7801 int ret = 0;
7802
7803 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
7804 new_bg_list) {
7805 list_del_init(&block_group->new_bg_list);
7806
7807 if (ret)
7808 continue;
7809
7810 spin_lock(&block_group->lock);
7811 memcpy(&item, &block_group->item, sizeof(item));
7812 memcpy(&key, &block_group->key, sizeof(key));
7813 spin_unlock(&block_group->lock);
7814
7815 ret = btrfs_insert_item(trans, extent_root, &key, &item,
7816 sizeof(item));
7817 if (ret)
7818 btrfs_abort_transaction(trans, extent_root, ret);
7819 }
7820}
7821
Chris Mason6324fbf2008-03-24 15:01:59 -04007822int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7823 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04007824 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04007825 u64 size)
7826{
7827 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007828 struct btrfs_root *extent_root;
7829 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007830
7831 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04007832
Chris Mason12fcfd22009-03-24 10:24:20 -04007833 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04007834
Chris Mason8f18cf12008-04-25 16:53:30 -04007835 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007836 if (!cache)
7837 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08007838 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7839 GFP_NOFS);
7840 if (!cache->free_space_ctl) {
7841 kfree(cache);
7842 return -ENOMEM;
7843 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007844
Chris Masone17cade2008-04-15 15:41:47 -04007845 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04007846 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05007847 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04007848 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04007849 cache->fs_info = root->fs_info;
Josef Bacik96303082009-07-13 21:29:25 -04007850
Yan Zhengd2fb3432008-12-11 16:30:39 -05007851 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04007852 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007853 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007854 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04007855 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04007856
Li Zefan34d52cb2011-03-29 13:46:06 +08007857 btrfs_init_free_space_ctl(cache);
7858
Chris Mason6324fbf2008-03-24 15:01:59 -04007859 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04007860 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7861 cache->flags = type;
7862 btrfs_set_block_group_flags(&cache->item, type);
7863
Yan Zheng11833d62009-09-11 16:11:19 -04007864 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007865 cache->cached = BTRFS_CACHE_FINISHED;
Yan Zheng11833d62009-09-11 16:11:19 -04007866 exclude_super_stripes(root, cache);
Josef Bacik96303082009-07-13 21:29:25 -04007867
Josef Bacik817d52f2009-07-13 21:29:25 -04007868 add_new_free_space(cache, root->fs_info, chunk_offset,
7869 chunk_offset + size);
7870
Yan Zheng11833d62009-09-11 16:11:19 -04007871 free_excluded_extents(root, cache);
7872
Chris Mason6324fbf2008-03-24 15:01:59 -04007873 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7874 &cache->space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007875 BUG_ON(ret); /* -ENOMEM */
Li Zefanc7c144d2011-12-07 10:39:22 +08007876 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04007877
7878 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007879 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04007880 spin_unlock(&cache->space_info->lock);
7881
Yan, Zhengb742bb82010-05-16 10:46:24 -04007882 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007883
Josef Bacik0f9dd462008-09-23 13:14:11 -04007884 ret = btrfs_add_block_group_cache(root->fs_info, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007885 BUG_ON(ret); /* Logic error */
Chris Masonc286ac42008-07-22 23:06:41 -04007886
Josef Bacikea658ba2012-09-11 16:57:25 -04007887 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04007888
Chris Masond18a2c42008-04-04 15:40:00 -04007889 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04007890
Chris Mason6324fbf2008-03-24 15:01:59 -04007891 return 0;
7892}
Zheng Yan1a40e232008-09-26 10:09:34 -04007893
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007894static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
7895{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03007896 u64 extra_flags = chunk_to_extended(flags) &
7897 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007898
7899 if (flags & BTRFS_BLOCK_GROUP_DATA)
7900 fs_info->avail_data_alloc_bits &= ~extra_flags;
7901 if (flags & BTRFS_BLOCK_GROUP_METADATA)
7902 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
7903 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
7904 fs_info->avail_system_alloc_bits &= ~extra_flags;
7905}
7906
Zheng Yan1a40e232008-09-26 10:09:34 -04007907int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7908 struct btrfs_root *root, u64 group_start)
7909{
7910 struct btrfs_path *path;
7911 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04007912 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04007913 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04007914 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04007915 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04007916 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007917 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04007918 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04007919
Zheng Yan1a40e232008-09-26 10:09:34 -04007920 root = root->fs_info->extent_root;
7921
7922 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7923 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05007924 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04007925
liubo9f7c43c2011-03-07 02:13:33 +00007926 /*
7927 * Free the reserved super bytes from this block group before
7928 * remove it.
7929 */
7930 free_excluded_extents(root, block_group);
7931
Zheng Yan1a40e232008-09-26 10:09:34 -04007932 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02007933 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04007934 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7935 BTRFS_BLOCK_GROUP_RAID1 |
7936 BTRFS_BLOCK_GROUP_RAID10))
7937 factor = 2;
7938 else
7939 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04007940
Chris Mason44fb5512009-06-04 15:34:51 -04007941 /* make sure this block group isn't part of an allocation cluster */
7942 cluster = &root->fs_info->data_alloc_cluster;
7943 spin_lock(&cluster->refill_lock);
7944 btrfs_return_cluster_to_free_space(block_group, cluster);
7945 spin_unlock(&cluster->refill_lock);
7946
7947 /*
7948 * make sure this block group isn't part of a metadata
7949 * allocation cluster
7950 */
7951 cluster = &root->fs_info->meta_alloc_cluster;
7952 spin_lock(&cluster->refill_lock);
7953 btrfs_return_cluster_to_free_space(block_group, cluster);
7954 spin_unlock(&cluster->refill_lock);
7955
Zheng Yan1a40e232008-09-26 10:09:34 -04007956 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007957 if (!path) {
7958 ret = -ENOMEM;
7959 goto out;
7960 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007961
Ilya Dryomov10b2f342011-10-02 13:56:53 +03007962 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007963 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00007964 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007965 if (ret) {
7966 btrfs_add_delayed_iput(inode);
7967 goto out;
7968 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007969 clear_nlink(inode);
7970 /* One for the block groups ref */
7971 spin_lock(&block_group->lock);
7972 if (block_group->iref) {
7973 block_group->iref = 0;
7974 block_group->inode = NULL;
7975 spin_unlock(&block_group->lock);
7976 iput(inode);
7977 } else {
7978 spin_unlock(&block_group->lock);
7979 }
7980 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04007981 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04007982 }
7983
7984 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7985 key.offset = block_group->key.objectid;
7986 key.type = 0;
7987
7988 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7989 if (ret < 0)
7990 goto out;
7991 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02007992 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007993 if (ret == 0) {
7994 ret = btrfs_del_item(trans, tree_root, path);
7995 if (ret)
7996 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02007997 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04007998 }
7999
Yan Zheng3dfdb932009-01-21 10:49:16 -05008000 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008001 rb_erase(&block_group->cache_node,
8002 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05008003 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008004
Josef Bacik80eb2342008-10-29 14:49:05 -04008005 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008006 /*
8007 * we must use list_del_init so people can check to see if they
8008 * are still on the list after taking the semaphore
8009 */
8010 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008011 if (list_empty(&block_group->space_info->block_groups[index]))
8012 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008013 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008014
Josef Bacik817d52f2009-07-13 21:29:25 -04008015 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008016 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008017
8018 btrfs_remove_free_space_cache(block_group);
8019
Yan Zhengc146afa2008-11-12 14:34:12 -05008020 spin_lock(&block_group->space_info->lock);
8021 block_group->space_info->total_bytes -= block_group->key.offset;
8022 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008023 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008024 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008025
Josef Bacik0af3d002010-06-21 14:48:16 -04008026 memcpy(&key, &block_group->key, sizeof(key));
8027
Chris Mason283bb192009-07-24 16:30:55 -04008028 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008029
Chris Masonfa9c0d792009-04-03 09:47:43 -04008030 btrfs_put_block_group(block_group);
8031 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008032
8033 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8034 if (ret > 0)
8035 ret = -EIO;
8036 if (ret < 0)
8037 goto out;
8038
8039 ret = btrfs_del_item(trans, root, path);
8040out:
8041 btrfs_free_path(path);
8042 return ret;
8043}
liuboacce9522011-01-06 19:30:25 +08008044
liuboc59021f2011-03-07 02:13:14 +00008045int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8046{
8047 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008048 struct btrfs_super_block *disk_super;
8049 u64 features;
8050 u64 flags;
8051 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008052 int ret;
8053
David Sterba6c417612011-04-13 15:41:04 +02008054 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008055 if (!btrfs_super_root(disk_super))
8056 return 1;
liuboc59021f2011-03-07 02:13:14 +00008057
liubo1aba86d2011-04-08 08:44:37 +00008058 features = btrfs_super_incompat_flags(disk_super);
8059 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8060 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008061
liubo1aba86d2011-04-08 08:44:37 +00008062 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8063 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008064 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008065 goto out;
liuboc59021f2011-03-07 02:13:14 +00008066
liubo1aba86d2011-04-08 08:44:37 +00008067 if (mixed) {
8068 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8069 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8070 } else {
8071 flags = BTRFS_BLOCK_GROUP_METADATA;
8072 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8073 if (ret)
8074 goto out;
8075
8076 flags = BTRFS_BLOCK_GROUP_DATA;
8077 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8078 }
8079out:
liuboc59021f2011-03-07 02:13:14 +00008080 return ret;
8081}
8082
liuboacce9522011-01-06 19:30:25 +08008083int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8084{
8085 return unpin_extent_range(root, start, end);
8086}
8087
8088int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008089 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008090{
Li Dongyang5378e602011-03-24 10:24:27 +00008091 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008092}
Li Dongyangf7039b12011-03-24 10:24:28 +00008093
8094int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8095{
8096 struct btrfs_fs_info *fs_info = root->fs_info;
8097 struct btrfs_block_group_cache *cache = NULL;
8098 u64 group_trimmed;
8099 u64 start;
8100 u64 end;
8101 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008102 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008103 int ret = 0;
8104
Liu Bo2cac13e2012-02-09 18:17:41 +08008105 /*
8106 * try to trim all FS space, our block group may start from non-zero.
8107 */
8108 if (range->len == total_bytes)
8109 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8110 else
8111 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008112
8113 while (cache) {
8114 if (cache->key.objectid >= (range->start + range->len)) {
8115 btrfs_put_block_group(cache);
8116 break;
8117 }
8118
8119 start = max(range->start, cache->key.objectid);
8120 end = min(range->start + range->len,
8121 cache->key.objectid + cache->key.offset);
8122
8123 if (end - start >= range->minlen) {
8124 if (!block_group_cache_done(cache)) {
8125 ret = cache_block_group(cache, NULL, root, 0);
8126 if (!ret)
8127 wait_block_group_cache_done(cache);
8128 }
8129 ret = btrfs_trim_block_group(cache,
8130 &group_trimmed,
8131 start,
8132 end,
8133 range->minlen);
8134
8135 trimmed += group_trimmed;
8136 if (ret) {
8137 btrfs_put_block_group(cache);
8138 break;
8139 }
8140 }
8141
8142 cache = next_block_group(fs_info->tree_root, cache);
8143 }
8144
8145 range->len = trimmed;
8146 return ret;
8147}