blob: f84d53bc2f5d4ad397f866d8f60a9da7c671ab6a [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"
David Woodhouse53b381b2013-01-29 18:40:14 -050034#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040036#include "free-space-cache.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060037#include "math.h"
Chris Masonfec577f2007-02-26 10:40:21 -050038
Arne Jansen709c0482011-09-12 12:22:57 +020039#undef SCRAMBLE_DELAYED_REFS
40
Miao Xie9e622d62012-01-26 15:01:12 -050041/*
42 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040043 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
44 * if we really need one.
45 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040046 * CHUNK_ALLOC_LIMITED means to only try and allocate one
47 * if we have very few chunks already allocated. This is
48 * used as part of the clustering code to help make sure
49 * we have a good pool of storage to cluster in, without
50 * filling the FS with empty chunks
51 *
Miao Xie9e622d62012-01-26 15:01:12 -050052 * CHUNK_ALLOC_FORCE means it must try to allocate one
53 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040054 */
55enum {
56 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050057 CHUNK_ALLOC_LIMITED = 1,
58 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040059};
60
Josef Bacikfb25e912011-07-26 17:00:46 -040061/*
62 * Control how reservations are dealt with.
63 *
64 * RESERVE_FREE - freeing a reservation.
65 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
66 * ENOSPC accounting
67 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
68 * bytes_may_use as the ENOSPC accounting is done elsewhere
69 */
70enum {
71 RESERVE_FREE = 0,
72 RESERVE_ALLOC = 1,
73 RESERVE_ALLOC_NO_ACCOUNT = 2,
74};
75
Liu Boc53d6132012-12-27 09:01:19 +000076static int update_block_group(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 Bacik5d803662013-02-07 16:06:02 -0500106static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
107 u64 num_bytes);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000108int btrfs_pin_extent(struct btrfs_root *root,
109 u64 bytenr, u64 num_bytes, int reserved);
Josef Bacik6a632092009-02-20 11:00:09 -0500110
Josef Bacik817d52f2009-07-13 21:29:25 -0400111static noinline int
112block_group_cache_done(struct btrfs_block_group_cache *cache)
113{
114 smp_mb();
115 return cache->cached == BTRFS_CACHE_FINISHED;
116}
117
Josef Bacik0f9dd462008-09-23 13:14:11 -0400118static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
119{
120 return (cache->flags & bits) == bits;
121}
122
David Sterba62a45b62011-04-20 15:52:26 +0200123static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000124{
125 atomic_inc(&cache->count);
126}
127
128void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
129{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400130 if (atomic_dec_and_test(&cache->count)) {
131 WARN_ON(cache->pinned > 0);
132 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800133 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000134 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400135 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000136}
137
Josef Bacik0f9dd462008-09-23 13:14:11 -0400138/*
139 * this adds the block group to the fs_info rb tree for the block group
140 * cache
141 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500142static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400143 struct btrfs_block_group_cache *block_group)
144{
145 struct rb_node **p;
146 struct rb_node *parent = NULL;
147 struct btrfs_block_group_cache *cache;
148
149 spin_lock(&info->block_group_cache_lock);
150 p = &info->block_group_cache_tree.rb_node;
151
152 while (*p) {
153 parent = *p;
154 cache = rb_entry(parent, struct btrfs_block_group_cache,
155 cache_node);
156 if (block_group->key.objectid < cache->key.objectid) {
157 p = &(*p)->rb_left;
158 } else if (block_group->key.objectid > cache->key.objectid) {
159 p = &(*p)->rb_right;
160 } else {
161 spin_unlock(&info->block_group_cache_lock);
162 return -EEXIST;
163 }
164 }
165
166 rb_link_node(&block_group->cache_node, parent, p);
167 rb_insert_color(&block_group->cache_node,
168 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000169
170 if (info->first_logical_byte > block_group->key.objectid)
171 info->first_logical_byte = block_group->key.objectid;
172
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 spin_unlock(&info->block_group_cache_lock);
174
175 return 0;
176}
177
178/*
179 * This will return the block group at or after bytenr if contains is 0, else
180 * it will return the block group that contains the bytenr
181 */
182static struct btrfs_block_group_cache *
183block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
184 int contains)
185{
186 struct btrfs_block_group_cache *cache, *ret = NULL;
187 struct rb_node *n;
188 u64 end, start;
189
190 spin_lock(&info->block_group_cache_lock);
191 n = info->block_group_cache_tree.rb_node;
192
193 while (n) {
194 cache = rb_entry(n, struct btrfs_block_group_cache,
195 cache_node);
196 end = cache->key.objectid + cache->key.offset - 1;
197 start = cache->key.objectid;
198
199 if (bytenr < start) {
200 if (!contains && (!ret || start < ret->key.objectid))
201 ret = cache;
202 n = n->rb_left;
203 } else if (bytenr > start) {
204 if (contains && bytenr <= end) {
205 ret = cache;
206 break;
207 }
208 n = n->rb_right;
209 } else {
210 ret = cache;
211 break;
212 }
213 }
Liu Boa1897fd2012-12-27 09:01:23 +0000214 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000215 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000216 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
217 info->first_logical_byte = ret->key.objectid;
218 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400219 spin_unlock(&info->block_group_cache_lock);
220
221 return ret;
222}
223
Yan Zheng11833d62009-09-11 16:11:19 -0400224static int add_excluded_extent(struct btrfs_root *root,
225 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400226{
Yan Zheng11833d62009-09-11 16:11:19 -0400227 u64 end = start + num_bytes - 1;
228 set_extent_bits(&root->fs_info->freed_extents[0],
229 start, end, EXTENT_UPTODATE, GFP_NOFS);
230 set_extent_bits(&root->fs_info->freed_extents[1],
231 start, end, EXTENT_UPTODATE, GFP_NOFS);
232 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400233}
234
Yan Zheng11833d62009-09-11 16:11:19 -0400235static void free_excluded_extents(struct btrfs_root *root,
236 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400237{
Yan Zheng11833d62009-09-11 16:11:19 -0400238 u64 start, end;
239
240 start = cache->key.objectid;
241 end = start + cache->key.offset - 1;
242
243 clear_extent_bits(&root->fs_info->freed_extents[0],
244 start, end, EXTENT_UPTODATE, GFP_NOFS);
245 clear_extent_bits(&root->fs_info->freed_extents[1],
246 start, end, EXTENT_UPTODATE, GFP_NOFS);
247}
248
249static int exclude_super_stripes(struct btrfs_root *root,
250 struct btrfs_block_group_cache *cache)
251{
Josef Bacik817d52f2009-07-13 21:29:25 -0400252 u64 bytenr;
253 u64 *logical;
254 int stripe_len;
255 int i, nr, ret;
256
Yan, Zheng06b23312009-11-26 09:31:11 +0000257 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
258 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
259 cache->bytes_super += stripe_len;
260 ret = add_excluded_extent(root, cache->key.objectid,
261 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400262 if (ret)
263 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000264 }
265
Josef Bacik817d52f2009-07-13 21:29:25 -0400266 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
267 bytenr = btrfs_sb_offset(i);
268 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
269 cache->key.objectid, bytenr,
270 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400271 if (ret)
272 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400273
Josef Bacik817d52f2009-07-13 21:29:25 -0400274 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400275 u64 start, len;
276
277 if (logical[nr] > cache->key.objectid +
278 cache->key.offset)
279 continue;
280
281 if (logical[nr] + stripe_len <= cache->key.objectid)
282 continue;
283
284 start = logical[nr];
285 if (start < cache->key.objectid) {
286 start = cache->key.objectid;
287 len = (logical[nr] + stripe_len) - start;
288 } else {
289 len = min_t(u64, stripe_len,
290 cache->key.objectid +
291 cache->key.offset - start);
292 }
293
294 cache->bytes_super += len;
295 ret = add_excluded_extent(root, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400296 if (ret) {
297 kfree(logical);
298 return ret;
299 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400300 }
Yan Zheng11833d62009-09-11 16:11:19 -0400301
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 kfree(logical);
303 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400304 return 0;
305}
306
Yan Zheng11833d62009-09-11 16:11:19 -0400307static struct btrfs_caching_control *
308get_caching_control(struct btrfs_block_group_cache *cache)
309{
310 struct btrfs_caching_control *ctl;
311
312 spin_lock(&cache->lock);
313 if (cache->cached != BTRFS_CACHE_STARTED) {
314 spin_unlock(&cache->lock);
315 return NULL;
316 }
317
Josef Bacikdde5abe2010-09-16 16:17:03 -0400318 /* We're loading it the fast way, so we don't have a caching_ctl. */
319 if (!cache->caching_ctl) {
320 spin_unlock(&cache->lock);
321 return NULL;
322 }
323
Yan Zheng11833d62009-09-11 16:11:19 -0400324 ctl = cache->caching_ctl;
325 atomic_inc(&ctl->count);
326 spin_unlock(&cache->lock);
327 return ctl;
328}
329
330static void put_caching_control(struct btrfs_caching_control *ctl)
331{
332 if (atomic_dec_and_test(&ctl->count))
333 kfree(ctl);
334}
335
Josef Bacik0f9dd462008-09-23 13:14:11 -0400336/*
337 * this is only called by cache_block_group, since we could have freed extents
338 * we need to check the pinned_extents for any extents that can't be used yet
339 * since their free space will be released as soon as the transaction commits.
340 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400341static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400342 struct btrfs_fs_info *info, u64 start, u64 end)
343{
Josef Bacik817d52f2009-07-13 21:29:25 -0400344 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400345 int ret;
346
347 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400348 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400349 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400350 EXTENT_DIRTY | EXTENT_UPTODATE,
351 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400352 if (ret)
353 break;
354
Yan, Zheng06b23312009-11-26 09:31:11 +0000355 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400356 start = extent_end + 1;
357 } else if (extent_start > start && extent_start < end) {
358 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400359 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500360 ret = btrfs_add_free_space(block_group, start,
361 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100362 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400363 start = extent_end + 1;
364 } else {
365 break;
366 }
367 }
368
369 if (start < end) {
370 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400371 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500372 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100373 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400374 }
375
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400377}
378
Josef Bacikbab39bf2011-06-30 14:42:28 -0400379static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400380{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400381 struct btrfs_block_group_cache *block_group;
382 struct btrfs_fs_info *fs_info;
383 struct btrfs_caching_control *caching_ctl;
384 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400385 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400386 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400387 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400388 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400389 u64 last = 0;
390 u32 nritems;
391 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400392
Josef Bacikbab39bf2011-06-30 14:42:28 -0400393 caching_ctl = container_of(work, struct btrfs_caching_control, work);
394 block_group = caching_ctl->block_group;
395 fs_info = block_group->fs_info;
396 extent_root = fs_info->extent_root;
397
Chris Masone37c9e62007-05-09 20:13:14 -0400398 path = btrfs_alloc_path();
399 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400400 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400401
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400403
Chris Mason5cd57b22008-06-25 16:01:30 -0400404 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400405 * We don't want to deadlock with somebody trying to allocate a new
406 * extent for the extent root while also trying to search the extent
407 * root to add free space. So we skip locking and search the commit
408 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400409 */
410 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400411 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400412 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400413
Yan Zhenge4404d62008-12-12 10:03:26 -0500414 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400415 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400416 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400417again:
Yan Zheng11833d62009-09-11 16:11:19 -0400418 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400419 /* need to make sure the commit_root doesn't disappear */
420 down_read(&fs_info->extent_commit_sem);
421
Yan Zheng11833d62009-09-11 16:11:19 -0400422 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400423 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400424 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500425
Yan Zheng11833d62009-09-11 16:11:19 -0400426 leaf = path->nodes[0];
427 nritems = btrfs_header_nritems(leaf);
428
Chris Masond3977122009-01-05 21:25:51 -0500429 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200430 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400431 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400432 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400433 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400434
Yan Zheng11833d62009-09-11 16:11:19 -0400435 if (path->slots[0] < nritems) {
436 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
437 } else {
438 ret = find_next_key(path, 0, &key);
439 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400440 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400441
Josef Bacik0a3896d2013-04-19 14:37:26 -0400442 if (need_resched()) {
Josef Bacik589d8ad2011-05-11 17:30:53 -0400443 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400444 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400445 up_read(&fs_info->extent_commit_sem);
446 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400447 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400448 goto again;
449 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400450
451 ret = btrfs_next_leaf(extent_root, path);
452 if (ret < 0)
453 goto err;
454 if (ret)
455 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400456 leaf = path->nodes[0];
457 nritems = btrfs_header_nritems(leaf);
458 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400459 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400460
Yan Zheng11833d62009-09-11 16:11:19 -0400461 if (key.objectid < block_group->key.objectid) {
462 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400463 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400464 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400465
Chris Masone37c9e62007-05-09 20:13:14 -0400466 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400467 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400468 break;
Yan7d7d6062007-09-14 16:15:28 -0400469
Josef Bacik3173a182013-03-07 14:22:04 -0500470 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
471 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400472 total_found += add_new_free_space(block_group,
473 fs_info, last,
474 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500475 if (key.type == BTRFS_METADATA_ITEM_KEY)
476 last = key.objectid +
477 fs_info->tree_root->leafsize;
478 else
479 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400480
Yan Zheng11833d62009-09-11 16:11:19 -0400481 if (total_found > (1024 * 1024 * 2)) {
482 total_found = 0;
483 wake_up(&caching_ctl->wait);
484 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400485 }
Chris Masone37c9e62007-05-09 20:13:14 -0400486 path->slots[0]++;
487 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400488 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400489
490 total_found += add_new_free_space(block_group, fs_info, last,
491 block_group->key.objectid +
492 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400493 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400494
495 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400496 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400497 block_group->cached = BTRFS_CACHE_FINISHED;
498 spin_unlock(&block_group->lock);
499
Chris Mason54aa1f42007-06-22 14:16:25 -0400500err:
Chris Masone37c9e62007-05-09 20:13:14 -0400501 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400502 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400503
Yan Zheng11833d62009-09-11 16:11:19 -0400504 free_excluded_extents(extent_root, block_group);
505
506 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400507out:
Yan Zheng11833d62009-09-11 16:11:19 -0400508 wake_up(&caching_ctl->wait);
509
510 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000511 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400512}
513
Josef Bacik9d66e232010-08-25 16:54:15 -0400514static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400515 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400516{
Josef Bacik291c7d22011-11-14 13:52:14 -0500517 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400518 struct btrfs_fs_info *fs_info = cache->fs_info;
519 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400520 int ret = 0;
521
Josef Bacik291c7d22011-11-14 13:52:14 -0500522 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100523 if (!caching_ctl)
524 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500525
526 INIT_LIST_HEAD(&caching_ctl->list);
527 mutex_init(&caching_ctl->mutex);
528 init_waitqueue_head(&caching_ctl->wait);
529 caching_ctl->block_group = cache;
530 caching_ctl->progress = cache->key.objectid;
531 atomic_set(&caching_ctl->count, 1);
532 caching_ctl->work.func = caching_thread;
533
534 spin_lock(&cache->lock);
535 /*
536 * This should be a rare occasion, but this could happen I think in the
537 * case where one thread starts to load the space cache info, and then
538 * some other thread starts a transaction commit which tries to do an
539 * allocation while the other thread is still loading the space cache
540 * info. The previous loop should have kept us from choosing this block
541 * group, but if we've moved to the state where we will wait on caching
542 * block groups we need to first check if we're doing a fast load here,
543 * so we can wait for it to finish, otherwise we could end up allocating
544 * from a block group who's cache gets evicted for one reason or
545 * another.
546 */
547 while (cache->cached == BTRFS_CACHE_FAST) {
548 struct btrfs_caching_control *ctl;
549
550 ctl = cache->caching_ctl;
551 atomic_inc(&ctl->count);
552 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
553 spin_unlock(&cache->lock);
554
555 schedule();
556
557 finish_wait(&ctl->wait, &wait);
558 put_caching_control(ctl);
559 spin_lock(&cache->lock);
560 }
561
562 if (cache->cached != BTRFS_CACHE_NO) {
563 spin_unlock(&cache->lock);
564 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400565 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500566 }
567 WARN_ON(cache->caching_ctl);
568 cache->caching_ctl = caching_ctl;
569 cache->cached = BTRFS_CACHE_FAST;
570 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400571
Josef Bacikd53ba472012-04-12 16:03:57 -0400572 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400573 ret = load_free_space_cache(fs_info, cache);
574
575 spin_lock(&cache->lock);
576 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500577 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400578 cache->cached = BTRFS_CACHE_FINISHED;
579 cache->last_byte_to_unpin = (u64)-1;
580 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500581 if (load_cache_only) {
582 cache->caching_ctl = NULL;
583 cache->cached = BTRFS_CACHE_NO;
584 } else {
585 cache->cached = BTRFS_CACHE_STARTED;
586 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400587 }
588 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500589 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000590 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500591 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000592 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400593 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000594 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500595 } else {
596 /*
597 * We are not going to do the fast caching, set cached to the
598 * appropriate value and wakeup any waiters.
599 */
600 spin_lock(&cache->lock);
601 if (load_cache_only) {
602 cache->caching_ctl = NULL;
603 cache->cached = BTRFS_CACHE_NO;
604 } else {
605 cache->cached = BTRFS_CACHE_STARTED;
606 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400607 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500608 wake_up(&caching_ctl->wait);
609 }
610
611 if (load_cache_only) {
612 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400613 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400614 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400615
Yan Zheng11833d62009-09-11 16:11:19 -0400616 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500617 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400618 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
619 up_write(&fs_info->extent_commit_sem);
620
Josef Bacik11dfe352009-11-13 20:12:59 +0000621 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400622
Josef Bacikbab39bf2011-06-30 14:42:28 -0400623 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400624
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400625 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400626}
627
Josef Bacik0f9dd462008-09-23 13:14:11 -0400628/*
629 * return the block group that starts at or after bytenr
630 */
Chris Masond3977122009-01-05 21:25:51 -0500631static struct btrfs_block_group_cache *
632btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400633{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400634 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400635
Josef Bacik0f9dd462008-09-23 13:14:11 -0400636 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400637
Josef Bacik0f9dd462008-09-23 13:14:11 -0400638 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400639}
640
Josef Bacik0f9dd462008-09-23 13:14:11 -0400641/*
Sankar P9f556842009-05-14 13:52:22 -0400642 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400643 */
Chris Masond3977122009-01-05 21:25:51 -0500644struct btrfs_block_group_cache *btrfs_lookup_block_group(
645 struct btrfs_fs_info *info,
646 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400647{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400648 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400649
Josef Bacik0f9dd462008-09-23 13:14:11 -0400650 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400651
Josef Bacik0f9dd462008-09-23 13:14:11 -0400652 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400653}
Chris Mason0b86a832008-03-24 15:01:56 -0400654
Josef Bacik0f9dd462008-09-23 13:14:11 -0400655static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
656 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400657{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400658 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400659 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400660
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200661 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb822010-05-16 10:46:24 -0400662
Chris Mason4184ea72009-03-10 12:39:20 -0400663 rcu_read_lock();
664 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400665 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400666 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400667 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400668 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400669 }
Chris Mason4184ea72009-03-10 12:39:20 -0400670 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400671 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400672}
673
Chris Mason4184ea72009-03-10 12:39:20 -0400674/*
675 * after adding space to the filesystem, we need to clear the full flags
676 * on all the space infos.
677 */
678void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
679{
680 struct list_head *head = &info->space_info;
681 struct btrfs_space_info *found;
682
683 rcu_read_lock();
684 list_for_each_entry_rcu(found, head, list)
685 found->full = 0;
686 rcu_read_unlock();
687}
688
Chris Masone02119d2008-09-05 16:13:11 -0400689/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400690int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400691{
692 int ret;
693 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400694 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400695
Zheng Yan31840ae2008-09-23 13:14:14 -0400696 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700697 if (!path)
698 return -ENOMEM;
699
Chris Masone02119d2008-09-05 16:13:11 -0400700 key.objectid = start;
701 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500702 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400703 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
704 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500705 if (ret > 0) {
706 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
707 if (key.objectid == start &&
708 key.type == BTRFS_METADATA_ITEM_KEY)
709 ret = 0;
710 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400711 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500712 return ret;
713}
714
Chris Masond8d5f3e2007-12-11 12:42:00 -0500715/*
Josef Bacik3173a182013-03-07 14:22:04 -0500716 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400717 *
718 * the head node for delayed ref is used to store the sum of all the
719 * reference count modifications queued up in the rbtree. the head
720 * node may also store the extent flags to set. This way you can check
721 * to see what the reference count and extent flags would be if all of
722 * the delayed refs are not processed.
723 */
724int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
725 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500726 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400727{
728 struct btrfs_delayed_ref_head *head;
729 struct btrfs_delayed_ref_root *delayed_refs;
730 struct btrfs_path *path;
731 struct btrfs_extent_item *ei;
732 struct extent_buffer *leaf;
733 struct btrfs_key key;
734 u32 item_size;
735 u64 num_refs;
736 u64 extent_flags;
737 int ret;
738
Josef Bacik3173a182013-03-07 14:22:04 -0500739 /*
740 * If we don't have skinny metadata, don't bother doing anything
741 * different
742 */
743 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
744 offset = root->leafsize;
745 metadata = 0;
746 }
747
Yan, Zhenga22285a2010-05-16 10:48:46 -0400748 path = btrfs_alloc_path();
749 if (!path)
750 return -ENOMEM;
751
Josef Bacik3173a182013-03-07 14:22:04 -0500752 if (metadata) {
753 key.objectid = bytenr;
754 key.type = BTRFS_METADATA_ITEM_KEY;
755 key.offset = offset;
756 } else {
757 key.objectid = bytenr;
758 key.type = BTRFS_EXTENT_ITEM_KEY;
759 key.offset = offset;
760 }
761
Yan, Zhenga22285a2010-05-16 10:48:46 -0400762 if (!trans) {
763 path->skip_locking = 1;
764 path->search_commit_root = 1;
765 }
766again:
767 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
768 &key, path, 0, 0);
769 if (ret < 0)
770 goto out_free;
771
Josef Bacik3173a182013-03-07 14:22:04 -0500772 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
773 key.type = BTRFS_EXTENT_ITEM_KEY;
774 key.offset = root->leafsize;
775 btrfs_release_path(path);
776 goto again;
777 }
778
Yan, Zhenga22285a2010-05-16 10:48:46 -0400779 if (ret == 0) {
780 leaf = path->nodes[0];
781 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
782 if (item_size >= sizeof(*ei)) {
783 ei = btrfs_item_ptr(leaf, path->slots[0],
784 struct btrfs_extent_item);
785 num_refs = btrfs_extent_refs(leaf, ei);
786 extent_flags = btrfs_extent_flags(leaf, ei);
787 } else {
788#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
789 struct btrfs_extent_item_v0 *ei0;
790 BUG_ON(item_size != sizeof(*ei0));
791 ei0 = btrfs_item_ptr(leaf, path->slots[0],
792 struct btrfs_extent_item_v0);
793 num_refs = btrfs_extent_refs_v0(leaf, ei0);
794 /* FIXME: this isn't correct for data */
795 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
796#else
797 BUG();
798#endif
799 }
800 BUG_ON(num_refs == 0);
801 } else {
802 num_refs = 0;
803 extent_flags = 0;
804 ret = 0;
805 }
806
807 if (!trans)
808 goto out;
809
810 delayed_refs = &trans->transaction->delayed_refs;
811 spin_lock(&delayed_refs->lock);
812 head = btrfs_find_delayed_ref_head(trans, bytenr);
813 if (head) {
814 if (!mutex_trylock(&head->mutex)) {
815 atomic_inc(&head->node.refs);
816 spin_unlock(&delayed_refs->lock);
817
David Sterbab3b4aa72011-04-21 01:20:15 +0200818 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400819
David Sterba8cc33e52011-05-02 15:29:25 +0200820 /*
821 * Mutex was contended, block until it's released and try
822 * again
823 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400824 mutex_lock(&head->mutex);
825 mutex_unlock(&head->mutex);
826 btrfs_put_delayed_ref(&head->node);
827 goto again;
828 }
829 if (head->extent_op && head->extent_op->update_flags)
830 extent_flags |= head->extent_op->flags_to_set;
831 else
832 BUG_ON(num_refs == 0);
833
834 num_refs += head->node.ref_mod;
835 mutex_unlock(&head->mutex);
836 }
837 spin_unlock(&delayed_refs->lock);
838out:
839 WARN_ON(num_refs == 0);
840 if (refs)
841 *refs = num_refs;
842 if (flags)
843 *flags = extent_flags;
844out_free:
845 btrfs_free_path(path);
846 return ret;
847}
848
849/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500850 * Back reference rules. Back refs have three main goals:
851 *
852 * 1) differentiate between all holders of references to an extent so that
853 * when a reference is dropped we can make sure it was a valid reference
854 * before freeing the extent.
855 *
856 * 2) Provide enough information to quickly find the holders of an extent
857 * if we notice a given block is corrupted or bad.
858 *
859 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
860 * maintenance. This is actually the same as #2, but with a slightly
861 * different use case.
862 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 * There are two kinds of back refs. The implicit back refs is optimized
864 * for pointers in non-shared tree blocks. For a given pointer in a block,
865 * back refs of this kind provide information about the block's owner tree
866 * and the pointer's key. These information allow us to find the block by
867 * b-tree searching. The full back refs is for pointers in tree blocks not
868 * referenced by their owner trees. The location of tree block is recorded
869 * in the back refs. Actually the full back refs is generic, and can be
870 * used in all cases the implicit back refs is used. The major shortcoming
871 * of the full back refs is its overhead. Every time a tree block gets
872 * COWed, we have to update back refs entry for all pointers in it.
873 *
874 * For a newly allocated tree block, we use implicit back refs for
875 * pointers in it. This means most tree related operations only involve
876 * implicit back refs. For a tree block created in old transaction, the
877 * only way to drop a reference to it is COW it. So we can detect the
878 * event that tree block loses its owner tree's reference and do the
879 * back refs conversion.
880 *
881 * When a tree block is COW'd through a tree, there are four cases:
882 *
883 * The reference count of the block is one and the tree is the block's
884 * owner tree. Nothing to do in this case.
885 *
886 * The reference count of the block is one and the tree is not the
887 * block's owner tree. In this case, full back refs is used for pointers
888 * in the block. Remove these full back refs, add implicit back refs for
889 * every pointers in the new block.
890 *
891 * The reference count of the block is greater than one and the tree is
892 * the block's owner tree. In this case, implicit back refs is used for
893 * pointers in the block. Add full back refs for every pointers in the
894 * block, increase lower level extents' reference counts. The original
895 * implicit back refs are entailed to the new block.
896 *
897 * The reference count of the block is greater than one and the tree is
898 * not the block's owner tree. Add implicit back refs for every pointer in
899 * the new block, increase lower level extents' reference count.
900 *
901 * Back Reference Key composing:
902 *
903 * The key objectid corresponds to the first byte in the extent,
904 * The key type is used to differentiate between types of back refs.
905 * There are different meanings of the key offset for different types
906 * of back refs.
907 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500908 * File extents can be referenced by:
909 *
910 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400911 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500912 * - different offsets inside a file (bookend extents in file.c)
913 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400914 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500915 *
916 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500917 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 * - original offset in the file
919 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400920 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400921 * The key offset for the implicit back refs is hash of the first
922 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500923 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500925 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500927 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400928 * The key offset for the implicit back refs is the first byte of
929 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500930 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400931 * When a file extent is allocated, The implicit back refs is used.
932 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500933 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400934 * (root_key.objectid, inode objectid, offset in file, 1)
935 *
936 * When a file extent is removed file truncation, we find the
937 * corresponding implicit back refs and check the following fields:
938 *
939 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500940 *
941 * Btree extents can be referenced by:
942 *
943 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500944 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 * Both the implicit back refs and the full back refs for tree blocks
946 * only consist of key. The key offset for the implicit back refs is
947 * objectid of block's owner tree. The key offset for the full back refs
948 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500949 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 * When implicit back refs is used, information about the lowest key and
951 * level of the tree block are required. These information are stored in
952 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500953 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400954
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400955#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
956static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
957 struct btrfs_root *root,
958 struct btrfs_path *path,
959 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500960{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 struct btrfs_extent_item *item;
962 struct btrfs_extent_item_v0 *ei0;
963 struct btrfs_extent_ref_v0 *ref0;
964 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400965 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400966 struct btrfs_key key;
967 struct btrfs_key found_key;
968 u32 new_size = sizeof(*item);
969 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500970 int ret;
971
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400972 leaf = path->nodes[0];
973 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500974
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400975 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
976 ei0 = btrfs_item_ptr(leaf, path->slots[0],
977 struct btrfs_extent_item_v0);
978 refs = btrfs_extent_refs_v0(leaf, ei0);
979
980 if (owner == (u64)-1) {
981 while (1) {
982 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
983 ret = btrfs_next_leaf(root, path);
984 if (ret < 0)
985 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400987 leaf = path->nodes[0];
988 }
989 btrfs_item_key_to_cpu(leaf, &found_key,
990 path->slots[0]);
991 BUG_ON(key.objectid != found_key.objectid);
992 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
993 path->slots[0]++;
994 continue;
995 }
996 ref0 = btrfs_item_ptr(leaf, path->slots[0],
997 struct btrfs_extent_ref_v0);
998 owner = btrfs_ref_objectid_v0(leaf, ref0);
999 break;
1000 }
1001 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001002 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001003
1004 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1005 new_size += sizeof(*bi);
1006
1007 new_size -= sizeof(*ei0);
1008 ret = btrfs_search_slot(trans, root, &key, path,
1009 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001010 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001011 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001012 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001014 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015
1016 leaf = path->nodes[0];
1017 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1018 btrfs_set_extent_refs(leaf, item, refs);
1019 /* FIXME: get real generation */
1020 btrfs_set_extent_generation(leaf, item, 0);
1021 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1022 btrfs_set_extent_flags(leaf, item,
1023 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1024 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1025 bi = (struct btrfs_tree_block_info *)(item + 1);
1026 /* FIXME: get first key of the block */
1027 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1028 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1029 } else {
1030 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1031 }
1032 btrfs_mark_buffer_dirty(leaf);
1033 return 0;
1034}
1035#endif
1036
1037static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1038{
1039 u32 high_crc = ~(u32)0;
1040 u32 low_crc = ~(u32)0;
1041 __le64 lenum;
1042
1043 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001044 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001045 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001046 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001048 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001049
1050 return ((u64)high_crc << 31) ^ (u64)low_crc;
1051}
1052
1053static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1054 struct btrfs_extent_data_ref *ref)
1055{
1056 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1057 btrfs_extent_data_ref_objectid(leaf, ref),
1058 btrfs_extent_data_ref_offset(leaf, ref));
1059}
1060
1061static int match_extent_data_ref(struct extent_buffer *leaf,
1062 struct btrfs_extent_data_ref *ref,
1063 u64 root_objectid, u64 owner, u64 offset)
1064{
1065 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1066 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1067 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1068 return 0;
1069 return 1;
1070}
1071
1072static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1073 struct btrfs_root *root,
1074 struct btrfs_path *path,
1075 u64 bytenr, u64 parent,
1076 u64 root_objectid,
1077 u64 owner, u64 offset)
1078{
1079 struct btrfs_key key;
1080 struct btrfs_extent_data_ref *ref;
1081 struct extent_buffer *leaf;
1082 u32 nritems;
1083 int ret;
1084 int recow;
1085 int err = -ENOENT;
1086
1087 key.objectid = bytenr;
1088 if (parent) {
1089 key.type = BTRFS_SHARED_DATA_REF_KEY;
1090 key.offset = parent;
1091 } else {
1092 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1093 key.offset = hash_extent_data_ref(root_objectid,
1094 owner, offset);
1095 }
1096again:
1097 recow = 0;
1098 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1099 if (ret < 0) {
1100 err = ret;
1101 goto fail;
1102 }
1103
1104 if (parent) {
1105 if (!ret)
1106 return 0;
1107#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1108 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001109 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001110 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1111 if (ret < 0) {
1112 err = ret;
1113 goto fail;
1114 }
1115 if (!ret)
1116 return 0;
1117#endif
1118 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001119 }
1120
1121 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001122 nritems = btrfs_header_nritems(leaf);
1123 while (1) {
1124 if (path->slots[0] >= nritems) {
1125 ret = btrfs_next_leaf(root, path);
1126 if (ret < 0)
1127 err = ret;
1128 if (ret)
1129 goto fail;
1130
1131 leaf = path->nodes[0];
1132 nritems = btrfs_header_nritems(leaf);
1133 recow = 1;
1134 }
1135
1136 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1137 if (key.objectid != bytenr ||
1138 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1139 goto fail;
1140
1141 ref = btrfs_item_ptr(leaf, path->slots[0],
1142 struct btrfs_extent_data_ref);
1143
1144 if (match_extent_data_ref(leaf, ref, root_objectid,
1145 owner, offset)) {
1146 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001147 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001148 goto again;
1149 }
1150 err = 0;
1151 break;
1152 }
1153 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001154 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001155fail:
1156 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001157}
1158
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001159static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1160 struct btrfs_root *root,
1161 struct btrfs_path *path,
1162 u64 bytenr, u64 parent,
1163 u64 root_objectid, u64 owner,
1164 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001165{
1166 struct btrfs_key key;
1167 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001168 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001169 u32 num_refs;
1170 int ret;
1171
1172 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001173 if (parent) {
1174 key.type = BTRFS_SHARED_DATA_REF_KEY;
1175 key.offset = parent;
1176 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001177 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001178 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1179 key.offset = hash_extent_data_ref(root_objectid,
1180 owner, offset);
1181 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001182 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183
1184 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1185 if (ret && ret != -EEXIST)
1186 goto fail;
1187
1188 leaf = path->nodes[0];
1189 if (parent) {
1190 struct btrfs_shared_data_ref *ref;
1191 ref = btrfs_item_ptr(leaf, path->slots[0],
1192 struct btrfs_shared_data_ref);
1193 if (ret == 0) {
1194 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1195 } else {
1196 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1197 num_refs += refs_to_add;
1198 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1199 }
1200 } else {
1201 struct btrfs_extent_data_ref *ref;
1202 while (ret == -EEXIST) {
1203 ref = btrfs_item_ptr(leaf, path->slots[0],
1204 struct btrfs_extent_data_ref);
1205 if (match_extent_data_ref(leaf, ref, root_objectid,
1206 owner, offset))
1207 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001208 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209 key.offset++;
1210 ret = btrfs_insert_empty_item(trans, root, path, &key,
1211 size);
1212 if (ret && ret != -EEXIST)
1213 goto fail;
1214
1215 leaf = path->nodes[0];
1216 }
1217 ref = btrfs_item_ptr(leaf, path->slots[0],
1218 struct btrfs_extent_data_ref);
1219 if (ret == 0) {
1220 btrfs_set_extent_data_ref_root(leaf, ref,
1221 root_objectid);
1222 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1223 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1224 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1225 } else {
1226 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1227 num_refs += refs_to_add;
1228 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1229 }
1230 }
1231 btrfs_mark_buffer_dirty(leaf);
1232 ret = 0;
1233fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001234 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001235 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001236}
1237
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001238static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1239 struct btrfs_root *root,
1240 struct btrfs_path *path,
1241 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001242{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001243 struct btrfs_key key;
1244 struct btrfs_extent_data_ref *ref1 = NULL;
1245 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001246 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001247 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001248 int ret = 0;
1249
1250 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001251 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1252
1253 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1254 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1255 struct btrfs_extent_data_ref);
1256 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1257 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1258 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1259 struct btrfs_shared_data_ref);
1260 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1261#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1262 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1263 struct btrfs_extent_ref_v0 *ref0;
1264 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1265 struct btrfs_extent_ref_v0);
1266 num_refs = btrfs_ref_count_v0(leaf, ref0);
1267#endif
1268 } else {
1269 BUG();
1270 }
1271
Chris Mason56bec292009-03-13 10:10:06 -04001272 BUG_ON(num_refs < refs_to_drop);
1273 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001274
Zheng Yan31840ae2008-09-23 13:14:14 -04001275 if (num_refs == 0) {
1276 ret = btrfs_del_item(trans, root, path);
1277 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001278 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1279 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1280 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1281 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1282#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1283 else {
1284 struct btrfs_extent_ref_v0 *ref0;
1285 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1286 struct btrfs_extent_ref_v0);
1287 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1288 }
1289#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001290 btrfs_mark_buffer_dirty(leaf);
1291 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001292 return ret;
1293}
1294
1295static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1296 struct btrfs_path *path,
1297 struct btrfs_extent_inline_ref *iref)
1298{
1299 struct btrfs_key key;
1300 struct extent_buffer *leaf;
1301 struct btrfs_extent_data_ref *ref1;
1302 struct btrfs_shared_data_ref *ref2;
1303 u32 num_refs = 0;
1304
1305 leaf = path->nodes[0];
1306 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1307 if (iref) {
1308 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1309 BTRFS_EXTENT_DATA_REF_KEY) {
1310 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1311 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1312 } else {
1313 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1314 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1315 }
1316 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1317 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1318 struct btrfs_extent_data_ref);
1319 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1320 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1321 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1322 struct btrfs_shared_data_ref);
1323 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1324#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1325 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1326 struct btrfs_extent_ref_v0 *ref0;
1327 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1328 struct btrfs_extent_ref_v0);
1329 num_refs = btrfs_ref_count_v0(leaf, ref0);
1330#endif
1331 } else {
1332 WARN_ON(1);
1333 }
1334 return num_refs;
1335}
1336
1337static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1338 struct btrfs_root *root,
1339 struct btrfs_path *path,
1340 u64 bytenr, u64 parent,
1341 u64 root_objectid)
1342{
1343 struct btrfs_key key;
1344 int ret;
1345
1346 key.objectid = bytenr;
1347 if (parent) {
1348 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1349 key.offset = parent;
1350 } else {
1351 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1352 key.offset = root_objectid;
1353 }
1354
1355 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1356 if (ret > 0)
1357 ret = -ENOENT;
1358#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1359 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001360 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001361 key.type = BTRFS_EXTENT_REF_V0_KEY;
1362 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1363 if (ret > 0)
1364 ret = -ENOENT;
1365 }
1366#endif
1367 return ret;
1368}
1369
1370static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1371 struct btrfs_root *root,
1372 struct btrfs_path *path,
1373 u64 bytenr, u64 parent,
1374 u64 root_objectid)
1375{
1376 struct btrfs_key key;
1377 int ret;
1378
1379 key.objectid = bytenr;
1380 if (parent) {
1381 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1382 key.offset = parent;
1383 } else {
1384 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1385 key.offset = root_objectid;
1386 }
1387
1388 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001389 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001390 return ret;
1391}
1392
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393static inline int extent_ref_type(u64 parent, u64 owner)
1394{
1395 int type;
1396 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1397 if (parent > 0)
1398 type = BTRFS_SHARED_BLOCK_REF_KEY;
1399 else
1400 type = BTRFS_TREE_BLOCK_REF_KEY;
1401 } else {
1402 if (parent > 0)
1403 type = BTRFS_SHARED_DATA_REF_KEY;
1404 else
1405 type = BTRFS_EXTENT_DATA_REF_KEY;
1406 }
1407 return type;
1408}
1409
Yan Zheng2c47e6052009-06-27 21:07:35 -04001410static int find_next_key(struct btrfs_path *path, int level,
1411 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001412
1413{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001414 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001415 if (!path->nodes[level])
1416 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001417 if (path->slots[level] + 1 >=
1418 btrfs_header_nritems(path->nodes[level]))
1419 continue;
1420 if (level == 0)
1421 btrfs_item_key_to_cpu(path->nodes[level], key,
1422 path->slots[level] + 1);
1423 else
1424 btrfs_node_key_to_cpu(path->nodes[level], key,
1425 path->slots[level] + 1);
1426 return 0;
1427 }
1428 return 1;
1429}
1430
1431/*
1432 * look for inline back ref. if back ref is found, *ref_ret is set
1433 * to the address of inline back ref, and 0 is returned.
1434 *
1435 * if back ref isn't found, *ref_ret is set to the address where it
1436 * should be inserted, and -ENOENT is returned.
1437 *
1438 * if insert is true and there are too many inline back refs, the path
1439 * points to the extent item, and -EAGAIN is returned.
1440 *
1441 * NOTE: inline back refs are ordered in the same way that back ref
1442 * items in the tree are ordered.
1443 */
1444static noinline_for_stack
1445int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1446 struct btrfs_root *root,
1447 struct btrfs_path *path,
1448 struct btrfs_extent_inline_ref **ref_ret,
1449 u64 bytenr, u64 num_bytes,
1450 u64 parent, u64 root_objectid,
1451 u64 owner, u64 offset, int insert)
1452{
1453 struct btrfs_key key;
1454 struct extent_buffer *leaf;
1455 struct btrfs_extent_item *ei;
1456 struct btrfs_extent_inline_ref *iref;
1457 u64 flags;
1458 u64 item_size;
1459 unsigned long ptr;
1460 unsigned long end;
1461 int extra_size;
1462 int type;
1463 int want;
1464 int ret;
1465 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001466 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1467 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001468
1469 key.objectid = bytenr;
1470 key.type = BTRFS_EXTENT_ITEM_KEY;
1471 key.offset = num_bytes;
1472
1473 want = extent_ref_type(parent, owner);
1474 if (insert) {
1475 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001476 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001477 } else
1478 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001479
1480 /*
1481 * Owner is our parent level, so we can just add one to get the level
1482 * for the block we are interested in.
1483 */
1484 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1485 key.type = BTRFS_METADATA_ITEM_KEY;
1486 key.offset = owner;
1487 }
1488
1489again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001490 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1491 if (ret < 0) {
1492 err = ret;
1493 goto out;
1494 }
Josef Bacik3173a182013-03-07 14:22:04 -05001495
1496 /*
1497 * We may be a newly converted file system which still has the old fat
1498 * extent entries for metadata, so try and see if we have one of those.
1499 */
1500 if (ret > 0 && skinny_metadata) {
1501 skinny_metadata = false;
1502 if (path->slots[0]) {
1503 path->slots[0]--;
1504 btrfs_item_key_to_cpu(path->nodes[0], &key,
1505 path->slots[0]);
1506 if (key.objectid == bytenr &&
1507 key.type == BTRFS_EXTENT_ITEM_KEY &&
1508 key.offset == num_bytes)
1509 ret = 0;
1510 }
1511 if (ret) {
1512 key.type = BTRFS_EXTENT_ITEM_KEY;
1513 key.offset = num_bytes;
1514 btrfs_release_path(path);
1515 goto again;
1516 }
1517 }
1518
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001519 if (ret && !insert) {
1520 err = -ENOENT;
1521 goto out;
Josef Bacik492104c2013-03-08 15:41:02 -05001522 } else if (ret) {
1523 err = -EIO;
1524 WARN_ON(1);
1525 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001526 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001527
1528 leaf = path->nodes[0];
1529 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1530#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1531 if (item_size < sizeof(*ei)) {
1532 if (!insert) {
1533 err = -ENOENT;
1534 goto out;
1535 }
1536 ret = convert_extent_item_v0(trans, root, path, owner,
1537 extra_size);
1538 if (ret < 0) {
1539 err = ret;
1540 goto out;
1541 }
1542 leaf = path->nodes[0];
1543 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1544 }
1545#endif
1546 BUG_ON(item_size < sizeof(*ei));
1547
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001548 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1549 flags = btrfs_extent_flags(leaf, ei);
1550
1551 ptr = (unsigned long)(ei + 1);
1552 end = (unsigned long)ei + item_size;
1553
Josef Bacik3173a182013-03-07 14:22:04 -05001554 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001555 ptr += sizeof(struct btrfs_tree_block_info);
1556 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001557 }
1558
1559 err = -ENOENT;
1560 while (1) {
1561 if (ptr >= end) {
1562 WARN_ON(ptr > end);
1563 break;
1564 }
1565 iref = (struct btrfs_extent_inline_ref *)ptr;
1566 type = btrfs_extent_inline_ref_type(leaf, iref);
1567 if (want < type)
1568 break;
1569 if (want > type) {
1570 ptr += btrfs_extent_inline_ref_size(type);
1571 continue;
1572 }
1573
1574 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1575 struct btrfs_extent_data_ref *dref;
1576 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1577 if (match_extent_data_ref(leaf, dref, root_objectid,
1578 owner, offset)) {
1579 err = 0;
1580 break;
1581 }
1582 if (hash_extent_data_ref_item(leaf, dref) <
1583 hash_extent_data_ref(root_objectid, owner, offset))
1584 break;
1585 } else {
1586 u64 ref_offset;
1587 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1588 if (parent > 0) {
1589 if (parent == ref_offset) {
1590 err = 0;
1591 break;
1592 }
1593 if (ref_offset < parent)
1594 break;
1595 } else {
1596 if (root_objectid == ref_offset) {
1597 err = 0;
1598 break;
1599 }
1600 if (ref_offset < root_objectid)
1601 break;
1602 }
1603 }
1604 ptr += btrfs_extent_inline_ref_size(type);
1605 }
1606 if (err == -ENOENT && insert) {
1607 if (item_size + extra_size >=
1608 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1609 err = -EAGAIN;
1610 goto out;
1611 }
1612 /*
1613 * To add new inline back ref, we have to make sure
1614 * there is no corresponding back ref item.
1615 * For simplicity, we just do not add new inline back
1616 * ref if there is any kind of item for this block
1617 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001618 if (find_next_key(path, 0, &key) == 0 &&
1619 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001620 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001621 err = -EAGAIN;
1622 goto out;
1623 }
1624 }
1625 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1626out:
Yan Zheng85d41982009-06-11 08:51:10 -04001627 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001628 path->keep_locks = 0;
1629 btrfs_unlock_up_safe(path, 1);
1630 }
1631 return err;
1632}
1633
1634/*
1635 * helper to add new inline back ref
1636 */
1637static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001638void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001639 struct btrfs_path *path,
1640 struct btrfs_extent_inline_ref *iref,
1641 u64 parent, u64 root_objectid,
1642 u64 owner, u64 offset, int refs_to_add,
1643 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644{
1645 struct extent_buffer *leaf;
1646 struct btrfs_extent_item *ei;
1647 unsigned long ptr;
1648 unsigned long end;
1649 unsigned long item_offset;
1650 u64 refs;
1651 int size;
1652 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001653
1654 leaf = path->nodes[0];
1655 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1656 item_offset = (unsigned long)iref - (unsigned long)ei;
1657
1658 type = extent_ref_type(parent, owner);
1659 size = btrfs_extent_inline_ref_size(type);
1660
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001661 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662
1663 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1664 refs = btrfs_extent_refs(leaf, ei);
1665 refs += refs_to_add;
1666 btrfs_set_extent_refs(leaf, ei, refs);
1667 if (extent_op)
1668 __run_delayed_extent_op(extent_op, leaf, ei);
1669
1670 ptr = (unsigned long)ei + item_offset;
1671 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1672 if (ptr < end - size)
1673 memmove_extent_buffer(leaf, ptr + size, ptr,
1674 end - size - ptr);
1675
1676 iref = (struct btrfs_extent_inline_ref *)ptr;
1677 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1678 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1679 struct btrfs_extent_data_ref *dref;
1680 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1681 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1682 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1683 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1684 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1685 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1686 struct btrfs_shared_data_ref *sref;
1687 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1688 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1689 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1690 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1691 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1692 } else {
1693 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1694 }
1695 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001696}
1697
1698static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1699 struct btrfs_root *root,
1700 struct btrfs_path *path,
1701 struct btrfs_extent_inline_ref **ref_ret,
1702 u64 bytenr, u64 num_bytes, u64 parent,
1703 u64 root_objectid, u64 owner, u64 offset)
1704{
1705 int ret;
1706
1707 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1708 bytenr, num_bytes, parent,
1709 root_objectid, owner, offset, 0);
1710 if (ret != -ENOENT)
1711 return ret;
1712
David Sterbab3b4aa72011-04-21 01:20:15 +02001713 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001714 *ref_ret = NULL;
1715
1716 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1717 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1718 root_objectid);
1719 } else {
1720 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1721 root_objectid, owner, offset);
1722 }
1723 return ret;
1724}
1725
1726/*
1727 * helper to update/remove inline back ref
1728 */
1729static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001730void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001731 struct btrfs_path *path,
1732 struct btrfs_extent_inline_ref *iref,
1733 int refs_to_mod,
1734 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735{
1736 struct extent_buffer *leaf;
1737 struct btrfs_extent_item *ei;
1738 struct btrfs_extent_data_ref *dref = NULL;
1739 struct btrfs_shared_data_ref *sref = NULL;
1740 unsigned long ptr;
1741 unsigned long end;
1742 u32 item_size;
1743 int size;
1744 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001745 u64 refs;
1746
1747 leaf = path->nodes[0];
1748 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1749 refs = btrfs_extent_refs(leaf, ei);
1750 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1751 refs += refs_to_mod;
1752 btrfs_set_extent_refs(leaf, ei, refs);
1753 if (extent_op)
1754 __run_delayed_extent_op(extent_op, leaf, ei);
1755
1756 type = btrfs_extent_inline_ref_type(leaf, iref);
1757
1758 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1759 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1760 refs = btrfs_extent_data_ref_count(leaf, dref);
1761 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1762 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1763 refs = btrfs_shared_data_ref_count(leaf, sref);
1764 } else {
1765 refs = 1;
1766 BUG_ON(refs_to_mod != -1);
1767 }
1768
1769 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1770 refs += refs_to_mod;
1771
1772 if (refs > 0) {
1773 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1774 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1775 else
1776 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1777 } else {
1778 size = btrfs_extent_inline_ref_size(type);
1779 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1780 ptr = (unsigned long)iref;
1781 end = (unsigned long)ei + item_size;
1782 if (ptr + size < end)
1783 memmove_extent_buffer(leaf, ptr, ptr + size,
1784 end - ptr - size);
1785 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001786 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001787 }
1788 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001789}
1790
1791static noinline_for_stack
1792int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1793 struct btrfs_root *root,
1794 struct btrfs_path *path,
1795 u64 bytenr, u64 num_bytes, u64 parent,
1796 u64 root_objectid, u64 owner,
1797 u64 offset, int refs_to_add,
1798 struct btrfs_delayed_extent_op *extent_op)
1799{
1800 struct btrfs_extent_inline_ref *iref;
1801 int ret;
1802
1803 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1804 bytenr, num_bytes, parent,
1805 root_objectid, owner, offset, 1);
1806 if (ret == 0) {
1807 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001808 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001809 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001810 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001811 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001812 root_objectid, owner, offset,
1813 refs_to_add, extent_op);
1814 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001815 }
1816 return ret;
1817}
1818
1819static int insert_extent_backref(struct btrfs_trans_handle *trans,
1820 struct btrfs_root *root,
1821 struct btrfs_path *path,
1822 u64 bytenr, u64 parent, u64 root_objectid,
1823 u64 owner, u64 offset, int refs_to_add)
1824{
1825 int ret;
1826 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1827 BUG_ON(refs_to_add != 1);
1828 ret = insert_tree_block_ref(trans, root, path, bytenr,
1829 parent, root_objectid);
1830 } else {
1831 ret = insert_extent_data_ref(trans, root, path, bytenr,
1832 parent, root_objectid,
1833 owner, offset, refs_to_add);
1834 }
1835 return ret;
1836}
1837
1838static int remove_extent_backref(struct btrfs_trans_handle *trans,
1839 struct btrfs_root *root,
1840 struct btrfs_path *path,
1841 struct btrfs_extent_inline_ref *iref,
1842 int refs_to_drop, int is_data)
1843{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001844 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001845
1846 BUG_ON(!is_data && refs_to_drop != 1);
1847 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001848 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001849 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001850 } else if (is_data) {
1851 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1852 } else {
1853 ret = btrfs_del_item(trans, root, path);
1854 }
1855 return ret;
1856}
1857
Li Dongyang5378e602011-03-24 10:24:27 +00001858static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001859 u64 start, u64 len)
1860{
Li Dongyang5378e602011-03-24 10:24:27 +00001861 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001862}
Chris Mason15916de2008-11-19 21:17:22 -05001863
Liu Hui1f3c79a2009-01-05 15:57:51 -05001864static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001865 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001866{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001867 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001868 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001869 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001870
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001871
Liu Hui1f3c79a2009-01-05 15:57:51 -05001872 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001873 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001874 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001875 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001876 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001877 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001878 int i;
1879
Liu Hui1f3c79a2009-01-05 15:57:51 -05001880
Jan Schmidta1d3c472011-08-04 17:15:33 +02001881 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001882 if (!stripe->dev->can_discard)
1883 continue;
1884
Li Dongyang5378e602011-03-24 10:24:27 +00001885 ret = btrfs_issue_discard(stripe->dev->bdev,
1886 stripe->physical,
1887 stripe->length);
1888 if (!ret)
1889 discarded_bytes += stripe->length;
1890 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001891 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001892
1893 /*
1894 * Just in case we get back EOPNOTSUPP for some reason,
1895 * just ignore the return value so we don't screw up
1896 * people calling discard_extent.
1897 */
1898 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001899 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001900 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001901 }
Li Dongyang5378e602011-03-24 10:24:27 +00001902
1903 if (actual_bytes)
1904 *actual_bytes = discarded_bytes;
1905
Liu Hui1f3c79a2009-01-05 15:57:51 -05001906
David Woodhouse53b381b2013-01-29 18:40:14 -05001907 if (ret == -EOPNOTSUPP)
1908 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001909 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001910}
1911
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001912/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001913int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1914 struct btrfs_root *root,
1915 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001916 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001917{
1918 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001919 struct btrfs_fs_info *fs_info = root->fs_info;
1920
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001921 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1922 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001923
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001924 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001925 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1926 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001927 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001928 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001929 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001930 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1931 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001932 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001933 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001934 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001935 return ret;
1936}
1937
Chris Mason925baed2008-06-25 16:01:30 -04001938static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001939 struct btrfs_root *root,
1940 u64 bytenr, u64 num_bytes,
1941 u64 parent, u64 root_objectid,
1942 u64 owner, u64 offset, int refs_to_add,
1943 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001944{
Chris Mason5caf2a02007-04-02 11:20:42 -04001945 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001947 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001948 u64 refs;
1949 int ret;
1950 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001951
Chris Mason5caf2a02007-04-02 11:20:42 -04001952 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001953 if (!path)
1954 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001955
Chris Mason3c12ac72008-04-21 12:01:38 -04001956 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001957 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001958 /* this will setup the path even if it fails to insert the back ref */
1959 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1960 path, bytenr, num_bytes, parent,
1961 root_objectid, owner, offset,
1962 refs_to_add, extent_op);
1963 if (ret == 0)
1964 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001966 if (ret != -EAGAIN) {
1967 err = ret;
1968 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001969 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001970
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001971 leaf = path->nodes[0];
1972 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1973 refs = btrfs_extent_refs(leaf, item);
1974 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1975 if (extent_op)
1976 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001977
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001978 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001979 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001980
Chris Mason3c12ac72008-04-21 12:01:38 -04001981 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001982 path->leave_spinning = 1;
1983
Chris Mason56bec292009-03-13 10:10:06 -04001984 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001985 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001986 path, bytenr, parent, root_objectid,
1987 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001988 if (ret)
1989 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001990out:
Chris Mason74493f72007-12-11 09:25:06 -05001991 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001992 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05001993}
1994
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001995static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1996 struct btrfs_root *root,
1997 struct btrfs_delayed_ref_node *node,
1998 struct btrfs_delayed_extent_op *extent_op,
1999 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002000{
Chris Mason56bec292009-03-13 10:10:06 -04002001 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002002 struct btrfs_delayed_data_ref *ref;
2003 struct btrfs_key ins;
2004 u64 parent = 0;
2005 u64 ref_root = 0;
2006 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002007
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008 ins.objectid = node->bytenr;
2009 ins.offset = node->num_bytes;
2010 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002012 ref = btrfs_delayed_node_to_data_ref(node);
2013 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2014 parent = ref->parent;
2015 else
2016 ref_root = ref->root;
2017
2018 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002019 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002020 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021 ret = alloc_reserved_file_extent(trans, root,
2022 parent, ref_root, flags,
2023 ref->objectid, ref->offset,
2024 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002025 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2026 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2027 node->num_bytes, parent,
2028 ref_root, ref->objectid,
2029 ref->offset, node->ref_mod,
2030 extent_op);
2031 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2032 ret = __btrfs_free_extent(trans, root, node->bytenr,
2033 node->num_bytes, parent,
2034 ref_root, ref->objectid,
2035 ref->offset, node->ref_mod,
2036 extent_op);
2037 } else {
2038 BUG();
2039 }
Chris Mason56bec292009-03-13 10:10:06 -04002040 return ret;
2041}
2042
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002043static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2044 struct extent_buffer *leaf,
2045 struct btrfs_extent_item *ei)
2046{
2047 u64 flags = btrfs_extent_flags(leaf, ei);
2048 if (extent_op->update_flags) {
2049 flags |= extent_op->flags_to_set;
2050 btrfs_set_extent_flags(leaf, ei, flags);
2051 }
2052
2053 if (extent_op->update_key) {
2054 struct btrfs_tree_block_info *bi;
2055 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2056 bi = (struct btrfs_tree_block_info *)(ei + 1);
2057 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2058 }
2059}
2060
2061static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2062 struct btrfs_root *root,
2063 struct btrfs_delayed_ref_node *node,
2064 struct btrfs_delayed_extent_op *extent_op)
2065{
2066 struct btrfs_key key;
2067 struct btrfs_path *path;
2068 struct btrfs_extent_item *ei;
2069 struct extent_buffer *leaf;
2070 u32 item_size;
2071 int ret;
2072 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002073 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002074
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002075 if (trans->aborted)
2076 return 0;
2077
Josef Bacik3173a182013-03-07 14:22:04 -05002078 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2079 metadata = 0;
2080
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002081 path = btrfs_alloc_path();
2082 if (!path)
2083 return -ENOMEM;
2084
2085 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086
Josef Bacik3173a182013-03-07 14:22:04 -05002087 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002088 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002089 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002090 } else {
2091 key.type = BTRFS_EXTENT_ITEM_KEY;
2092 key.offset = node->num_bytes;
2093 }
2094
2095again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096 path->reada = 1;
2097 path->leave_spinning = 1;
2098 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2099 path, 0, 1);
2100 if (ret < 0) {
2101 err = ret;
2102 goto out;
2103 }
2104 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002105 if (metadata) {
2106 btrfs_release_path(path);
2107 metadata = 0;
2108
2109 key.offset = node->num_bytes;
2110 key.type = BTRFS_EXTENT_ITEM_KEY;
2111 goto again;
2112 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 err = -EIO;
2114 goto out;
2115 }
2116
2117 leaf = path->nodes[0];
2118 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2119#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2120 if (item_size < sizeof(*ei)) {
2121 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2122 path, (u64)-1, 0);
2123 if (ret < 0) {
2124 err = ret;
2125 goto out;
2126 }
2127 leaf = path->nodes[0];
2128 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2129 }
2130#endif
2131 BUG_ON(item_size < sizeof(*ei));
2132 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2133 __run_delayed_extent_op(extent_op, leaf, ei);
2134
2135 btrfs_mark_buffer_dirty(leaf);
2136out:
2137 btrfs_free_path(path);
2138 return err;
2139}
2140
2141static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2142 struct btrfs_root *root,
2143 struct btrfs_delayed_ref_node *node,
2144 struct btrfs_delayed_extent_op *extent_op,
2145 int insert_reserved)
2146{
2147 int ret = 0;
2148 struct btrfs_delayed_tree_ref *ref;
2149 struct btrfs_key ins;
2150 u64 parent = 0;
2151 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002152 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2153 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002154
2155 ref = btrfs_delayed_node_to_tree_ref(node);
2156 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2157 parent = ref->parent;
2158 else
2159 ref_root = ref->root;
2160
Josef Bacik3173a182013-03-07 14:22:04 -05002161 ins.objectid = node->bytenr;
2162 if (skinny_metadata) {
2163 ins.offset = ref->level;
2164 ins.type = BTRFS_METADATA_ITEM_KEY;
2165 } else {
2166 ins.offset = node->num_bytes;
2167 ins.type = BTRFS_EXTENT_ITEM_KEY;
2168 }
2169
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002170 BUG_ON(node->ref_mod != 1);
2171 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002172 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002173 ret = alloc_reserved_tree_block(trans, root,
2174 parent, ref_root,
2175 extent_op->flags_to_set,
2176 &extent_op->key,
2177 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002178 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2179 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2180 node->num_bytes, parent, ref_root,
2181 ref->level, 0, 1, extent_op);
2182 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2183 ret = __btrfs_free_extent(trans, root, node->bytenr,
2184 node->num_bytes, parent, ref_root,
2185 ref->level, 0, 1, extent_op);
2186 } else {
2187 BUG();
2188 }
2189 return ret;
2190}
2191
Chris Mason56bec292009-03-13 10:10:06 -04002192/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002193static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2194 struct btrfs_root *root,
2195 struct btrfs_delayed_ref_node *node,
2196 struct btrfs_delayed_extent_op *extent_op,
2197 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002198{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002199 int ret = 0;
2200
2201 if (trans->aborted)
2202 return 0;
2203
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002205 struct btrfs_delayed_ref_head *head;
2206 /*
2207 * we've hit the end of the chain and we were supposed
2208 * to insert this extent into the tree. But, it got
2209 * deleted before we ever needed to insert it, so all
2210 * we have to do is clean up the accounting
2211 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002212 BUG_ON(extent_op);
2213 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002214 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002215 btrfs_pin_extent(root, node->bytenr,
2216 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217 if (head->is_data) {
2218 ret = btrfs_del_csums(trans, root,
2219 node->bytenr,
2220 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221 }
Chris Mason56bec292009-03-13 10:10:06 -04002222 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002223 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002224 }
Josef Bacikeb099672009-02-12 09:27:38 -05002225
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002226 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2227 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2228 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2229 insert_reserved);
2230 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2231 node->type == BTRFS_SHARED_DATA_REF_KEY)
2232 ret = run_delayed_data_ref(trans, root, node, extent_op,
2233 insert_reserved);
2234 else
2235 BUG();
2236 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002237}
2238
Chris Mason56bec292009-03-13 10:10:06 -04002239static noinline struct btrfs_delayed_ref_node *
2240select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002241{
Chris Mason56bec292009-03-13 10:10:06 -04002242 struct rb_node *node;
2243 struct btrfs_delayed_ref_node *ref;
2244 int action = BTRFS_ADD_DELAYED_REF;
2245again:
2246 /*
2247 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2248 * this prevents ref count from going down to zero when
2249 * there still are pending delayed ref.
2250 */
2251 node = rb_prev(&head->node.rb_node);
2252 while (1) {
2253 if (!node)
2254 break;
2255 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2256 rb_node);
2257 if (ref->bytenr != head->node.bytenr)
2258 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002259 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002260 return ref;
2261 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002262 }
Chris Mason56bec292009-03-13 10:10:06 -04002263 if (action == BTRFS_ADD_DELAYED_REF) {
2264 action = BTRFS_DROP_DELAYED_REF;
2265 goto again;
2266 }
2267 return NULL;
2268}
2269
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002270/*
2271 * Returns 0 on success or if called with an already aborted transaction.
2272 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2273 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002274static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2275 struct btrfs_root *root,
2276 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002277{
Chris Mason56bec292009-03-13 10:10:06 -04002278 struct btrfs_delayed_ref_root *delayed_refs;
2279 struct btrfs_delayed_ref_node *ref;
2280 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002282 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002283 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002284 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002285 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002286
2287 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002288 while (1) {
2289 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002290 /* pick a new head ref from the cluster list */
2291 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002292 break;
Chris Mason56bec292009-03-13 10:10:06 -04002293
Chris Masonc3e69d52009-03-13 10:17:05 -04002294 locked_ref = list_entry(cluster->next,
2295 struct btrfs_delayed_ref_head, cluster);
2296
2297 /* grab the lock that says we are going to process
2298 * all the refs for this head */
2299 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2300
2301 /*
2302 * we may have dropped the spin lock to get the head
2303 * mutex lock, and that might have given someone else
2304 * time to free the head. If that's true, it has been
2305 * removed from our list and we can move on.
2306 */
2307 if (ret == -EAGAIN) {
2308 locked_ref = NULL;
2309 count++;
2310 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002311 }
2312 }
2313
2314 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002315 * We need to try and merge add/drops of the same ref since we
2316 * can run into issues with relocate dropping the implicit ref
2317 * and then it being added back again before the drop can
2318 * finish. If we merged anything we need to re-loop so we can
2319 * get a good ref.
2320 */
2321 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2322 locked_ref);
2323
2324 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002325 * locked_ref is the head node, so we have to go one
2326 * node back for any delayed ref updates
2327 */
2328 ref = select_delayed_ref(locked_ref);
2329
2330 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002331 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002332 /*
2333 * there are still refs with lower seq numbers in the
2334 * process of being added. Don't run this ref yet.
2335 */
2336 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002337 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002338 locked_ref = NULL;
2339 delayed_refs->num_heads_ready++;
2340 spin_unlock(&delayed_refs->lock);
2341 cond_resched();
2342 spin_lock(&delayed_refs->lock);
2343 continue;
2344 }
2345
2346 /*
Chris Mason56bec292009-03-13 10:10:06 -04002347 * record the must insert reserved flag before we
2348 * drop the spin lock.
2349 */
2350 must_insert_reserved = locked_ref->must_insert_reserved;
2351 locked_ref->must_insert_reserved = 0;
2352
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002353 extent_op = locked_ref->extent_op;
2354 locked_ref->extent_op = NULL;
2355
Chris Mason56bec292009-03-13 10:10:06 -04002356 if (!ref) {
2357 /* All delayed refs have been processed, Go ahead
2358 * and send the head node to run_one_delayed_ref,
2359 * so that any accounting fixes can happen
2360 */
2361 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002362
2363 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002364 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365 extent_op = NULL;
2366 }
2367
2368 if (extent_op) {
2369 spin_unlock(&delayed_refs->lock);
2370
2371 ret = run_delayed_extent_op(trans, root,
2372 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002373 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002375 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002376 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002377 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002378 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002379 return ret;
2380 }
2381
Chris Mason203bf282012-01-06 15:23:57 -05002382 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002383 }
Chris Mason56bec292009-03-13 10:10:06 -04002384 }
2385
2386 ref->in_tree = 0;
2387 rb_erase(&ref->rb_node, &delayed_refs->root);
2388 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002389 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002390 /*
2391 * when we play the delayed ref, also correct the
2392 * ref_mod on head
2393 */
2394 switch (ref->action) {
2395 case BTRFS_ADD_DELAYED_REF:
2396 case BTRFS_ADD_DELAYED_EXTENT:
2397 locked_ref->node.ref_mod -= ref->ref_mod;
2398 break;
2399 case BTRFS_DROP_DELAYED_REF:
2400 locked_ref->node.ref_mod += ref->ref_mod;
2401 break;
2402 default:
2403 WARN_ON(1);
2404 }
2405 }
Chris Mason56bec292009-03-13 10:10:06 -04002406 spin_unlock(&delayed_refs->lock);
2407
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002408 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002409 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002410
Miao Xie78a61842012-11-21 02:21:28 +00002411 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002412 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002413 btrfs_delayed_ref_unlock(locked_ref);
2414 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002415 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002416 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002417 return ret;
2418 }
2419
Miao Xie093486c2012-12-19 08:10:10 +00002420 /*
2421 * If this node is a head, that means all the refs in this head
2422 * have been dealt with, and we will pick the next head to deal
2423 * with, so we must unlock the head and drop it from the cluster
2424 * list before we release it.
2425 */
2426 if (btrfs_delayed_ref_is_head(ref)) {
2427 list_del_init(&locked_ref->cluster);
2428 btrfs_delayed_ref_unlock(locked_ref);
2429 locked_ref = NULL;
2430 }
2431 btrfs_put_delayed_ref(ref);
2432 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002433next:
Chris Mason1887be62009-03-13 10:11:24 -04002434 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002435 spin_lock(&delayed_refs->lock);
2436 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002437 return count;
2438}
2439
Arne Jansen709c0482011-09-12 12:22:57 +02002440#ifdef SCRAMBLE_DELAYED_REFS
2441/*
2442 * Normally delayed refs get processed in ascending bytenr order. This
2443 * correlates in most cases to the order added. To expose dependencies on this
2444 * order, we start to process the tree in the middle instead of the beginning
2445 */
2446static u64 find_middle(struct rb_root *root)
2447{
2448 struct rb_node *n = root->rb_node;
2449 struct btrfs_delayed_ref_node *entry;
2450 int alt = 1;
2451 u64 middle;
2452 u64 first = 0, last = 0;
2453
2454 n = rb_first(root);
2455 if (n) {
2456 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2457 first = entry->bytenr;
2458 }
2459 n = rb_last(root);
2460 if (n) {
2461 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2462 last = entry->bytenr;
2463 }
2464 n = root->rb_node;
2465
2466 while (n) {
2467 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2468 WARN_ON(!entry->in_tree);
2469
2470 middle = entry->bytenr;
2471
2472 if (alt)
2473 n = n->rb_left;
2474 else
2475 n = n->rb_right;
2476
2477 alt = 1 - alt;
2478 }
2479 return middle;
2480}
2481#endif
2482
Arne Jansenbed92ea2012-06-28 18:03:02 +02002483int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2484 struct btrfs_fs_info *fs_info)
2485{
2486 struct qgroup_update *qgroup_update;
2487 int ret = 0;
2488
2489 if (list_empty(&trans->qgroup_ref_list) !=
2490 !trans->delayed_ref_elem.seq) {
2491 /* list without seq or seq without list */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002492 btrfs_err(fs_info,
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002493 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002494 list_empty(&trans->qgroup_ref_list) ? "" : " not",
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002495 (u32)(trans->delayed_ref_elem.seq >> 32),
2496 (u32)trans->delayed_ref_elem.seq);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002497 BUG();
2498 }
2499
2500 if (!trans->delayed_ref_elem.seq)
2501 return 0;
2502
2503 while (!list_empty(&trans->qgroup_ref_list)) {
2504 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2505 struct qgroup_update, list);
2506 list_del(&qgroup_update->list);
2507 if (!ret)
2508 ret = btrfs_qgroup_account_ref(
2509 trans, fs_info, qgroup_update->node,
2510 qgroup_update->extent_op);
2511 kfree(qgroup_update);
2512 }
2513
2514 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2515
2516 return ret;
2517}
2518
Chris Masonbb721702013-01-29 18:44:12 -05002519static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2520 int count)
2521{
2522 int val = atomic_read(&delayed_refs->ref_seq);
2523
2524 if (val < seq || val >= seq + count)
2525 return 1;
2526 return 0;
2527}
2528
Chris Masonc3e69d52009-03-13 10:17:05 -04002529/*
2530 * this starts processing the delayed reference count updates and
2531 * extent insertions we have queued up so far. count can be
2532 * 0, which means to process everything in the tree at the start
2533 * of the run (but not newly added entries), or it can be some target
2534 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002535 *
2536 * Returns 0 on success or if called with an aborted transaction
2537 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002538 */
2539int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2540 struct btrfs_root *root, unsigned long count)
2541{
2542 struct rb_node *node;
2543 struct btrfs_delayed_ref_root *delayed_refs;
2544 struct btrfs_delayed_ref_node *ref;
2545 struct list_head cluster;
2546 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002547 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002548 int run_all = count == (unsigned long)-1;
2549 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002550 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002551
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002552 /* We'll clean this up in btrfs_cleanup_transaction */
2553 if (trans->aborted)
2554 return 0;
2555
Chris Masonc3e69d52009-03-13 10:17:05 -04002556 if (root == root->fs_info->extent_root)
2557 root = root->fs_info->tree_root;
2558
Jan Schmidtedf39272012-06-28 18:04:55 +02002559 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2560
Chris Masonc3e69d52009-03-13 10:17:05 -04002561 delayed_refs = &trans->transaction->delayed_refs;
2562 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002563 if (count == 0) {
2564 count = delayed_refs->num_entries * 2;
2565 run_most = 1;
2566 }
2567
2568 if (!run_all && !run_most) {
2569 int old;
2570 int seq = atomic_read(&delayed_refs->ref_seq);
2571
2572progress:
2573 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2574 if (old) {
2575 DEFINE_WAIT(__wait);
2576 if (delayed_refs->num_entries < 16348)
2577 return 0;
2578
2579 prepare_to_wait(&delayed_refs->wait, &__wait,
2580 TASK_UNINTERRUPTIBLE);
2581
2582 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2583 if (old) {
2584 schedule();
2585 finish_wait(&delayed_refs->wait, &__wait);
2586
2587 if (!refs_newer(delayed_refs, seq, 256))
2588 goto progress;
2589 else
2590 return 0;
2591 } else {
2592 finish_wait(&delayed_refs->wait, &__wait);
2593 goto again;
2594 }
2595 }
2596
2597 } else {
2598 atomic_inc(&delayed_refs->procs_running_refs);
2599 }
2600
Chris Masonc3e69d52009-03-13 10:17:05 -04002601again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002602 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002603 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002604
Arne Jansen709c0482011-09-12 12:22:57 +02002605#ifdef SCRAMBLE_DELAYED_REFS
2606 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2607#endif
2608
Chris Masonc3e69d52009-03-13 10:17:05 -04002609 while (1) {
2610 if (!(run_all || run_most) &&
2611 delayed_refs->num_heads_ready < 64)
2612 break;
2613
2614 /*
2615 * go find something we can process in the rbtree. We start at
2616 * the beginning of the tree, and then build a cluster
2617 * of refs to process starting at the first one we are able to
2618 * lock
2619 */
Jan Schmidta1686502011-12-12 16:10:07 +01002620 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002621 ret = btrfs_find_ref_cluster(trans, &cluster,
2622 delayed_refs->run_delayed_start);
2623 if (ret)
2624 break;
2625
2626 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002627 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002628 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002629 spin_unlock(&delayed_refs->lock);
2630 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002631 atomic_dec(&delayed_refs->procs_running_refs);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002632 return ret;
2633 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002634
Chris Masonbb721702013-01-29 18:44:12 -05002635 atomic_add(ret, &delayed_refs->ref_seq);
2636
Chris Masonc3e69d52009-03-13 10:17:05 -04002637 count -= min_t(unsigned long, ret, count);
2638
2639 if (count == 0)
2640 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002641
Arne Jansen1fa11e22012-08-06 14:18:51 -06002642 if (delayed_start >= delayed_refs->run_delayed_start) {
2643 if (loops == 0) {
2644 /*
2645 * btrfs_find_ref_cluster looped. let's do one
2646 * more cycle. if we don't run any delayed ref
2647 * during that cycle (because we can't because
2648 * all of them are blocked), bail out.
2649 */
2650 loops = 1;
2651 } else {
2652 /*
2653 * no runnable refs left, stop trying
2654 */
2655 BUG_ON(run_all);
2656 break;
2657 }
2658 }
2659 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002660 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002661 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002662 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002663 }
2664
Chris Mason56bec292009-03-13 10:10:06 -04002665 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002666 if (!list_empty(&trans->new_bgs)) {
2667 spin_unlock(&delayed_refs->lock);
2668 btrfs_create_pending_block_groups(trans, root);
2669 spin_lock(&delayed_refs->lock);
2670 }
2671
Chris Mason56bec292009-03-13 10:10:06 -04002672 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002673 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002674 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002675 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002676
2677 while (node) {
2678 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2679 rb_node);
2680 if (btrfs_delayed_ref_is_head(ref)) {
2681 struct btrfs_delayed_ref_head *head;
2682
2683 head = btrfs_delayed_node_to_head(ref);
2684 atomic_inc(&ref->refs);
2685
2686 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002687 /*
2688 * Mutex was contended, block until it's
2689 * released and try again
2690 */
Chris Mason56bec292009-03-13 10:10:06 -04002691 mutex_lock(&head->mutex);
2692 mutex_unlock(&head->mutex);
2693
2694 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002695 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002696 goto again;
2697 }
2698 node = rb_next(node);
2699 }
2700 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002701 schedule_timeout(1);
2702 goto again;
2703 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002704out:
Chris Masonbb721702013-01-29 18:44:12 -05002705 atomic_dec(&delayed_refs->procs_running_refs);
2706 smp_mb();
2707 if (waitqueue_active(&delayed_refs->wait))
2708 wake_up(&delayed_refs->wait);
2709
Chris Masonc3e69d52009-03-13 10:17:05 -04002710 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002711 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002712 return 0;
2713}
2714
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002715int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2716 struct btrfs_root *root,
2717 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002718 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002719{
2720 struct btrfs_delayed_extent_op *extent_op;
2721 int ret;
2722
Miao Xie78a61842012-11-21 02:21:28 +00002723 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002724 if (!extent_op)
2725 return -ENOMEM;
2726
2727 extent_op->flags_to_set = flags;
2728 extent_op->update_flags = 1;
2729 extent_op->update_key = 0;
2730 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002731 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002732
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002733 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2734 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002735 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002736 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002737 return ret;
2738}
2739
2740static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2741 struct btrfs_root *root,
2742 struct btrfs_path *path,
2743 u64 objectid, u64 offset, u64 bytenr)
2744{
2745 struct btrfs_delayed_ref_head *head;
2746 struct btrfs_delayed_ref_node *ref;
2747 struct btrfs_delayed_data_ref *data_ref;
2748 struct btrfs_delayed_ref_root *delayed_refs;
2749 struct rb_node *node;
2750 int ret = 0;
2751
2752 ret = -ENOENT;
2753 delayed_refs = &trans->transaction->delayed_refs;
2754 spin_lock(&delayed_refs->lock);
2755 head = btrfs_find_delayed_ref_head(trans, bytenr);
2756 if (!head)
2757 goto out;
2758
2759 if (!mutex_trylock(&head->mutex)) {
2760 atomic_inc(&head->node.refs);
2761 spin_unlock(&delayed_refs->lock);
2762
David Sterbab3b4aa72011-04-21 01:20:15 +02002763 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002764
David Sterba8cc33e52011-05-02 15:29:25 +02002765 /*
2766 * Mutex was contended, block until it's released and let
2767 * caller try again
2768 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002769 mutex_lock(&head->mutex);
2770 mutex_unlock(&head->mutex);
2771 btrfs_put_delayed_ref(&head->node);
2772 return -EAGAIN;
2773 }
2774
2775 node = rb_prev(&head->node.rb_node);
2776 if (!node)
2777 goto out_unlock;
2778
2779 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2780
2781 if (ref->bytenr != bytenr)
2782 goto out_unlock;
2783
2784 ret = 1;
2785 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2786 goto out_unlock;
2787
2788 data_ref = btrfs_delayed_node_to_data_ref(ref);
2789
2790 node = rb_prev(node);
2791 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002792 int seq = ref->seq;
2793
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002794 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002795 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002796 goto out_unlock;
2797 }
2798
2799 if (data_ref->root != root->root_key.objectid ||
2800 data_ref->objectid != objectid || data_ref->offset != offset)
2801 goto out_unlock;
2802
2803 ret = 0;
2804out_unlock:
2805 mutex_unlock(&head->mutex);
2806out:
2807 spin_unlock(&delayed_refs->lock);
2808 return ret;
2809}
2810
2811static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2812 struct btrfs_root *root,
2813 struct btrfs_path *path,
2814 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002815{
2816 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002817 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002818 struct btrfs_extent_data_ref *ref;
2819 struct btrfs_extent_inline_ref *iref;
2820 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002821 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002822 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002823 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002824
Chris Masonbe20aa92007-12-17 20:14:01 -05002825 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002826 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002827 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002828
Chris Masonbe20aa92007-12-17 20:14:01 -05002829 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2830 if (ret < 0)
2831 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002832 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002833
2834 ret = -ENOENT;
2835 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002836 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002837
Zheng Yan31840ae2008-09-23 13:14:14 -04002838 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002839 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002840 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002841
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002842 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002843 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002844
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002845 ret = 1;
2846 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2847#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2848 if (item_size < sizeof(*ei)) {
2849 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2850 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002851 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002852#endif
2853 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2854
2855 if (item_size != sizeof(*ei) +
2856 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2857 goto out;
2858
2859 if (btrfs_extent_generation(leaf, ei) <=
2860 btrfs_root_last_snapshot(&root->root_item))
2861 goto out;
2862
2863 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2864 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2865 BTRFS_EXTENT_DATA_REF_KEY)
2866 goto out;
2867
2868 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2869 if (btrfs_extent_refs(leaf, ei) !=
2870 btrfs_extent_data_ref_count(leaf, ref) ||
2871 btrfs_extent_data_ref_root(leaf, ref) !=
2872 root->root_key.objectid ||
2873 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2874 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2875 goto out;
2876
Yan Zhengf321e492008-07-30 09:26:11 -04002877 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002878out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002879 return ret;
2880}
2881
2882int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2883 struct btrfs_root *root,
2884 u64 objectid, u64 offset, u64 bytenr)
2885{
2886 struct btrfs_path *path;
2887 int ret;
2888 int ret2;
2889
2890 path = btrfs_alloc_path();
2891 if (!path)
2892 return -ENOENT;
2893
2894 do {
2895 ret = check_committed_ref(trans, root, path, objectid,
2896 offset, bytenr);
2897 if (ret && ret != -ENOENT)
2898 goto out;
2899
2900 ret2 = check_delayed_ref(trans, root, path, objectid,
2901 offset, bytenr);
2902 } while (ret2 == -EAGAIN);
2903
2904 if (ret2 && ret2 != -ENOENT) {
2905 ret = ret2;
2906 goto out;
2907 }
2908
2909 if (ret != -ENOENT || ret2 != -ENOENT)
2910 ret = 0;
2911out:
Yan Zhengf321e492008-07-30 09:26:11 -04002912 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002913 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2914 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002915 return ret;
2916}
2917
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002918static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002919 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002920 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002921 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002922{
2923 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002924 u64 num_bytes;
2925 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002926 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002927 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002928 struct btrfs_key key;
2929 struct btrfs_file_extent_item *fi;
2930 int i;
2931 int level;
2932 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002933 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002934 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002935
2936 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002937 nritems = btrfs_header_nritems(buf);
2938 level = btrfs_header_level(buf);
2939
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002940 if (!root->ref_cows && level == 0)
2941 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002942
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002943 if (inc)
2944 process_func = btrfs_inc_extent_ref;
2945 else
2946 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002947
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002948 if (full_backref)
2949 parent = buf->start;
2950 else
2951 parent = 0;
2952
Zheng Yan31840ae2008-09-23 13:14:14 -04002953 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002954 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002955 btrfs_item_key_to_cpu(buf, &key, i);
2956 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002957 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002958 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002959 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002960 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002961 BTRFS_FILE_EXTENT_INLINE)
2962 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002963 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2964 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002965 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002966
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002967 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2968 key.offset -= btrfs_file_extent_offset(buf, fi);
2969 ret = process_func(trans, root, bytenr, num_bytes,
2970 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002971 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002972 if (ret)
2973 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002974 } else {
2975 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002976 num_bytes = btrfs_level_size(root, level - 1);
2977 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002978 parent, ref_root, level - 1, 0,
2979 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002980 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04002981 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002982 }
2983 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002984 return 0;
2985fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04002986 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002987}
2988
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002989int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002990 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002991{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002992 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002993}
Zheng Yan31840ae2008-09-23 13:14:14 -04002994
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002995int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002996 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002997{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002998 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04002999}
3000
Chris Mason9078a3e2007-04-26 16:46:15 -04003001static int write_one_cache_group(struct btrfs_trans_handle *trans,
3002 struct btrfs_root *root,
3003 struct btrfs_path *path,
3004 struct btrfs_block_group_cache *cache)
3005{
3006 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003007 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003008 unsigned long bi;
3009 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003010
Chris Mason9078a3e2007-04-26 16:46:15 -04003011 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003012 if (ret < 0)
3013 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003014 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003015
3016 leaf = path->nodes[0];
3017 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3018 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3019 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003020 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003021fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003022 if (ret) {
3023 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003024 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003025 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003026 return 0;
3027
3028}
3029
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003030static struct btrfs_block_group_cache *
3031next_block_group(struct btrfs_root *root,
3032 struct btrfs_block_group_cache *cache)
3033{
3034 struct rb_node *node;
3035 spin_lock(&root->fs_info->block_group_cache_lock);
3036 node = rb_next(&cache->cache_node);
3037 btrfs_put_block_group(cache);
3038 if (node) {
3039 cache = rb_entry(node, struct btrfs_block_group_cache,
3040 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003041 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003042 } else
3043 cache = NULL;
3044 spin_unlock(&root->fs_info->block_group_cache_lock);
3045 return cache;
3046}
3047
Josef Bacik0af3d002010-06-21 14:48:16 -04003048static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3049 struct btrfs_trans_handle *trans,
3050 struct btrfs_path *path)
3051{
3052 struct btrfs_root *root = block_group->fs_info->tree_root;
3053 struct inode *inode = NULL;
3054 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003055 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003056 int num_pages = 0;
3057 int retries = 0;
3058 int ret = 0;
3059
3060 /*
3061 * If this block group is smaller than 100 megs don't bother caching the
3062 * block group.
3063 */
3064 if (block_group->key.offset < (100 * 1024 * 1024)) {
3065 spin_lock(&block_group->lock);
3066 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3067 spin_unlock(&block_group->lock);
3068 return 0;
3069 }
3070
3071again:
3072 inode = lookup_free_space_inode(root, block_group, path);
3073 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3074 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003075 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003076 goto out;
3077 }
3078
3079 if (IS_ERR(inode)) {
3080 BUG_ON(retries);
3081 retries++;
3082
3083 if (block_group->ro)
3084 goto out_free;
3085
3086 ret = create_free_space_inode(root, trans, block_group, path);
3087 if (ret)
3088 goto out_free;
3089 goto again;
3090 }
3091
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003092 /* We've already setup this transaction, go ahead and exit */
3093 if (block_group->cache_generation == trans->transid &&
3094 i_size_read(inode)) {
3095 dcs = BTRFS_DC_SETUP;
3096 goto out_put;
3097 }
3098
Josef Bacik0af3d002010-06-21 14:48:16 -04003099 /*
3100 * We want to set the generation to 0, that way if anything goes wrong
3101 * from here on out we know not to trust this cache when we load up next
3102 * time.
3103 */
3104 BTRFS_I(inode)->generation = 0;
3105 ret = btrfs_update_inode(trans, root, inode);
3106 WARN_ON(ret);
3107
3108 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003109 ret = btrfs_check_trunc_cache_free_space(root,
3110 &root->fs_info->global_block_rsv);
3111 if (ret)
3112 goto out_put;
3113
Josef Bacik0af3d002010-06-21 14:48:16 -04003114 ret = btrfs_truncate_free_space_cache(root, trans, path,
3115 inode);
3116 if (ret)
3117 goto out_put;
3118 }
3119
3120 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003121 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3122 !btrfs_test_opt(root, SPACE_CACHE)) {
3123 /*
3124 * don't bother trying to write stuff out _if_
3125 * a) we're not cached,
3126 * b) we're with nospace_cache mount option.
3127 */
Josef Bacik2b209822010-12-03 13:17:53 -05003128 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003129 spin_unlock(&block_group->lock);
3130 goto out_put;
3131 }
3132 spin_unlock(&block_group->lock);
3133
Josef Bacik6fc823b2012-08-06 13:46:38 -06003134 /*
3135 * Try to preallocate enough space based on how big the block group is.
3136 * Keep in mind this has to include any pinned space which could end up
3137 * taking up quite a bit since it's not folded into the other space
3138 * cache.
3139 */
3140 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003141 if (!num_pages)
3142 num_pages = 1;
3143
Josef Bacik0af3d002010-06-21 14:48:16 -04003144 num_pages *= 16;
3145 num_pages *= PAGE_CACHE_SIZE;
3146
3147 ret = btrfs_check_data_free_space(inode, num_pages);
3148 if (ret)
3149 goto out_put;
3150
3151 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3152 num_pages, num_pages,
3153 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003154 if (!ret)
3155 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003156 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003157
Josef Bacik0af3d002010-06-21 14:48:16 -04003158out_put:
3159 iput(inode);
3160out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003161 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003162out:
3163 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003164 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003165 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003166 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003167 spin_unlock(&block_group->lock);
3168
3169 return ret;
3170}
3171
Chris Mason96b51792007-10-15 16:15:19 -04003172int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3173 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003174{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003175 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003176 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003177 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003178 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003179
3180 path = btrfs_alloc_path();
3181 if (!path)
3182 return -ENOMEM;
3183
Josef Bacik0af3d002010-06-21 14:48:16 -04003184again:
3185 while (1) {
3186 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3187 while (cache) {
3188 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3189 break;
3190 cache = next_block_group(root, cache);
3191 }
3192 if (!cache) {
3193 if (last == 0)
3194 break;
3195 last = 0;
3196 continue;
3197 }
3198 err = cache_save_setup(cache, trans, path);
3199 last = cache->key.objectid + cache->key.offset;
3200 btrfs_put_block_group(cache);
3201 }
3202
Chris Masond3977122009-01-05 21:25:51 -05003203 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003204 if (last == 0) {
3205 err = btrfs_run_delayed_refs(trans, root,
3206 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003207 if (err) /* File system offline */
3208 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003209 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003210
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003211 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3212 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003213 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3214 btrfs_put_block_group(cache);
3215 goto again;
3216 }
3217
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003218 if (cache->dirty)
3219 break;
3220 cache = next_block_group(root, cache);
3221 }
3222 if (!cache) {
3223 if (last == 0)
3224 break;
3225 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003226 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003227 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003228
Josef Bacik0cb59c92010-07-02 12:14:14 -04003229 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3230 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003231 cache->dirty = 0;
3232 last = cache->key.objectid + cache->key.offset;
3233
3234 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003235 if (err) /* File system offline */
3236 goto out;
3237
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003238 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003239 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003240
Josef Bacik0cb59c92010-07-02 12:14:14 -04003241 while (1) {
3242 /*
3243 * I don't think this is needed since we're just marking our
3244 * preallocated extent as written, but just in case it can't
3245 * hurt.
3246 */
3247 if (last == 0) {
3248 err = btrfs_run_delayed_refs(trans, root,
3249 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003250 if (err) /* File system offline */
3251 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003252 }
3253
3254 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3255 while (cache) {
3256 /*
3257 * Really this shouldn't happen, but it could if we
3258 * couldn't write the entire preallocated extent and
3259 * splitting the extent resulted in a new block.
3260 */
3261 if (cache->dirty) {
3262 btrfs_put_block_group(cache);
3263 goto again;
3264 }
3265 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3266 break;
3267 cache = next_block_group(root, cache);
3268 }
3269 if (!cache) {
3270 if (last == 0)
3271 break;
3272 last = 0;
3273 continue;
3274 }
3275
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003276 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003277
3278 /*
3279 * If we didn't have an error then the cache state is still
3280 * NEED_WRITE, so we can set it to WRITTEN.
3281 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003282 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003283 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3284 last = cache->key.objectid + cache->key.offset;
3285 btrfs_put_block_group(cache);
3286 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003287out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003288
Chris Mason9078a3e2007-04-26 16:46:15 -04003289 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003290 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003291}
3292
Yan Zhengd2fb3432008-12-11 16:30:39 -05003293int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3294{
3295 struct btrfs_block_group_cache *block_group;
3296 int readonly = 0;
3297
3298 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3299 if (!block_group || block_group->ro)
3300 readonly = 1;
3301 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003302 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003303 return readonly;
3304}
3305
Chris Mason593060d2008-03-25 16:50:33 -04003306static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3307 u64 total_bytes, u64 bytes_used,
3308 struct btrfs_space_info **space_info)
3309{
3310 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003311 int i;
3312 int factor;
3313
3314 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3315 BTRFS_BLOCK_GROUP_RAID10))
3316 factor = 2;
3317 else
3318 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003319
3320 found = __find_space_info(info, flags);
3321 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003322 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003323 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003324 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003325 found->bytes_used += bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003326 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003327 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003328 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003329 *space_info = found;
3330 return 0;
3331 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003332 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003333 if (!found)
3334 return -ENOMEM;
3335
Yan, Zhengb742bb822010-05-16 10:46:24 -04003336 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3337 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003338 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003339 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003340 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003341 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003342 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003343 found->bytes_used = bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003344 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003345 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003346 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003347 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003348 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003349 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003350 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003351 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003352 found->flush = 0;
3353 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003354 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003355 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003356 if (flags & BTRFS_BLOCK_GROUP_DATA)
3357 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003358 return 0;
3359}
3360
Chris Mason8790d502008-04-03 16:29:03 -04003361static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3362{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003363 u64 extra_flags = chunk_to_extended(flags) &
3364 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003365
Miao Xiede98ced2013-01-29 10:13:12 +00003366 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003367 if (flags & BTRFS_BLOCK_GROUP_DATA)
3368 fs_info->avail_data_alloc_bits |= extra_flags;
3369 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3370 fs_info->avail_metadata_alloc_bits |= extra_flags;
3371 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3372 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003373 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003374}
Chris Mason593060d2008-03-25 16:50:33 -04003375
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003376/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003377 * returns target flags in extended format or 0 if restripe for this
3378 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003379 *
3380 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003381 */
3382static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3383{
3384 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3385 u64 target = 0;
3386
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003387 if (!bctl)
3388 return 0;
3389
3390 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3391 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3392 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3393 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3394 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3395 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3396 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3397 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3398 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3399 }
3400
3401 return target;
3402}
3403
3404/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003405 * @flags: available profiles in extended format (see ctree.h)
3406 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003407 * Returns reduced profile in chunk format. If profile changing is in
3408 * progress (either running or paused) picks the target profile (if it's
3409 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003410 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003411static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003412{
Chris Masoncd02dca2010-12-13 14:56:23 -05003413 /*
3414 * we add in the count of missing devices because we want
3415 * to make sure that any RAID levels on a degraded FS
3416 * continue to be honored.
3417 */
3418 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3419 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003420 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003421 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003422
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003423 /*
3424 * see if restripe for this chunk_type is in progress, if so
3425 * try to reduce to the target profile
3426 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003427 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003428 target = get_restripe_target(root->fs_info, flags);
3429 if (target) {
3430 /* pick target profile only if it's already available */
3431 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003432 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003433 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003434 }
3435 }
3436 spin_unlock(&root->fs_info->balance_lock);
3437
David Woodhouse53b381b2013-01-29 18:40:14 -05003438 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003439 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003440 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3441 BTRFS_BLOCK_GROUP_RAID5);
3442 if (num_devices < 3)
3443 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003444 if (num_devices < 4)
3445 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3446
David Woodhouse53b381b2013-01-29 18:40:14 -05003447 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3448 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3449 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3450 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003451
David Woodhouse53b381b2013-01-29 18:40:14 -05003452 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3453 tmp = BTRFS_BLOCK_GROUP_RAID6;
3454 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3455 tmp = BTRFS_BLOCK_GROUP_RAID5;
3456 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3457 tmp = BTRFS_BLOCK_GROUP_RAID10;
3458 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3459 tmp = BTRFS_BLOCK_GROUP_RAID1;
3460 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3461 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003462
David Woodhouse53b381b2013-01-29 18:40:14 -05003463 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003464}
3465
Yan, Zhengb742bb822010-05-16 10:46:24 -04003466static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003467{
Miao Xiede98ced2013-01-29 10:13:12 +00003468 unsigned seq;
3469
3470 do {
3471 seq = read_seqbegin(&root->fs_info->profiles_lock);
3472
3473 if (flags & BTRFS_BLOCK_GROUP_DATA)
3474 flags |= root->fs_info->avail_data_alloc_bits;
3475 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3476 flags |= root->fs_info->avail_system_alloc_bits;
3477 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3478 flags |= root->fs_info->avail_metadata_alloc_bits;
3479 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003480
Yan, Zhengb742bb822010-05-16 10:46:24 -04003481 return btrfs_reduce_alloc_profile(root, flags);
3482}
Josef Bacik6a632092009-02-20 11:00:09 -05003483
Miao Xie6d07bce2011-01-05 10:07:31 +00003484u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003485{
3486 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003487 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003488
Yan, Zhengb742bb822010-05-16 10:46:24 -04003489 if (data)
3490 flags = BTRFS_BLOCK_GROUP_DATA;
3491 else if (root == root->fs_info->chunk_root)
3492 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3493 else
3494 flags = BTRFS_BLOCK_GROUP_METADATA;
3495
David Woodhouse53b381b2013-01-29 18:40:14 -05003496 ret = get_alloc_profile(root, flags);
3497 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003498}
3499
Josef Bacik6a632092009-02-20 11:00:09 -05003500/*
3501 * This will check the space that the inode allocates from to make sure we have
3502 * enough space for bytes.
3503 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003504int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003505{
3506 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003507 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003508 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003509 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003510 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003511
3512 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003513 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003514
Li Zefan82d59022011-04-20 10:33:24 +08003515 if (root == root->fs_info->tree_root ||
3516 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003517 alloc_chunk = 0;
3518 committed = 1;
3519 }
3520
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003521 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003522 if (!data_sinfo)
3523 goto alloc;
3524
Josef Bacik6a632092009-02-20 11:00:09 -05003525again:
3526 /* make sure we have enough space to handle the data first */
3527 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003528 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3529 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3530 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003531
3532 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003533 struct btrfs_trans_handle *trans;
3534
Josef Bacik6a632092009-02-20 11:00:09 -05003535 /*
3536 * if we don't have enough free bytes in this space then we need
3537 * to alloc a new chunk.
3538 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003539 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003540 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003541
Chris Mason0e4f8f82011-04-15 16:05:44 -04003542 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003543 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003544alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003545 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003546 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003547 if (IS_ERR(trans))
3548 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003549
3550 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003551 alloc_target,
3552 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003553 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003554 if (ret < 0) {
3555 if (ret != -ENOSPC)
3556 return ret;
3557 else
3558 goto commit_trans;
3559 }
Chris Mason33b4d472009-09-22 14:45:50 -04003560
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003561 if (!data_sinfo)
3562 data_sinfo = fs_info->data_sinfo;
3563
Josef Bacik6a632092009-02-20 11:00:09 -05003564 goto again;
3565 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003566
3567 /*
3568 * If we have less pinned bytes than we want to allocate then
3569 * don't bother committing the transaction, it won't help us.
3570 */
3571 if (data_sinfo->bytes_pinned < bytes)
3572 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003573 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003574
3575 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003576commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003577 if (!committed &&
3578 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003579 committed = 1;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003580 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003581 if (IS_ERR(trans))
3582 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003583 ret = btrfs_commit_transaction(trans, root);
3584 if (ret)
3585 return ret;
3586 goto again;
3587 }
3588
Josef Bacik6a632092009-02-20 11:00:09 -05003589 return -ENOSPC;
3590 }
3591 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003592 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003593 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003594 spin_unlock(&data_sinfo->lock);
3595
Josef Bacik9ed74f22009-09-11 16:12:44 -04003596 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003597}
3598
3599/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003600 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003601 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003602void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003603{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003604 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003605 struct btrfs_space_info *data_sinfo;
3606
3607 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003608 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003609
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003610 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003611 spin_lock(&data_sinfo->lock);
3612 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003613 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003614 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003615 spin_unlock(&data_sinfo->lock);
3616}
3617
Josef Bacik97e728d2009-04-21 17:40:57 -04003618static void force_metadata_allocation(struct btrfs_fs_info *info)
3619{
3620 struct list_head *head = &info->space_info;
3621 struct btrfs_space_info *found;
3622
3623 rcu_read_lock();
3624 list_for_each_entry_rcu(found, head, list) {
3625 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003626 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003627 }
3628 rcu_read_unlock();
3629}
3630
Miao Xie3c76cd82013-04-25 10:12:38 +00003631static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3632{
3633 return (global->size << 1);
3634}
3635
Chris Masone5bc2452010-10-26 13:37:56 -04003636static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003637 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003638{
Josef Bacikfb25e912011-07-26 17:00:46 -04003639 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003640 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003641 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003642 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003643
Chris Mason0e4f8f82011-04-15 16:05:44 -04003644 if (force == CHUNK_ALLOC_FORCE)
3645 return 1;
3646
3647 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003648 * We need to take into account the global rsv because for all intents
3649 * and purposes it's used space. Don't worry about locking the
3650 * global_rsv, it doesn't change except when the transaction commits.
3651 */
Josef Bacik54338b52012-08-14 16:20:52 -04003652 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003653 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003654
3655 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003656 * in limited mode, we want to have some free space up to
3657 * about 1% of the FS size.
3658 */
3659 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003660 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003661 thresh = max_t(u64, 64 * 1024 * 1024,
3662 div_factor_fine(thresh, 1));
3663
3664 if (num_bytes - num_allocated < thresh)
3665 return 1;
3666 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003667
Josef Bacik698d0082012-09-12 14:08:47 -04003668 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003669 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003670 return 1;
3671}
3672
Liu Bo15d1ff82012-03-29 09:57:44 -04003673static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3674{
3675 u64 num_dev;
3676
David Woodhouse53b381b2013-01-29 18:40:14 -05003677 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3678 BTRFS_BLOCK_GROUP_RAID0 |
3679 BTRFS_BLOCK_GROUP_RAID5 |
3680 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003681 num_dev = root->fs_info->fs_devices->rw_devices;
3682 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3683 num_dev = 2;
3684 else
3685 num_dev = 1; /* DUP or single */
3686
3687 /* metadata for updaing devices and chunk tree */
3688 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3689}
3690
3691static void check_system_chunk(struct btrfs_trans_handle *trans,
3692 struct btrfs_root *root, u64 type)
3693{
3694 struct btrfs_space_info *info;
3695 u64 left;
3696 u64 thresh;
3697
3698 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3699 spin_lock(&info->lock);
3700 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3701 info->bytes_reserved - info->bytes_readonly;
3702 spin_unlock(&info->lock);
3703
3704 thresh = get_system_chunk_thresh(root, type);
3705 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003706 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3707 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003708 dump_space_info(info, 0, 0);
3709 }
3710
3711 if (left < thresh) {
3712 u64 flags;
3713
3714 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3715 btrfs_alloc_chunk(trans, root, flags);
3716 }
3717}
3718
Chris Mason6324fbf2008-03-24 15:01:59 -04003719static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003720 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003721{
3722 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003723 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003724 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003725 int ret = 0;
3726
Josef Bacikc6b305a2012-12-18 09:16:16 -05003727 /* Don't re-enter if we're already allocating a chunk */
3728 if (trans->allocating_chunk)
3729 return -ENOSPC;
3730
Chris Mason6324fbf2008-03-24 15:01:59 -04003731 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003732 if (!space_info) {
3733 ret = update_space_info(extent_root->fs_info, flags,
3734 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003735 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003736 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003737 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003738
Josef Bacik6d741192011-04-11 20:20:11 -04003739again:
Josef Bacik25179202008-10-29 14:49:05 -04003740 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003741 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003742 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003743 if (space_info->full) {
3744 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003745 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003746 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003747
Josef Bacik698d0082012-09-12 14:08:47 -04003748 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003749 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003750 return 0;
3751 } else if (space_info->chunk_alloc) {
3752 wait_for_alloc = 1;
3753 } else {
3754 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003755 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003756
Josef Bacik25179202008-10-29 14:49:05 -04003757 spin_unlock(&space_info->lock);
3758
Josef Bacik6d741192011-04-11 20:20:11 -04003759 mutex_lock(&fs_info->chunk_mutex);
3760
3761 /*
3762 * The chunk_mutex is held throughout the entirety of a chunk
3763 * allocation, so once we've acquired the chunk_mutex we know that the
3764 * other guy is done and we need to recheck and see if we should
3765 * allocate.
3766 */
3767 if (wait_for_alloc) {
3768 mutex_unlock(&fs_info->chunk_mutex);
3769 wait_for_alloc = 0;
3770 goto again;
3771 }
3772
Josef Bacikc6b305a2012-12-18 09:16:16 -05003773 trans->allocating_chunk = true;
3774
Josef Bacik97e728d2009-04-21 17:40:57 -04003775 /*
Josef Bacik67377732010-09-16 16:19:09 -04003776 * If we have mixed data/metadata chunks we want to make sure we keep
3777 * allocating mixed chunks instead of individual chunks.
3778 */
3779 if (btrfs_mixed_space_info(space_info))
3780 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3781
3782 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003783 * if we're doing a data chunk, go ahead and make sure that
3784 * we keep a reasonable number of metadata chunks allocated in the
3785 * FS as well.
3786 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003787 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003788 fs_info->data_chunk_allocations++;
3789 if (!(fs_info->data_chunk_allocations %
3790 fs_info->metadata_ratio))
3791 force_metadata_allocation(fs_info);
3792 }
3793
Liu Bo15d1ff82012-03-29 09:57:44 -04003794 /*
3795 * Check if we have enough space in SYSTEM chunk because we may need
3796 * to update devices.
3797 */
3798 check_system_chunk(trans, extent_root, flags);
3799
Yan Zheng2b820322008-11-17 21:11:30 -05003800 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003801 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003802
Josef Bacik9ed74f22009-09-11 16:12:44 -04003803 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003804 if (ret < 0 && ret != -ENOSPC)
3805 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003806 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003807 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003808 else
3809 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003810
Chris Mason0e4f8f82011-04-15 16:05:44 -04003811 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003812out:
Josef Bacik6d741192011-04-11 20:20:11 -04003813 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003814 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003815 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003816 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003817}
3818
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003819static int can_overcommit(struct btrfs_root *root,
3820 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003821 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003822{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003823 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003824 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00003825 u64 space_size;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003826 u64 avail;
3827 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003828 u64 to_add;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003829
3830 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003831 space_info->bytes_pinned + space_info->bytes_readonly;
3832
Josef Bacik96f1bb52013-01-30 17:02:51 -05003833 /*
3834 * We only want to allow over committing if we have lots of actual space
3835 * free, but if we don't have enough space to handle the global reserve
3836 * space then we could end up having a real enospc problem when trying
3837 * to allocate a chunk or some other such important allocation.
3838 */
Miao Xie3c76cd82013-04-25 10:12:38 +00003839 spin_lock(&global_rsv->lock);
3840 space_size = calc_global_rsv_need_space(global_rsv);
3841 spin_unlock(&global_rsv->lock);
3842 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05003843 return 0;
3844
3845 used += space_info->bytes_may_use;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003846
3847 spin_lock(&root->fs_info->free_chunk_lock);
3848 avail = root->fs_info->free_chunk_space;
3849 spin_unlock(&root->fs_info->free_chunk_lock);
3850
3851 /*
3852 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003853 * space is actually useable. For raid56, the space info used
3854 * doesn't include the parity drive, so we don't have to
3855 * change the math
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003856 */
3857 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3858 BTRFS_BLOCK_GROUP_RAID1 |
3859 BTRFS_BLOCK_GROUP_RAID10))
3860 avail >>= 1;
3861
Josef Bacik70afa392013-02-06 13:53:19 -05003862 to_add = space_info->total_bytes;
3863
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003864 /*
Miao Xie561c2942012-10-16 11:32:18 +00003865 * If we aren't flushing all things, let us overcommit up to
3866 * 1/2th of the space. If we can flush, don't let us overcommit
3867 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003868 */
Miao Xie08e007d2012-10-16 11:33:38 +00003869 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003870 to_add >>= 3;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003871 else
Josef Bacik70afa392013-02-06 13:53:19 -05003872 to_add >>= 1;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003873
Josef Bacik70afa392013-02-06 13:53:19 -05003874 /*
3875 * Limit the overcommit to the amount of free space we could possibly
3876 * allocate for chunks.
3877 */
3878 to_add = min(avail, to_add);
3879
3880 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04003881 return 1;
3882 return 0;
3883}
3884
Eric Sandeen48a3b632013-04-25 20:41:01 +00003885static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3886 unsigned long nr_pages)
Miao Xieda633a42012-12-20 11:19:09 +00003887{
3888 struct super_block *sb = root->fs_info->sb;
3889 int started;
3890
3891 /* If we can not start writeback, just sync all the delalloc file. */
Linus Torvaldsb6951882013-03-02 16:41:54 -08003892 started = try_to_writeback_inodes_sb_nr(sb, nr_pages,
Miao Xieda633a42012-12-20 11:19:09 +00003893 WB_REASON_FS_FREE_SPACE);
3894 if (!started) {
3895 /*
3896 * We needn't worry the filesystem going from r/w to r/o though
3897 * we don't acquire ->s_umount mutex, because the filesystem
3898 * should guarantee the delalloc inodes list be empty after
3899 * the filesystem is readonly(all dirty pages are written to
3900 * the disk).
3901 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00003902 btrfs_start_all_delalloc_inodes(root->fs_info, 0);
Josef Bacik98ad69c2013-04-04 11:55:49 -04003903 if (!current->journal_info)
Miao Xie199c2a92013-05-15 07:48:23 +00003904 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Miao Xieda633a42012-12-20 11:19:09 +00003905 }
3906}
3907
Yan, Zheng5da9d012010-05-16 10:46:25 -04003908/*
3909 * shrink metadata reservation for delalloc
3910 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003911static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3912 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003913{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003914 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003915 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003916 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003917 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003918 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003919 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04003920 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00003921 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00003922 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003923
Josef Bacik663350a2011-11-03 22:54:25 -04003924 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003925 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003926 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04003927
3928 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003929 delalloc_bytes = percpu_counter_sum_positive(
3930 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003931 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003932 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04003933 return;
Miao Xie199c2a92013-05-15 07:48:23 +00003934 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04003935 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003936 }
3937
Josef Bacikf4c738c2012-07-02 17:10:51 -04003938 while (delalloc_bytes && loops < 3) {
3939 max_reclaim = min(delalloc_bytes, to_reclaim);
3940 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00003941 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04003942 /*
3943 * We need to wait for the async pages to actually start before
3944 * we do anything.
3945 */
3946 wait_event(root->fs_info->async_submit_wait,
3947 !atomic_read(&root->fs_info->async_delalloc_pages));
3948
Miao Xie08e007d2012-10-16 11:33:38 +00003949 if (!trans)
3950 flush = BTRFS_RESERVE_FLUSH_ALL;
3951 else
3952 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04003953 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00003954 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003955 spin_unlock(&space_info->lock);
3956 break;
3957 }
Josef Bacik0019f102010-10-15 15:18:40 -04003958 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003959
Chris Mason36e39c42011-03-12 07:08:42 -05003960 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04003961 if (wait_ordered && !trans) {
Miao Xie199c2a92013-05-15 07:48:23 +00003962 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04003963 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04003964 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04003965 if (time_left)
3966 break;
3967 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04003968 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00003969 delalloc_bytes = percpu_counter_sum_positive(
3970 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04003971 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04003972}
3973
Josef Bacik4a92b1b2011-08-30 12:34:28 -04003974/**
Josef Bacik663350a2011-11-03 22:54:25 -04003975 * maybe_commit_transaction - possibly commit the transaction if its ok to
3976 * @root - the root we're allocating for
3977 * @bytes - the number of bytes we want to reserve
3978 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003979 *
Josef Bacik663350a2011-11-03 22:54:25 -04003980 * This will check to make sure that committing the transaction will actually
3981 * get us somewhere and then commit the transaction if it does. Otherwise it
3982 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04003983 */
Josef Bacik663350a2011-11-03 22:54:25 -04003984static int may_commit_transaction(struct btrfs_root *root,
3985 struct btrfs_space_info *space_info,
3986 u64 bytes, int force)
3987{
3988 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3989 struct btrfs_trans_handle *trans;
3990
3991 trans = (struct btrfs_trans_handle *)current->journal_info;
3992 if (trans)
3993 return -EAGAIN;
3994
3995 if (force)
3996 goto commit;
3997
3998 /* See if there is enough pinned space to make this reservation */
3999 spin_lock(&space_info->lock);
4000 if (space_info->bytes_pinned >= bytes) {
4001 spin_unlock(&space_info->lock);
4002 goto commit;
4003 }
4004 spin_unlock(&space_info->lock);
4005
4006 /*
4007 * See if there is some space in the delayed insertion reservation for
4008 * this reservation.
4009 */
4010 if (space_info != delayed_rsv->space_info)
4011 return -ENOSPC;
4012
Liu Bod9b02182012-02-16 18:34:39 +08004013 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004014 spin_lock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004015 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
Josef Bacik663350a2011-11-03 22:54:25 -04004016 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004017 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004018 return -ENOSPC;
4019 }
4020 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004021 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004022
4023commit:
4024 trans = btrfs_join_transaction(root);
4025 if (IS_ERR(trans))
4026 return -ENOSPC;
4027
4028 return btrfs_commit_transaction(trans, root);
4029}
4030
Josef Bacik96c3f432012-06-21 14:05:49 -04004031enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004032 FLUSH_DELAYED_ITEMS_NR = 1,
4033 FLUSH_DELAYED_ITEMS = 2,
4034 FLUSH_DELALLOC = 3,
4035 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004036 ALLOC_CHUNK = 5,
4037 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004038};
4039
4040static int flush_space(struct btrfs_root *root,
4041 struct btrfs_space_info *space_info, u64 num_bytes,
4042 u64 orig_bytes, int state)
4043{
4044 struct btrfs_trans_handle *trans;
4045 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004046 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004047
4048 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004049 case FLUSH_DELAYED_ITEMS_NR:
4050 case FLUSH_DELAYED_ITEMS:
4051 if (state == FLUSH_DELAYED_ITEMS_NR) {
4052 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
4053
4054 nr = (int)div64_u64(num_bytes, bytes);
4055 if (!nr)
4056 nr = 1;
4057 nr *= 2;
4058 } else {
4059 nr = -1;
4060 }
4061 trans = btrfs_join_transaction(root);
4062 if (IS_ERR(trans)) {
4063 ret = PTR_ERR(trans);
4064 break;
4065 }
4066 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4067 btrfs_end_transaction(trans, root);
4068 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004069 case FLUSH_DELALLOC:
4070 case FLUSH_DELALLOC_WAIT:
4071 shrink_delalloc(root, num_bytes, orig_bytes,
4072 state == FLUSH_DELALLOC_WAIT);
4073 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004074 case ALLOC_CHUNK:
4075 trans = btrfs_join_transaction(root);
4076 if (IS_ERR(trans)) {
4077 ret = PTR_ERR(trans);
4078 break;
4079 }
4080 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004081 btrfs_get_alloc_profile(root, 0),
4082 CHUNK_ALLOC_NO_FORCE);
4083 btrfs_end_transaction(trans, root);
4084 if (ret == -ENOSPC)
4085 ret = 0;
4086 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004087 case COMMIT_TRANS:
4088 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4089 break;
4090 default:
4091 ret = -ENOSPC;
4092 break;
4093 }
4094
4095 return ret;
4096}
Josef Bacik663350a2011-11-03 22:54:25 -04004097/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004098 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4099 * @root - the root we're allocating for
4100 * @block_rsv - the block_rsv we're allocating for
4101 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004102 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004103 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004104 * This will reserve orgi_bytes number of bytes from the space info associated
4105 * with the block_rsv. If there is not enough space it will make an attempt to
4106 * flush out space to make room. It will do this by flushing delalloc if
4107 * possible or committing the transaction. If flush is 0 then no attempts to
4108 * regain reservations will be made and this will fail if there is not enough
4109 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004110 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004111static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004112 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004113 u64 orig_bytes,
4114 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004115{
4116 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004117 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004118 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004119 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004120 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004121 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004122
4123again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004124 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004125 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004126 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004127 * We only want to wait if somebody other than us is flushing and we
4128 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004129 */
Miao Xie08e007d2012-10-16 11:33:38 +00004130 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4131 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004132 spin_unlock(&space_info->lock);
4133 /*
4134 * If we have a trans handle we can't wait because the flusher
4135 * may have to commit the transaction, which would mean we would
4136 * deadlock since we are waiting for the flusher to finish, but
4137 * hold the current transaction open.
4138 */
Josef Bacik663350a2011-11-03 22:54:25 -04004139 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004140 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004141 ret = wait_event_killable(space_info->wait, !space_info->flush);
4142 /* Must have been killed, return */
4143 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004144 return -EINTR;
4145
4146 spin_lock(&space_info->lock);
4147 }
4148
4149 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004150 used = space_info->bytes_used + space_info->bytes_reserved +
4151 space_info->bytes_pinned + space_info->bytes_readonly +
4152 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004153
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004154 /*
4155 * The idea here is that we've not already over-reserved the block group
4156 * then we can go ahead and save our reservation first and then start
4157 * flushing if we need to. Otherwise if we've already overcommitted
4158 * lets start flushing stuff first and then come back and try to make
4159 * our reservation.
4160 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004161 if (used <= space_info->total_bytes) {
4162 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004163 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004164 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004165 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004166 ret = 0;
4167 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004168 /*
4169 * Ok set num_bytes to orig_bytes since we aren't
4170 * overocmmitted, this way we only try and reclaim what
4171 * we need.
4172 */
4173 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004174 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004175 } else {
4176 /*
4177 * Ok we're over committed, set num_bytes to the overcommitted
4178 * amount plus the amount of bytes that we need for this
4179 * reservation.
4180 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004181 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004182 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004183 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004184
Josef Bacik44734ed2012-09-28 16:04:19 -04004185 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4186 space_info->bytes_may_use += orig_bytes;
4187 trace_btrfs_space_reservation(root->fs_info, "space_info",
4188 space_info->flags, orig_bytes,
4189 1);
4190 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004191 }
4192
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004193 /*
4194 * Couldn't make our reservation, save our place so while we're trying
4195 * to reclaim space we can actually use it instead of somebody else
4196 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004197 *
4198 * We make the other tasks wait for the flush only when we can flush
4199 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004200 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004201 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004202 flushing = true;
4203 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004204 }
4205
Yan, Zhengf0486c62010-05-16 10:46:25 -04004206 spin_unlock(&space_info->lock);
4207
Miao Xie08e007d2012-10-16 11:33:38 +00004208 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004209 goto out;
4210
Josef Bacik96c3f432012-06-21 14:05:49 -04004211 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4212 flush_state);
4213 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004214
4215 /*
4216 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4217 * would happen. So skip delalloc flush.
4218 */
4219 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4220 (flush_state == FLUSH_DELALLOC ||
4221 flush_state == FLUSH_DELALLOC_WAIT))
4222 flush_state = ALLOC_CHUNK;
4223
Josef Bacik96c3f432012-06-21 14:05:49 -04004224 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004225 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004226 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4227 flush_state < COMMIT_TRANS)
4228 goto again;
4229 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4230 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004231 goto again;
4232
4233out:
Josef Bacik5d803662013-02-07 16:06:02 -05004234 if (ret == -ENOSPC &&
4235 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4236 struct btrfs_block_rsv *global_rsv =
4237 &root->fs_info->global_block_rsv;
4238
4239 if (block_rsv != global_rsv &&
4240 !block_rsv_use_bytes(global_rsv, orig_bytes))
4241 ret = 0;
4242 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004243 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004244 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004245 space_info->flush = 0;
4246 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004247 spin_unlock(&space_info->lock);
4248 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004249 return ret;
4250}
4251
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004252static struct btrfs_block_rsv *get_block_rsv(
4253 const struct btrfs_trans_handle *trans,
4254 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004255{
Josef Bacik4c13d752011-08-30 11:31:29 -04004256 struct btrfs_block_rsv *block_rsv = NULL;
4257
Josef Bacik0e721102012-06-26 16:13:18 -04004258 if (root->ref_cows)
4259 block_rsv = trans->block_rsv;
4260
4261 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004262 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004263
4264 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004265 block_rsv = root->block_rsv;
4266
4267 if (!block_rsv)
4268 block_rsv = &root->fs_info->empty_block_rsv;
4269
4270 return block_rsv;
4271}
4272
4273static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4274 u64 num_bytes)
4275{
4276 int ret = -ENOSPC;
4277 spin_lock(&block_rsv->lock);
4278 if (block_rsv->reserved >= num_bytes) {
4279 block_rsv->reserved -= num_bytes;
4280 if (block_rsv->reserved < block_rsv->size)
4281 block_rsv->full = 0;
4282 ret = 0;
4283 }
4284 spin_unlock(&block_rsv->lock);
4285 return ret;
4286}
4287
4288static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4289 u64 num_bytes, int update_size)
4290{
4291 spin_lock(&block_rsv->lock);
4292 block_rsv->reserved += num_bytes;
4293 if (update_size)
4294 block_rsv->size += num_bytes;
4295 else if (block_rsv->reserved >= block_rsv->size)
4296 block_rsv->full = 1;
4297 spin_unlock(&block_rsv->lock);
4298}
4299
Josef Bacikd52be812013-05-29 14:54:47 -04004300int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4301 struct btrfs_block_rsv *dest, u64 num_bytes,
4302 int min_factor)
4303{
4304 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4305 u64 min_bytes;
4306
4307 if (global_rsv->space_info != dest->space_info)
4308 return -ENOSPC;
4309
4310 spin_lock(&global_rsv->lock);
4311 min_bytes = div_factor(global_rsv->size, min_factor);
4312 if (global_rsv->reserved < min_bytes + num_bytes) {
4313 spin_unlock(&global_rsv->lock);
4314 return -ENOSPC;
4315 }
4316 global_rsv->reserved -= num_bytes;
4317 if (global_rsv->reserved < global_rsv->size)
4318 global_rsv->full = 0;
4319 spin_unlock(&global_rsv->lock);
4320
4321 block_rsv_add_bytes(dest, num_bytes, 1);
4322 return 0;
4323}
4324
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004325static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4326 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004327 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004328{
4329 struct btrfs_space_info *space_info = block_rsv->space_info;
4330
4331 spin_lock(&block_rsv->lock);
4332 if (num_bytes == (u64)-1)
4333 num_bytes = block_rsv->size;
4334 block_rsv->size -= num_bytes;
4335 if (block_rsv->reserved >= block_rsv->size) {
4336 num_bytes = block_rsv->reserved - block_rsv->size;
4337 block_rsv->reserved = block_rsv->size;
4338 block_rsv->full = 1;
4339 } else {
4340 num_bytes = 0;
4341 }
4342 spin_unlock(&block_rsv->lock);
4343
4344 if (num_bytes > 0) {
4345 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004346 spin_lock(&dest->lock);
4347 if (!dest->full) {
4348 u64 bytes_to_add;
4349
4350 bytes_to_add = dest->size - dest->reserved;
4351 bytes_to_add = min(num_bytes, bytes_to_add);
4352 dest->reserved += bytes_to_add;
4353 if (dest->reserved >= dest->size)
4354 dest->full = 1;
4355 num_bytes -= bytes_to_add;
4356 }
4357 spin_unlock(&dest->lock);
4358 }
4359 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004360 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004361 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004362 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004363 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004364 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004365 spin_unlock(&space_info->lock);
4366 }
4367 }
4368}
4369
4370static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4371 struct btrfs_block_rsv *dst, u64 num_bytes)
4372{
4373 int ret;
4374
4375 ret = block_rsv_use_bytes(src, num_bytes);
4376 if (ret)
4377 return ret;
4378
4379 block_rsv_add_bytes(dst, num_bytes, 1);
4380 return 0;
4381}
4382
Miao Xie66d8f3d2012-09-06 04:02:28 -06004383void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004384{
4385 memset(rsv, 0, sizeof(*rsv));
4386 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004387 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004388}
4389
Miao Xie66d8f3d2012-09-06 04:02:28 -06004390struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4391 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004392{
4393 struct btrfs_block_rsv *block_rsv;
4394 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004395
4396 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4397 if (!block_rsv)
4398 return NULL;
4399
Miao Xie66d8f3d2012-09-06 04:02:28 -06004400 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004401 block_rsv->space_info = __find_space_info(fs_info,
4402 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004403 return block_rsv;
4404}
4405
4406void btrfs_free_block_rsv(struct btrfs_root *root,
4407 struct btrfs_block_rsv *rsv)
4408{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004409 if (!rsv)
4410 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004411 btrfs_block_rsv_release(root, rsv, (u64)-1);
4412 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004413}
4414
Miao Xie08e007d2012-10-16 11:33:38 +00004415int btrfs_block_rsv_add(struct btrfs_root *root,
4416 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4417 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004418{
4419 int ret;
4420
4421 if (num_bytes == 0)
4422 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004423
Miao Xie61b520a2011-11-10 20:45:05 -05004424 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004425 if (!ret) {
4426 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4427 return 0;
4428 }
4429
Yan, Zhengf0486c62010-05-16 10:46:25 -04004430 return ret;
4431}
4432
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004433int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004434 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004435{
4436 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004437 int ret = -ENOSPC;
4438
4439 if (!block_rsv)
4440 return 0;
4441
4442 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004443 num_bytes = div_factor(block_rsv->size, min_factor);
4444 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004445 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004446 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004447
Josef Bacik36ba0222011-10-18 12:15:48 -04004448 return ret;
4449}
4450
Miao Xie08e007d2012-10-16 11:33:38 +00004451int btrfs_block_rsv_refill(struct btrfs_root *root,
4452 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4453 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004454{
4455 u64 num_bytes = 0;
4456 int ret = -ENOSPC;
4457
4458 if (!block_rsv)
4459 return 0;
4460
4461 spin_lock(&block_rsv->lock);
4462 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004463 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004464 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004465 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004466 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004467 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004468
Yan, Zhengf0486c62010-05-16 10:46:25 -04004469 if (!ret)
4470 return 0;
4471
Miao Xieaa38a712011-11-18 17:43:00 +08004472 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004473 if (!ret) {
4474 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004475 return 0;
4476 }
4477
Josef Bacik13553e52011-08-08 13:33:21 -04004478 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004479}
4480
4481int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4482 struct btrfs_block_rsv *dst_rsv,
4483 u64 num_bytes)
4484{
4485 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4486}
4487
4488void btrfs_block_rsv_release(struct btrfs_root *root,
4489 struct btrfs_block_rsv *block_rsv,
4490 u64 num_bytes)
4491{
4492 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4493 if (global_rsv->full || global_rsv == block_rsv ||
4494 block_rsv->space_info != global_rsv->space_info)
4495 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004496 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4497 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004498}
4499
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004500/*
4501 * helper to calculate size of global block reservation.
4502 * the desired value is sum of space used by extent tree,
4503 * checksum tree and root tree
4504 */
4505static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4506{
4507 struct btrfs_space_info *sinfo;
4508 u64 num_bytes;
4509 u64 meta_used;
4510 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004511 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004512
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004513 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4514 spin_lock(&sinfo->lock);
4515 data_used = sinfo->bytes_used;
4516 spin_unlock(&sinfo->lock);
4517
4518 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4519 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004520 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4521 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004522 meta_used = sinfo->bytes_used;
4523 spin_unlock(&sinfo->lock);
4524
4525 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4526 csum_size * 2;
4527 num_bytes += div64_u64(data_used + meta_used, 50);
4528
4529 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004530 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004531
4532 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4533}
4534
4535static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4536{
4537 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4538 struct btrfs_space_info *sinfo = block_rsv->space_info;
4539 u64 num_bytes;
4540
4541 num_bytes = calc_global_metadata_size(fs_info);
4542
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004543 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004544 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004545
Josef Bacikfdf30d12013-03-26 15:31:45 -04004546 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004547
4548 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004549 sinfo->bytes_reserved + sinfo->bytes_readonly +
4550 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004551
4552 if (sinfo->total_bytes > num_bytes) {
4553 num_bytes = sinfo->total_bytes - num_bytes;
4554 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004555 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004556 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004557 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004558 }
4559
4560 if (block_rsv->reserved >= block_rsv->size) {
4561 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004562 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004563 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004564 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004565 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004566 block_rsv->reserved = block_rsv->size;
4567 block_rsv->full = 1;
4568 }
David Sterba182608c2011-05-05 13:13:16 +02004569
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004570 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004571 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004572}
4573
Yan, Zhengf0486c62010-05-16 10:46:25 -04004574static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4575{
4576 struct btrfs_space_info *space_info;
4577
4578 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4579 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004580
4581 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004582 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004583 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004584 fs_info->trans_block_rsv.space_info = space_info;
4585 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004586 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004587
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004588 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4589 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4590 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4591 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00004592 if (fs_info->quota_root)
4593 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004594 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004595
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004596 update_global_block_rsv(fs_info);
4597}
4598
4599static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4600{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004601 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4602 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004603 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4604 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4605 WARN_ON(fs_info->trans_block_rsv.size > 0);
4606 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4607 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4608 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004609 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4610 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004611}
4612
Yan, Zhenga22285a2010-05-16 10:48:46 -04004613void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4614 struct btrfs_root *root)
4615{
Josef Bacik0e721102012-06-26 16:13:18 -04004616 if (!trans->block_rsv)
4617 return;
4618
Yan, Zhenga22285a2010-05-16 10:48:46 -04004619 if (!trans->bytes_reserved)
4620 return;
4621
Chris Masone77266e2012-02-24 10:39:05 -05004622 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004623 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004624 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004625 trans->bytes_reserved = 0;
4626}
4627
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004628/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004629int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4630 struct inode *inode)
4631{
4632 struct btrfs_root *root = BTRFS_I(inode)->root;
4633 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4634 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4635
4636 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004637 * We need to hold space in order to delete our orphan item once we've
4638 * added it, so this takes the reservation so we can release it later
4639 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004640 */
Chris Masonff5714c2011-05-28 07:00:39 -04004641 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004642 trace_btrfs_space_reservation(root->fs_info, "orphan",
4643 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004644 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4645}
4646
4647void btrfs_orphan_release_metadata(struct inode *inode)
4648{
4649 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004650 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004651 trace_btrfs_space_reservation(root->fs_info, "orphan",
4652 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004653 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4654}
4655
Miao Xied5c12072013-02-28 10:04:33 +00004656/*
4657 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4658 * root: the root of the parent directory
4659 * rsv: block reservation
4660 * items: the number of items that we need do reservation
4661 * qgroup_reserved: used to return the reserved size in qgroup
4662 *
4663 * This function is used to reserve the space for snapshot/subvolume
4664 * creation and deletion. Those operations are different with the
4665 * common file/directory operations, they change two fs/file trees
4666 * and root tree, the number of items that the qgroup reserves is
4667 * different with the free space reservation. So we can not use
4668 * the space reseravtion mechanism in start_transaction().
4669 */
4670int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4671 struct btrfs_block_rsv *rsv,
4672 int items,
4673 u64 *qgroup_reserved)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004674{
Miao Xied5c12072013-02-28 10:04:33 +00004675 u64 num_bytes;
4676 int ret;
4677
4678 if (root->fs_info->quota_enabled) {
4679 /* One for parent inode, two for dir entries */
4680 num_bytes = 3 * root->leafsize;
4681 ret = btrfs_qgroup_reserve(root, num_bytes);
4682 if (ret)
4683 return ret;
4684 } else {
4685 num_bytes = 0;
4686 }
4687
4688 *qgroup_reserved = num_bytes;
4689
4690 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4691 rsv->space_info = __find_space_info(root->fs_info,
4692 BTRFS_BLOCK_GROUP_METADATA);
4693 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4694 BTRFS_RESERVE_FLUSH_ALL);
4695 if (ret) {
4696 if (*qgroup_reserved)
4697 btrfs_qgroup_free(root, *qgroup_reserved);
4698 }
4699
4700 return ret;
4701}
4702
4703void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4704 struct btrfs_block_rsv *rsv,
4705 u64 qgroup_reserved)
4706{
4707 btrfs_block_rsv_release(root, rsv, (u64)-1);
4708 if (qgroup_reserved)
4709 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004710}
4711
Josef Bacik7709cde2011-08-04 10:25:02 -04004712/**
4713 * drop_outstanding_extent - drop an outstanding extent
4714 * @inode: the inode we're dropping the extent for
4715 *
4716 * This is called when we are freeing up an outstanding extent, either called
4717 * after an error or after an extent is written. This will return the number of
4718 * reserved extents that need to be freed. This must be called with
4719 * BTRFS_I(inode)->lock held.
4720 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004721static unsigned drop_outstanding_extent(struct inode *inode)
4722{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004723 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004724 unsigned dropped_extents = 0;
4725
Josef Bacik9e0baf62011-07-15 15:16:44 +00004726 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4727 BTRFS_I(inode)->outstanding_extents--;
4728
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004729 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004730 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4731 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004732 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004733
Josef Bacik9e0baf62011-07-15 15:16:44 +00004734 /*
4735 * If we have more or the same amount of outsanding extents than we have
4736 * reserved then we need to leave the reserved extents count alone.
4737 */
4738 if (BTRFS_I(inode)->outstanding_extents >=
4739 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004740 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004741
4742 dropped_extents = BTRFS_I(inode)->reserved_extents -
4743 BTRFS_I(inode)->outstanding_extents;
4744 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004745 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004746}
4747
Josef Bacik7709cde2011-08-04 10:25:02 -04004748/**
4749 * calc_csum_metadata_size - return the amount of metada space that must be
4750 * reserved/free'd for the given bytes.
4751 * @inode: the inode we're manipulating
4752 * @num_bytes: the number of bytes in question
4753 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4754 *
4755 * This adjusts the number of csum_bytes in the inode and then returns the
4756 * correct amount of metadata that must either be reserved or freed. We
4757 * calculate how many checksums we can fit into one leaf and then divide the
4758 * number of bytes that will need to be checksumed by this value to figure out
4759 * how many checksums will be required. If we are adding bytes then the number
4760 * may go up and we will return the number of additional bytes that must be
4761 * reserved. If it is going down we will return the number of bytes that must
4762 * be freed.
4763 *
4764 * This must be called with BTRFS_I(inode)->lock held.
4765 */
4766static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4767 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004768{
Josef Bacik7709cde2011-08-04 10:25:02 -04004769 struct btrfs_root *root = BTRFS_I(inode)->root;
4770 u64 csum_size;
4771 int num_csums_per_leaf;
4772 int num_csums;
4773 int old_csums;
4774
4775 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4776 BTRFS_I(inode)->csum_bytes == 0)
4777 return 0;
4778
4779 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4780 if (reserve)
4781 BTRFS_I(inode)->csum_bytes += num_bytes;
4782 else
4783 BTRFS_I(inode)->csum_bytes -= num_bytes;
4784 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4785 num_csums_per_leaf = (int)div64_u64(csum_size,
4786 sizeof(struct btrfs_csum_item) +
4787 sizeof(struct btrfs_disk_key));
4788 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4789 num_csums = num_csums + num_csums_per_leaf - 1;
4790 num_csums = num_csums / num_csums_per_leaf;
4791
4792 old_csums = old_csums + num_csums_per_leaf - 1;
4793 old_csums = old_csums / num_csums_per_leaf;
4794
4795 /* No change, no need to reserve more */
4796 if (old_csums == num_csums)
4797 return 0;
4798
4799 if (reserve)
4800 return btrfs_calc_trans_metadata_size(root,
4801 num_csums - old_csums);
4802
4803 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004804}
4805
4806int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4807{
4808 struct btrfs_root *root = BTRFS_I(inode)->root;
4809 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004810 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004811 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004812 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004813 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004814 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004815 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004816 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004817 u64 to_free = 0;
4818 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004819
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004820 /* If we are a free space inode we need to not flush since we will be in
4821 * the middle of a transaction commit. We also don't need the delalloc
4822 * mutex since we won't race with anybody. We need this mostly to make
4823 * lockdep shut its filthy mouth.
4824 */
4825 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004826 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004827 delalloc_lock = false;
4828 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004829
Miao Xie08e007d2012-10-16 11:33:38 +00004830 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4831 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004832 schedule_timeout(1);
4833
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004834 if (delalloc_lock)
4835 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4836
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004837 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004838
Josef Bacik9e0baf62011-07-15 15:16:44 +00004839 spin_lock(&BTRFS_I(inode)->lock);
4840 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004841
Josef Bacik9e0baf62011-07-15 15:16:44 +00004842 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004843 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004844 nr_extents = BTRFS_I(inode)->outstanding_extents -
4845 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004846
4847 /*
4848 * Add an item to reserve for updating the inode when we complete the
4849 * delalloc io.
4850 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004851 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4852 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004853 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004854 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004855 }
4856
4857 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004858 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004859 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004860 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004861
Wang Shilong88e081bf2013-03-01 11:36:01 +00004862 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02004863 ret = btrfs_qgroup_reserve(root, num_bytes +
4864 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004865 if (ret)
4866 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00004867 }
Arne Jansenc5567232011-09-14 15:44:05 +02004868
Wang Shilong88e081bf2013-03-01 11:36:01 +00004869 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4870 if (unlikely(ret)) {
4871 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00004872 btrfs_qgroup_free(root, num_bytes +
4873 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004874 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004875 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004876
Josef Bacik660d3f62011-12-09 11:18:51 -05004877 spin_lock(&BTRFS_I(inode)->lock);
4878 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004879 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4880 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004881 nr_extents--;
4882 }
4883 BTRFS_I(inode)->reserved_extents += nr_extents;
4884 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004885
4886 if (delalloc_lock)
4887 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004888
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004889 if (to_reserve)
4890 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4891 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004892 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4893
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004894 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004895
4896out_fail:
4897 spin_lock(&BTRFS_I(inode)->lock);
4898 dropped = drop_outstanding_extent(inode);
4899 /*
4900 * If the inodes csum_bytes is the same as the original
4901 * csum_bytes then we know we haven't raced with any free()ers
4902 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00004903 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04004904 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00004905 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04004906 } else {
4907 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
4908 u64 bytes;
4909
4910 /*
4911 * This is tricky, but first we need to figure out how much we
4912 * free'd from any free-ers that occured during this
4913 * reservation, so we reset ->csum_bytes to the csum_bytes
4914 * before we dropped our lock, and then call the free for the
4915 * number of bytes that were freed while we were trying our
4916 * reservation.
4917 */
4918 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
4919 BTRFS_I(inode)->csum_bytes = csum_bytes;
4920 to_free = calc_csum_metadata_size(inode, bytes, 0);
4921
4922
4923 /*
4924 * Now we need to see how much we would have freed had we not
4925 * been making this reservation and our ->csum_bytes were not
4926 * artificially inflated.
4927 */
4928 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
4929 bytes = csum_bytes - orig_csum_bytes;
4930 bytes = calc_csum_metadata_size(inode, bytes, 0);
4931
4932 /*
4933 * Now reset ->csum_bytes to what it should be. If bytes is
4934 * more than to_free then we would have free'd more space had we
4935 * not had an artificially high ->csum_bytes, so we need to free
4936 * the remainder. If bytes is the same or less then we don't
4937 * need to do anything, the other free-ers did the correct
4938 * thing.
4939 */
4940 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
4941 if (bytes > to_free)
4942 to_free = bytes - to_free;
4943 else
4944 to_free = 0;
4945 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00004946 spin_unlock(&BTRFS_I(inode)->lock);
4947 if (dropped)
4948 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4949
4950 if (to_free) {
4951 btrfs_block_rsv_release(root, block_rsv, to_free);
4952 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4953 btrfs_ino(inode), to_free, 0);
4954 }
4955 if (delalloc_lock)
4956 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4957 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004958}
4959
Josef Bacik7709cde2011-08-04 10:25:02 -04004960/**
4961 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4962 * @inode: the inode to release the reservation for
4963 * @num_bytes: the number of bytes we're releasing
4964 *
4965 * This will release the metadata reservation for an inode. This can be called
4966 * once we complete IO for a given set of bytes to release their metadata
4967 * reservations.
4968 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004969void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4970{
4971 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004972 u64 to_free = 0;
4973 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004974
4975 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04004976 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004977 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004978
Miao Xie09348562013-02-07 10:12:07 +00004979 if (num_bytes)
4980 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04004981 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00004982 if (dropped > 0)
4983 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004984
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004985 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4986 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02004987 if (root->fs_info->quota_enabled) {
4988 btrfs_qgroup_free(root, num_bytes +
4989 dropped * root->leafsize);
4990 }
4991
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004992 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4993 to_free);
4994}
4995
Josef Bacik7709cde2011-08-04 10:25:02 -04004996/**
4997 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4998 * @inode: inode we're writing to
4999 * @num_bytes: the number of bytes we want to allocate
5000 *
5001 * This will do the following things
5002 *
5003 * o reserve space in the data space info for num_bytes
5004 * o reserve space in the metadata space info based on number of outstanding
5005 * extents and how much csums will be needed
5006 * o add to the inodes ->delalloc_bytes
5007 * o add it to the fs_info's delalloc inodes list.
5008 *
5009 * This will return 0 for success and -ENOSPC if there is no space left.
5010 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005011int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5012{
5013 int ret;
5014
5015 ret = btrfs_check_data_free_space(inode, num_bytes);
5016 if (ret)
5017 return ret;
5018
5019 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5020 if (ret) {
5021 btrfs_free_reserved_data_space(inode, num_bytes);
5022 return ret;
5023 }
5024
5025 return 0;
5026}
5027
Josef Bacik7709cde2011-08-04 10:25:02 -04005028/**
5029 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5030 * @inode: inode we're releasing space for
5031 * @num_bytes: the number of bytes we want to free up
5032 *
5033 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5034 * called in the case that we don't need the metadata AND data reservations
5035 * anymore. So if there is an error or we insert an inline extent.
5036 *
5037 * This function will release the metadata space that was not used and will
5038 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5039 * list if there are no delalloc bytes left.
5040 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005041void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5042{
5043 btrfs_delalloc_release_metadata(inode, num_bytes);
5044 btrfs_free_reserved_data_space(inode, num_bytes);
5045}
5046
Liu Boc53d6132012-12-27 09:01:19 +00005047static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005048 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005049{
Josef Bacik0af3d002010-06-21 14:48:16 -04005050 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005051 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005052 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005053 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005054 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005055 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005056
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005057 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005058 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005059 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005060 if (alloc)
5061 old_val += num_bytes;
5062 else
5063 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005064 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005065 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005066
Chris Masond3977122009-01-05 21:25:51 -05005067 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005068 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005069 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005070 return -ENOENT;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005071 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5072 BTRFS_BLOCK_GROUP_RAID1 |
5073 BTRFS_BLOCK_GROUP_RAID10))
5074 factor = 2;
5075 else
5076 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005077 /*
5078 * If this block group has free space cache written out, we
5079 * need to make sure to load it if we are removing space. This
5080 * is because we need the unpinning stage to actually add the
5081 * space back to the block group, otherwise we will leak space.
5082 */
5083 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005084 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005085
Chris Masondb945352007-10-15 16:15:53 -04005086 byte_in_group = bytenr - cache->key.objectid;
5087 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005088
Josef Bacik25179202008-10-29 14:49:05 -04005089 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005090 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005091
Josef Bacik73bc1872011-10-03 14:07:49 -04005092 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005093 cache->disk_cache_state < BTRFS_DC_CLEAR)
5094 cache->disk_cache_state = BTRFS_DC_CLEAR;
5095
Josef Bacik0f9dd462008-09-23 13:14:11 -04005096 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005097 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005098 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005099 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005100 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005101 btrfs_set_block_group_used(&cache->item, old_val);
5102 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005103 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005104 cache->space_info->bytes_used += num_bytes;
5105 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005106 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005107 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005108 } else {
Chris Masondb945352007-10-15 16:15:53 -04005109 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005110 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005111 cache->pinned += num_bytes;
5112 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005113 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005114 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005115 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005116 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005117
Yan, Zhengf0486c62010-05-16 10:46:25 -04005118 set_extent_dirty(info->pinned_extents,
5119 bytenr, bytenr + num_bytes - 1,
5120 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005121 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005122 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005123 total -= num_bytes;
5124 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005125 }
5126 return 0;
5127}
Chris Mason6324fbf2008-03-24 15:01:59 -04005128
Chris Masona061fc82008-05-07 11:43:44 -04005129static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5130{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005131 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005132 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005133
Liu Boa1897fd2012-12-27 09:01:23 +00005134 spin_lock(&root->fs_info->block_group_cache_lock);
5135 bytenr = root->fs_info->first_logical_byte;
5136 spin_unlock(&root->fs_info->block_group_cache_lock);
5137
5138 if (bytenr < (u64)-1)
5139 return bytenr;
5140
Josef Bacik0f9dd462008-09-23 13:14:11 -04005141 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5142 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005143 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005144
Yan Zhengd2fb3432008-12-11 16:30:39 -05005145 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005146 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005147
5148 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005149}
5150
Yan, Zhengf0486c62010-05-16 10:46:25 -04005151static int pin_down_extent(struct btrfs_root *root,
5152 struct btrfs_block_group_cache *cache,
5153 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005154{
Yan Zheng11833d62009-09-11 16:11:19 -04005155 spin_lock(&cache->space_info->lock);
5156 spin_lock(&cache->lock);
5157 cache->pinned += num_bytes;
5158 cache->space_info->bytes_pinned += num_bytes;
5159 if (reserved) {
5160 cache->reserved -= num_bytes;
5161 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005162 }
Yan Zheng11833d62009-09-11 16:11:19 -04005163 spin_unlock(&cache->lock);
5164 spin_unlock(&cache->space_info->lock);
5165
Yan, Zhengf0486c62010-05-16 10:46:25 -04005166 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5167 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005168 return 0;
5169}
Chris Mason9078a3e2007-04-26 16:46:15 -04005170
Yan, Zhengf0486c62010-05-16 10:46:25 -04005171/*
5172 * this function must be called within transaction
5173 */
5174int btrfs_pin_extent(struct btrfs_root *root,
5175 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005176{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005177 struct btrfs_block_group_cache *cache;
5178
5179 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005180 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005181
5182 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5183
5184 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005185 return 0;
5186}
Zheng Yane8569812008-09-26 10:05:48 -04005187
Yan, Zhengf0486c62010-05-16 10:46:25 -04005188/*
Chris Masone688b7252011-10-31 20:52:39 -04005189 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005190 */
Liu Bodcfac412012-12-27 09:01:20 +00005191int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005192 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005193{
Chris Masone688b7252011-10-31 20:52:39 -04005194 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005195 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04005196
5197 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005198 if (!cache)
5199 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04005200
5201 /*
5202 * pull in the free space cache (if any) so that our pin
5203 * removes the free space from the cache. We have load_only set
5204 * to one because the slow code to read in the free extents does check
5205 * the pinned extents.
5206 */
Liu Bof6373bf2012-12-27 09:01:18 +00005207 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005208
5209 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5210
5211 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005212 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04005213 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005214 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04005215}
5216
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005217static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5218{
5219 int ret;
5220 struct btrfs_block_group_cache *block_group;
5221 struct btrfs_caching_control *caching_ctl;
5222
5223 block_group = btrfs_lookup_block_group(root->fs_info, start);
5224 if (!block_group)
5225 return -EINVAL;
5226
5227 cache_block_group(block_group, 0);
5228 caching_ctl = get_caching_control(block_group);
5229
5230 if (!caching_ctl) {
5231 /* Logic error */
5232 BUG_ON(!block_group_cache_done(block_group));
5233 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5234 } else {
5235 mutex_lock(&caching_ctl->mutex);
5236
5237 if (start >= caching_ctl->progress) {
5238 ret = add_excluded_extent(root, start, num_bytes);
5239 } else if (start + num_bytes <= caching_ctl->progress) {
5240 ret = btrfs_remove_free_space(block_group,
5241 start, num_bytes);
5242 } else {
5243 num_bytes = caching_ctl->progress - start;
5244 ret = btrfs_remove_free_space(block_group,
5245 start, num_bytes);
5246 if (ret)
5247 goto out_lock;
5248
5249 num_bytes = (start + num_bytes) -
5250 caching_ctl->progress;
5251 start = caching_ctl->progress;
5252 ret = add_excluded_extent(root, start, num_bytes);
5253 }
5254out_lock:
5255 mutex_unlock(&caching_ctl->mutex);
5256 put_caching_control(caching_ctl);
5257 }
5258 btrfs_put_block_group(block_group);
5259 return ret;
5260}
5261
5262int btrfs_exclude_logged_extents(struct btrfs_root *log,
5263 struct extent_buffer *eb)
5264{
5265 struct btrfs_file_extent_item *item;
5266 struct btrfs_key key;
5267 int found_type;
5268 int i;
5269
5270 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5271 return 0;
5272
5273 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5274 btrfs_item_key_to_cpu(eb, &key, i);
5275 if (key.type != BTRFS_EXTENT_DATA_KEY)
5276 continue;
5277 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5278 found_type = btrfs_file_extent_type(eb, item);
5279 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5280 continue;
5281 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5282 continue;
5283 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5284 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5285 __exclude_logged_extent(log, key.objectid, key.offset);
5286 }
5287
5288 return 0;
5289}
5290
Josef Bacikfb25e912011-07-26 17:00:46 -04005291/**
5292 * btrfs_update_reserved_bytes - update the block_group and space info counters
5293 * @cache: The cache we are manipulating
5294 * @num_bytes: The number of bytes in question
5295 * @reserve: One of the reservation enums
5296 *
5297 * This is called by the allocator when it reserves space, or by somebody who is
5298 * freeing space that was never actually used on disk. For example if you
5299 * reserve some space for a new leaf in transaction A and before transaction A
5300 * commits you free that leaf, you call this with reserve set to 0 in order to
5301 * clear the reservation.
5302 *
5303 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5304 * ENOSPC accounting. For data we handle the reservation through clearing the
5305 * delalloc bits in the io_tree. We have to do this since we could end up
5306 * allocating less disk space for the amount of data we have reserved in the
5307 * case of compression.
5308 *
5309 * If this is a reservation and the block group has become read only we cannot
5310 * make the reservation and return -EAGAIN, otherwise this function always
5311 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005312 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005313static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5314 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005315{
Josef Bacikfb25e912011-07-26 17:00:46 -04005316 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005317 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005318
Josef Bacikfb25e912011-07-26 17:00:46 -04005319 spin_lock(&space_info->lock);
5320 spin_lock(&cache->lock);
5321 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005322 if (cache->ro) {
5323 ret = -EAGAIN;
5324 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005325 cache->reserved += num_bytes;
5326 space_info->bytes_reserved += num_bytes;
5327 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005328 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005329 "space_info", space_info->flags,
5330 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005331 space_info->bytes_may_use -= num_bytes;
5332 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005333 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005334 } else {
5335 if (cache->ro)
5336 space_info->bytes_readonly += num_bytes;
5337 cache->reserved -= num_bytes;
5338 space_info->bytes_reserved -= num_bytes;
5339 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005340 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005341 spin_unlock(&cache->lock);
5342 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005343 return ret;
5344}
5345
Jeff Mahoney143bede2012-03-01 14:56:26 +01005346void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005347 struct btrfs_root *root)
5348{
5349 struct btrfs_fs_info *fs_info = root->fs_info;
5350 struct btrfs_caching_control *next;
5351 struct btrfs_caching_control *caching_ctl;
5352 struct btrfs_block_group_cache *cache;
5353
5354 down_write(&fs_info->extent_commit_sem);
5355
5356 list_for_each_entry_safe(caching_ctl, next,
5357 &fs_info->caching_block_groups, list) {
5358 cache = caching_ctl->block_group;
5359 if (block_group_cache_done(cache)) {
5360 cache->last_byte_to_unpin = (u64)-1;
5361 list_del_init(&caching_ctl->list);
5362 put_caching_control(caching_ctl);
5363 } else {
5364 cache->last_byte_to_unpin = caching_ctl->progress;
5365 }
5366 }
5367
5368 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5369 fs_info->pinned_extents = &fs_info->freed_extents[1];
5370 else
5371 fs_info->pinned_extents = &fs_info->freed_extents[0];
5372
5373 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005374
5375 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005376}
5377
5378static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5379{
5380 struct btrfs_fs_info *fs_info = root->fs_info;
5381 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005382 struct btrfs_space_info *space_info;
5383 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005384 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005385 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005386
5387 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005388 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005389 if (!cache ||
5390 start >= cache->key.objectid + cache->key.offset) {
5391 if (cache)
5392 btrfs_put_block_group(cache);
5393 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005394 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005395 }
5396
5397 len = cache->key.objectid + cache->key.offset - start;
5398 len = min(len, end + 1 - start);
5399
5400 if (start < cache->last_byte_to_unpin) {
5401 len = min(len, cache->last_byte_to_unpin - start);
5402 btrfs_add_free_space(cache, start, len);
5403 }
Josef Bacik25179202008-10-29 14:49:05 -04005404
Yan, Zhengf0486c62010-05-16 10:46:25 -04005405 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005406 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005407
Josef Bacik7b398f82012-10-22 15:52:28 -04005408 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005409 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005410 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005411 space_info->bytes_pinned -= len;
5412 if (cache->ro) {
5413 space_info->bytes_readonly += len;
5414 readonly = true;
5415 }
Josef Bacik25179202008-10-29 14:49:05 -04005416 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005417 if (!readonly && global_rsv->space_info == space_info) {
5418 spin_lock(&global_rsv->lock);
5419 if (!global_rsv->full) {
5420 len = min(len, global_rsv->size -
5421 global_rsv->reserved);
5422 global_rsv->reserved += len;
5423 space_info->bytes_may_use += len;
5424 if (global_rsv->reserved >= global_rsv->size)
5425 global_rsv->full = 1;
5426 }
5427 spin_unlock(&global_rsv->lock);
5428 }
5429 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005430 }
5431
5432 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005433 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005434 return 0;
5435}
5436
5437int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005438 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005439{
Yan Zheng11833d62009-09-11 16:11:19 -04005440 struct btrfs_fs_info *fs_info = root->fs_info;
5441 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005442 u64 start;
5443 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005444 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005445
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005446 if (trans->aborted)
5447 return 0;
5448
Yan Zheng11833d62009-09-11 16:11:19 -04005449 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5450 unpin = &fs_info->freed_extents[1];
5451 else
5452 unpin = &fs_info->freed_extents[0];
5453
Chris Masond3977122009-01-05 21:25:51 -05005454 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005455 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005456 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005457 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005458 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005459
Li Dongyang5378e602011-03-24 10:24:27 +00005460 if (btrfs_test_opt(root, DISCARD))
5461 ret = btrfs_discard_extent(root, start,
5462 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005463
Chris Mason1a5bc162007-10-15 16:15:26 -04005464 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005465 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005466 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005467 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005468
Chris Masone20d96d2007-03-22 12:13:20 -04005469 return 0;
5470}
5471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005472static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5473 struct btrfs_root *root,
5474 u64 bytenr, u64 num_bytes, u64 parent,
5475 u64 root_objectid, u64 owner_objectid,
5476 u64 owner_offset, int refs_to_drop,
5477 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005478{
Chris Masone2fa7222007-03-12 16:22:34 -04005479 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005480 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005481 struct btrfs_fs_info *info = root->fs_info;
5482 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005483 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005484 struct btrfs_extent_item *ei;
5485 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005486 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005487 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005488 int extent_slot = 0;
5489 int found_extent = 0;
5490 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005491 u32 item_size;
5492 u64 refs;
Josef Bacik3173a182013-03-07 14:22:04 -05005493 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5494 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005495
Chris Mason5caf2a02007-04-02 11:20:42 -04005496 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005497 if (!path)
5498 return -ENOMEM;
5499
Chris Mason3c12ac72008-04-21 12:01:38 -04005500 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005501 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005502
5503 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5504 BUG_ON(!is_data && refs_to_drop != 1);
5505
Josef Bacik3173a182013-03-07 14:22:04 -05005506 if (is_data)
5507 skinny_metadata = 0;
5508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005509 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5510 bytenr, num_bytes, parent,
5511 root_objectid, owner_objectid,
5512 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005513 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005514 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005515 while (extent_slot >= 0) {
5516 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005517 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005518 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005519 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005520 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5521 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005522 found_extent = 1;
5523 break;
5524 }
Josef Bacik3173a182013-03-07 14:22:04 -05005525 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5526 key.offset == owner_objectid) {
5527 found_extent = 1;
5528 break;
5529 }
Chris Mason952fcca2008-02-18 16:33:44 -05005530 if (path->slots[0] - extent_slot > 5)
5531 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005532 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005533 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005534#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5535 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5536 if (found_extent && item_size < sizeof(*ei))
5537 found_extent = 0;
5538#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005539 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005540 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005541 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005542 NULL, refs_to_drop,
5543 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005544 if (ret) {
5545 btrfs_abort_transaction(trans, extent_root, ret);
5546 goto out;
5547 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005548 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005549 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005550
5551 key.objectid = bytenr;
5552 key.type = BTRFS_EXTENT_ITEM_KEY;
5553 key.offset = num_bytes;
5554
Josef Bacik3173a182013-03-07 14:22:04 -05005555 if (!is_data && skinny_metadata) {
5556 key.type = BTRFS_METADATA_ITEM_KEY;
5557 key.offset = owner_objectid;
5558 }
5559
Zheng Yan31840ae2008-09-23 13:14:14 -04005560 ret = btrfs_search_slot(trans, extent_root,
5561 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005562 if (ret > 0 && skinny_metadata && path->slots[0]) {
5563 /*
5564 * Couldn't find our skinny metadata item,
5565 * see if we have ye olde extent item.
5566 */
5567 path->slots[0]--;
5568 btrfs_item_key_to_cpu(path->nodes[0], &key,
5569 path->slots[0]);
5570 if (key.objectid == bytenr &&
5571 key.type == BTRFS_EXTENT_ITEM_KEY &&
5572 key.offset == num_bytes)
5573 ret = 0;
5574 }
5575
5576 if (ret > 0 && skinny_metadata) {
5577 skinny_metadata = false;
5578 key.type = BTRFS_EXTENT_ITEM_KEY;
5579 key.offset = num_bytes;
5580 btrfs_release_path(path);
5581 ret = btrfs_search_slot(trans, extent_root,
5582 &key, path, -1, 1);
5583 }
5584
Josef Bacikf3465ca2008-11-12 14:19:50 -05005585 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005586 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5587 ret, (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005588 if (ret > 0)
5589 btrfs_print_leaf(extent_root,
5590 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005591 }
David Sterba005d6422012-09-18 07:52:32 -06005592 if (ret < 0) {
5593 btrfs_abort_transaction(trans, extent_root, ret);
5594 goto out;
5595 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005596 extent_slot = path->slots[0];
5597 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005598 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005599 btrfs_print_leaf(extent_root, path->nodes[0]);
5600 WARN_ON(1);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005601 btrfs_err(info,
5602 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5603 (unsigned long long)bytenr,
5604 (unsigned long long)parent,
5605 (unsigned long long)root_objectid,
5606 (unsigned long long)owner_objectid,
5607 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005608 } else {
David Sterba005d6422012-09-18 07:52:32 -06005609 btrfs_abort_transaction(trans, extent_root, ret);
5610 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005611 }
Chris Mason5f39d392007-10-15 16:14:19 -04005612
5613 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005614 item_size = btrfs_item_size_nr(leaf, extent_slot);
5615#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5616 if (item_size < sizeof(*ei)) {
5617 BUG_ON(found_extent || extent_slot != path->slots[0]);
5618 ret = convert_extent_item_v0(trans, extent_root, path,
5619 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005620 if (ret < 0) {
5621 btrfs_abort_transaction(trans, extent_root, ret);
5622 goto out;
5623 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005624
David Sterbab3b4aa72011-04-21 01:20:15 +02005625 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005626 path->leave_spinning = 1;
5627
5628 key.objectid = bytenr;
5629 key.type = BTRFS_EXTENT_ITEM_KEY;
5630 key.offset = num_bytes;
5631
5632 ret = btrfs_search_slot(trans, extent_root, &key, path,
5633 -1, 1);
5634 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005635 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5636 ret, (unsigned long long)bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005637 btrfs_print_leaf(extent_root, path->nodes[0]);
5638 }
David Sterba005d6422012-09-18 07:52:32 -06005639 if (ret < 0) {
5640 btrfs_abort_transaction(trans, extent_root, ret);
5641 goto out;
5642 }
5643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005644 extent_slot = path->slots[0];
5645 leaf = path->nodes[0];
5646 item_size = btrfs_item_size_nr(leaf, extent_slot);
5647 }
5648#endif
5649 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005650 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005651 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005652 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5653 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005654 struct btrfs_tree_block_info *bi;
5655 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5656 bi = (struct btrfs_tree_block_info *)(ei + 1);
5657 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005658 }
5659
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005660 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04005661 if (refs < refs_to_drop) {
5662 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
5663 "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
5664 ret = -EINVAL;
5665 btrfs_abort_transaction(trans, extent_root, ret);
5666 goto out;
5667 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005668 refs -= refs_to_drop;
5669
5670 if (refs > 0) {
5671 if (extent_op)
5672 __run_delayed_extent_op(extent_op, leaf, ei);
5673 /*
5674 * In the case of inline back ref, reference count will
5675 * be updated by remove_extent_backref
5676 */
5677 if (iref) {
5678 BUG_ON(!found_extent);
5679 } else {
5680 btrfs_set_extent_refs(leaf, ei, refs);
5681 btrfs_mark_buffer_dirty(leaf);
5682 }
5683 if (found_extent) {
5684 ret = remove_extent_backref(trans, extent_root, path,
5685 iref, refs_to_drop,
5686 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005687 if (ret) {
5688 btrfs_abort_transaction(trans, extent_root, ret);
5689 goto out;
5690 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005691 }
5692 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005693 if (found_extent) {
5694 BUG_ON(is_data && refs_to_drop !=
5695 extent_data_ref_count(root, path, iref));
5696 if (iref) {
5697 BUG_ON(path->slots[0] != extent_slot);
5698 } else {
5699 BUG_ON(path->slots[0] != extent_slot + 1);
5700 path->slots[0] = extent_slot;
5701 num_to_del = 2;
5702 }
Chris Mason78fae272007-03-25 11:35:08 -04005703 }
Chris Masonb9473432009-03-13 11:00:37 -04005704
Chris Mason952fcca2008-02-18 16:33:44 -05005705 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5706 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005707 if (ret) {
5708 btrfs_abort_transaction(trans, extent_root, ret);
5709 goto out;
5710 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005711 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005712
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005713 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005714 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005715 if (ret) {
5716 btrfs_abort_transaction(trans, extent_root, ret);
5717 goto out;
5718 }
Chris Mason459931e2008-12-10 09:10:46 -05005719 }
5720
Liu Boc53d6132012-12-27 09:01:19 +00005721 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005722 if (ret) {
5723 btrfs_abort_transaction(trans, extent_root, ret);
5724 goto out;
5725 }
Chris Masona28ec192007-03-06 20:08:01 -05005726 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005727out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005728 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005729 return ret;
5730}
5731
5732/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005733 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005734 * delayed ref for that extent as well. This searches the delayed ref tree for
5735 * a given extent, and if there are no other delayed refs to be processed, it
5736 * removes it from the tree.
5737 */
5738static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5739 struct btrfs_root *root, u64 bytenr)
5740{
5741 struct btrfs_delayed_ref_head *head;
5742 struct btrfs_delayed_ref_root *delayed_refs;
5743 struct btrfs_delayed_ref_node *ref;
5744 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005745 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005746
5747 delayed_refs = &trans->transaction->delayed_refs;
5748 spin_lock(&delayed_refs->lock);
5749 head = btrfs_find_delayed_ref_head(trans, bytenr);
5750 if (!head)
5751 goto out;
5752
5753 node = rb_prev(&head->node.rb_node);
5754 if (!node)
5755 goto out;
5756
5757 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5758
5759 /* there are still entries for this ref, we can't drop it */
5760 if (ref->bytenr == bytenr)
5761 goto out;
5762
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005763 if (head->extent_op) {
5764 if (!head->must_insert_reserved)
5765 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005766 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005767 head->extent_op = NULL;
5768 }
5769
Chris Mason1887be62009-03-13 10:11:24 -04005770 /*
5771 * waiting for the lock here would deadlock. If someone else has it
5772 * locked they are already in the process of dropping it anyway
5773 */
5774 if (!mutex_trylock(&head->mutex))
5775 goto out;
5776
5777 /*
5778 * at this point we have a head with no other entries. Go
5779 * ahead and process it.
5780 */
5781 head->node.in_tree = 0;
5782 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005783
Chris Mason1887be62009-03-13 10:11:24 -04005784 delayed_refs->num_entries--;
5785
5786 /*
5787 * we don't take a ref on the node because we're removing it from the
5788 * tree, so we just steal the ref the tree was holding.
5789 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005790 delayed_refs->num_heads--;
5791 if (list_empty(&head->cluster))
5792 delayed_refs->num_heads_ready--;
5793
5794 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005795 spin_unlock(&delayed_refs->lock);
5796
Yan, Zhengf0486c62010-05-16 10:46:25 -04005797 BUG_ON(head->extent_op);
5798 if (head->must_insert_reserved)
5799 ret = 1;
5800
5801 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005802 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005803 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005804out:
5805 spin_unlock(&delayed_refs->lock);
5806 return 0;
5807}
5808
Yan, Zhengf0486c62010-05-16 10:46:25 -04005809void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5810 struct btrfs_root *root,
5811 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005812 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005813{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005814 struct btrfs_block_group_cache *cache = NULL;
5815 int ret;
5816
5817 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005818 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5819 buf->start, buf->len,
5820 parent, root->root_key.objectid,
5821 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005822 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005823 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005824 }
5825
5826 if (!last_ref)
5827 return;
5828
Yan, Zhengf0486c62010-05-16 10:46:25 -04005829 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005830
5831 if (btrfs_header_generation(buf) == trans->transid) {
5832 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5833 ret = check_ref_cleanup(trans, root, buf->start);
5834 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005835 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005836 }
5837
5838 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5839 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005840 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005841 }
5842
5843 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5844
5845 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005846 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005847 }
5848out:
Josef Bacika826d6d2011-03-16 13:42:43 -04005849 /*
5850 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5851 * anymore.
5852 */
5853 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005854 btrfs_put_block_group(cache);
5855}
5856
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005857/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005858int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5859 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5860 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005861{
5862 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005863 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005864
Chris Mason56bec292009-03-13 10:10:06 -04005865 /*
5866 * tree log blocks never actually go into the extent allocation
5867 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005868 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005869 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5870 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005871 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005872 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005873 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005874 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005875 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
5876 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005877 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005878 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005879 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005880 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
5881 num_bytes,
5882 parent, root_objectid, owner,
5883 offset, BTRFS_DROP_DELAYED_REF,
5884 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04005885 }
Chris Mason925baed2008-06-25 16:01:30 -04005886 return ret;
5887}
5888
David Woodhouse53b381b2013-01-29 18:40:14 -05005889static u64 stripe_align(struct btrfs_root *root,
5890 struct btrfs_block_group_cache *cache,
5891 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05005892{
Qu Wenruofda28322013-02-26 08:10:22 +00005893 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05005894 return ret;
5895}
5896
Chris Masonfec577f2007-02-26 10:40:21 -05005897/*
Josef Bacik817d52f2009-07-13 21:29:25 -04005898 * when we wait for progress in the block group caching, its because
5899 * our allocation attempt failed at least once. So, we must sleep
5900 * and let some progress happen before we try again.
5901 *
5902 * This function will sleep at least once waiting for new free space to
5903 * show up, and then it will check the block group free space numbers
5904 * for our min num_bytes. Another option is to have it go ahead
5905 * and look in the rbtree for a free extent of a given size, but this
5906 * is a good start.
5907 */
5908static noinline int
5909wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
5910 u64 num_bytes)
5911{
Yan Zheng11833d62009-09-11 16:11:19 -04005912 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04005913
Yan Zheng11833d62009-09-11 16:11:19 -04005914 caching_ctl = get_caching_control(cache);
5915 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04005916 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04005917
Yan Zheng11833d62009-09-11 16:11:19 -04005918 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08005919 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04005920
5921 put_caching_control(caching_ctl);
5922 return 0;
5923}
5924
5925static noinline int
5926wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5927{
5928 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04005929
5930 caching_ctl = get_caching_control(cache);
5931 if (!caching_ctl)
5932 return 0;
5933
5934 wait_event(caching_ctl->wait, block_group_cache_done(cache));
5935
5936 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04005937 return 0;
5938}
5939
Liu Bo31e50222012-11-21 14:18:10 +00005940int __get_raid_index(u64 flags)
Yan, Zhengb742bb822010-05-16 10:46:24 -04005941{
Ilya Dryomov7738a532012-03-27 17:09:17 +03005942 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00005943 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005944 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00005945 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005946 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00005947 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005948 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00005949 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005950 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05005951 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05005952 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05005953 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03005954
Chris Masone942f882013-02-20 14:06:05 -05005955 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb822010-05-16 10:46:24 -04005956}
5957
Ilya Dryomov7738a532012-03-27 17:09:17 +03005958static int get_block_group_index(struct btrfs_block_group_cache *cache)
5959{
Liu Bo31e50222012-11-21 14:18:10 +00005960 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03005961}
5962
Josef Bacik817d52f2009-07-13 21:29:25 -04005963enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05005964 LOOP_CACHING_NOWAIT = 0,
5965 LOOP_CACHING_WAIT = 1,
5966 LOOP_ALLOC_CHUNK = 2,
5967 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04005968};
5969
5970/*
Chris Masonfec577f2007-02-26 10:40:21 -05005971 * walks the btree of allocated extents and find a hole of a given size.
5972 * The key ins is changed to record the hole:
5973 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04005974 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05005975 * ins->offset == number of blocks
5976 * Any available blocks before search_start are skipped.
5977 */
Chris Masond3977122009-01-05 21:25:51 -05005978static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05005979 struct btrfs_root *orig_root,
5980 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05005981 u64 hint_byte, struct btrfs_key *ins,
David Sterbab6919a52013-04-29 13:39:40 +00005982 u64 flags)
Chris Masonfec577f2007-02-26 10:40:21 -05005983{
Josef Bacik80eb2342008-10-29 14:49:05 -04005984 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05005985 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005986 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005987 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005988 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05005989 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04005990 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04005991 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005992 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00005993 int index = __get_raid_index(flags);
5994 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04005995 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04005996 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04005997 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005998 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04005999 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006000 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006001
Chris Masondb945352007-10-15 16:15:53 -04006002 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04006003 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04006004 ins->objectid = 0;
6005 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006006
David Sterbab6919a52013-04-29 13:39:40 +00006007 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006008
David Sterbab6919a52013-04-29 13:39:40 +00006009 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006010 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006011 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006012 return -ENOSPC;
6013 }
Josef Bacik2552d172009-04-03 10:14:19 -04006014
Josef Bacik67377732010-09-16 16:19:09 -04006015 /*
6016 * If the space info is for both data and metadata it means we have a
6017 * small filesystem and we can't use the clustering stuff.
6018 */
6019 if (btrfs_mixed_space_info(space_info))
6020 use_cluster = false;
6021
David Sterbab6919a52013-04-29 13:39:40 +00006022 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006023 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05006024 if (!btrfs_test_opt(root, SSD))
6025 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04006026 }
6027
David Sterbab6919a52013-04-29 13:39:40 +00006028 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
Josef Bacik67377732010-09-16 16:19:09 -04006029 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006030 last_ptr = &root->fs_info->data_alloc_cluster;
6031 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006032
Chris Mason239b14b2008-03-24 15:02:07 -04006033 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006034 spin_lock(&last_ptr->lock);
6035 if (last_ptr->block_group)
6036 hint_byte = last_ptr->window_start;
6037 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04006038 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006039
Chris Masona061fc82008-05-07 11:43:44 -04006040 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04006041 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04006042
Josef Bacik817d52f2009-07-13 21:29:25 -04006043 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006044 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006045
Josef Bacik2552d172009-04-03 10:14:19 -04006046 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04006047 block_group = btrfs_lookup_block_group(root->fs_info,
6048 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006049 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04006050 /*
6051 * we don't want to use the block group if it doesn't match our
6052 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05006053 *
6054 * However if we are re-searching with an ideal block group
6055 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006056 */
David Sterbab6919a52013-04-29 13:39:40 +00006057 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006058 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006059 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006060 if (list_empty(&block_group->list) ||
6061 block_group->ro) {
6062 /*
6063 * someone is removing this block group,
6064 * we can't jump into the have_block_group
6065 * target because our list pointers are not
6066 * valid
6067 */
6068 btrfs_put_block_group(block_group);
6069 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006070 } else {
Yan, Zhengb742bb822010-05-16 10:46:24 -04006071 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006072 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006073 }
Josef Bacik2552d172009-04-03 10:14:19 -04006074 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006075 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006076 }
Chris Mason42e70e72008-11-07 18:17:11 -05006077 }
Josef Bacik2552d172009-04-03 10:14:19 -04006078search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006079 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006080 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04006081 list_for_each_entry(block_group, &space_info->block_groups[index],
6082 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04006083 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006084 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05006085
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006086 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00006087 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006088 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006089
Chris Mason83a50de2010-12-13 15:06:46 -05006090 /*
6091 * this can happen if we end up cycling through all the
6092 * raid types, but we want to make sure we only allocate
6093 * for the proper type.
6094 */
David Sterbab6919a52013-04-29 13:39:40 +00006095 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05006096 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6097 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006098 BTRFS_BLOCK_GROUP_RAID5 |
6099 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006100 BTRFS_BLOCK_GROUP_RAID10;
6101
6102 /*
6103 * if they asked for extra copies and this block group
6104 * doesn't provide them, bail. This does allow us to
6105 * fill raid0 from raid1.
6106 */
David Sterbab6919a52013-04-29 13:39:40 +00006107 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05006108 goto loop;
6109 }
6110
Josef Bacik2552d172009-04-03 10:14:19 -04006111have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006112 cached = block_group_cache_done(block_group);
6113 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05006114 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00006115 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006116 BUG_ON(ret < 0);
6117 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006118 }
6119
Josef Bacikea6a4782008-11-20 12:16:16 -05006120 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006121 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006122
Josef Bacik0a243252009-09-11 16:11:20 -04006123 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006124 * Ok we want to try and use the cluster allocator, so
6125 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006126 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006127 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05006128 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006129 /*
6130 * the refill lock keeps out other
6131 * people trying to start a new cluster
6132 */
6133 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006134 used_block_group = last_ptr->block_group;
6135 if (used_block_group != block_group &&
6136 (!used_block_group ||
6137 used_block_group->ro ||
David Sterbab6919a52013-04-29 13:39:40 +00006138 !block_group_bits(used_block_group, flags))) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006139 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006140 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006141 }
Chris Mason44fb5512009-06-04 15:34:51 -04006142
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006143 if (used_block_group != block_group)
6144 btrfs_get_block_group(used_block_group);
6145
6146 offset = btrfs_alloc_from_cluster(used_block_group,
6147 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006148 if (offset) {
6149 /* we have a block, we're done */
6150 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006151 trace_btrfs_reserve_extent_cluster(root,
6152 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006153 goto checks;
6154 }
6155
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006156 WARN_ON(last_ptr->block_group != used_block_group);
6157 if (used_block_group != block_group) {
6158 btrfs_put_block_group(used_block_group);
6159 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006160 }
Chris Mason44fb5512009-06-04 15:34:51 -04006161refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006162 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006163 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6164 * set up a new clusters, so lets just skip it
6165 * and let the allocator find whatever block
6166 * it can find. If we reach this point, we
6167 * will have tried the cluster allocator
6168 * plenty of times and not have found
6169 * anything, so we are likely way too
6170 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006171 * anything.
6172 *
6173 * However, if the cluster is taken from the
6174 * current block group, release the cluster
6175 * first, so that we stand a better chance of
6176 * succeeding in the unclustered
6177 * allocation. */
6178 if (loop >= LOOP_NO_EMPTY_SIZE &&
6179 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006180 spin_unlock(&last_ptr->refill_lock);
6181 goto unclustered_alloc;
6182 }
6183
Chris Masonfa9c0d792009-04-03 09:47:43 -04006184 /*
6185 * this cluster didn't work out, free it and
6186 * start over
6187 */
6188 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6189
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006190 if (loop >= LOOP_NO_EMPTY_SIZE) {
6191 spin_unlock(&last_ptr->refill_lock);
6192 goto unclustered_alloc;
6193 }
6194
Chris Mason8de972b2013-01-04 15:39:43 -05006195 aligned_cluster = max_t(unsigned long,
6196 empty_cluster + empty_size,
6197 block_group->full_stripe_len);
6198
Chris Masonfa9c0d792009-04-03 09:47:43 -04006199 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04006200 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04006201 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05006202 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05006203 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006204 if (ret == 0) {
6205 /*
6206 * now pull our allocation out of this
6207 * cluster
6208 */
6209 offset = btrfs_alloc_from_cluster(block_group,
6210 last_ptr, num_bytes,
6211 search_start);
6212 if (offset) {
6213 /* we found one, proceed */
6214 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006215 trace_btrfs_reserve_extent_cluster(root,
6216 block_group, search_start,
6217 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006218 goto checks;
6219 }
Josef Bacik0a243252009-09-11 16:11:20 -04006220 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6221 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006222 spin_unlock(&last_ptr->refill_lock);
6223
Josef Bacik0a243252009-09-11 16:11:20 -04006224 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006225 wait_block_group_cache_progress(block_group,
6226 num_bytes + empty_cluster + empty_size);
6227 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006228 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006229
Chris Masonfa9c0d792009-04-03 09:47:43 -04006230 /*
6231 * at this point we either didn't find a cluster
6232 * or we weren't able to allocate a block from our
6233 * cluster. Free the cluster we've been trying
6234 * to use, and go to the next block group
6235 */
Josef Bacik0a243252009-09-11 16:11:20 -04006236 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006237 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006238 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006239 }
6240
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006241unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006242 spin_lock(&block_group->free_space_ctl->tree_lock);
6243 if (cached &&
6244 block_group->free_space_ctl->free_space <
6245 num_bytes + empty_cluster + empty_size) {
6246 spin_unlock(&block_group->free_space_ctl->tree_lock);
6247 goto loop;
6248 }
6249 spin_unlock(&block_group->free_space_ctl->tree_lock);
6250
Josef Bacik6226cb02009-04-03 10:14:18 -04006251 offset = btrfs_find_space_for_alloc(block_group, search_start,
6252 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006253 /*
6254 * If we didn't find a chunk, and we haven't failed on this
6255 * block group before, and this block group is in the middle of
6256 * caching and we are ok with waiting, then go ahead and wait
6257 * for progress to be made, and set failed_alloc to true.
6258 *
6259 * If failed_alloc is true then we've already waited on this
6260 * block group once and should move on to the next block group.
6261 */
6262 if (!offset && !failed_alloc && !cached &&
6263 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006264 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006265 num_bytes + empty_size);
6266 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006267 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006268 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006269 if (!cached)
6270 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006271 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006272 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006273checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006274 search_start = stripe_align(root, used_block_group,
6275 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006276
Josef Bacik2552d172009-04-03 10:14:19 -04006277 /* move on to the next group */
6278 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006279 used_block_group->key.objectid + used_block_group->key.offset) {
6280 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006281 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04006282 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006283
Josef Bacik6226cb02009-04-03 10:14:18 -04006284 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006285 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04006286 search_start - offset);
6287 BUG_ON(offset > search_start);
6288
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006289 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006290 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006291 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006292 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006293 goto loop;
6294 }
Yan Zheng11833d62009-09-11 16:11:19 -04006295
Josef Bacik2552d172009-04-03 10:14:19 -04006296 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006297 ins->objectid = search_start;
6298 ins->offset = num_bytes;
6299
Josef Bacik3f7de032011-11-10 08:29:20 -05006300 trace_btrfs_reserve_extent(orig_root, block_group,
6301 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006302 if (used_block_group != block_group)
6303 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006304 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006305 break;
6306loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006307 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006308 failed_alloc = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04006309 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006310 if (used_block_group != block_group)
6311 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006312 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006313 }
6314 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006315
Miao Xie60d2adb2011-09-09 17:34:35 +08006316 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6317 goto search;
6318
Yan, Zhengb742bb822010-05-16 10:46:24 -04006319 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6320 goto search;
6321
Josef Bacik285ff5a2012-01-13 15:27:45 -05006322 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006323 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6324 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006325 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6326 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6327 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6328 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006329 */
Josef Bacik723bda22011-05-27 16:11:38 -04006330 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04006331 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006332 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006333 if (loop == LOOP_ALLOC_CHUNK) {
David Sterbab6919a52013-04-29 13:39:40 +00006334 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04006335 CHUNK_ALLOC_FORCE);
6336 /*
6337 * Do not bail out on ENOSPC since we
6338 * can do more things.
6339 */
6340 if (ret < 0 && ret != -ENOSPC) {
6341 btrfs_abort_transaction(trans,
6342 root, ret);
6343 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006344 }
Josef Bacik723bda22011-05-27 16:11:38 -04006345 }
6346
6347 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006348 empty_size = 0;
6349 empty_cluster = 0;
6350 }
Chris Mason42e70e72008-11-07 18:17:11 -05006351
Josef Bacik723bda22011-05-27 16:11:38 -04006352 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006353 } else if (!ins->objectid) {
6354 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006355 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006356 ret = 0;
6357 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006358out:
Chris Mason0b86a832008-03-24 15:01:56 -04006359
Chris Mason0f70abe2007-02-28 16:46:22 -05006360 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006361}
Chris Masonec44a352008-04-28 15:29:52 -04006362
Josef Bacik9ed74f22009-09-11 16:12:44 -04006363static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6364 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006365{
6366 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04006367 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006368
Josef Bacik9ed74f22009-09-11 16:12:44 -04006369 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006370 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6371 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006372 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006373 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006374 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006375 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006376 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6377 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006378 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006379 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006380 (unsigned long long)info->bytes_pinned,
6381 (unsigned long long)info->bytes_reserved,
6382 (unsigned long long)info->bytes_may_use,
6383 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006384 spin_unlock(&info->lock);
6385
6386 if (!dump_block_groups)
6387 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006388
Josef Bacik80eb2342008-10-29 14:49:05 -04006389 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04006390again:
6391 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006392 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006393 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006394 (unsigned long long)cache->key.objectid,
6395 (unsigned long long)cache->key.offset,
6396 (unsigned long long)btrfs_block_group_used(&cache->item),
6397 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006398 (unsigned long long)cache->reserved,
6399 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006400 btrfs_dump_free_space(cache, bytes);
6401 spin_unlock(&cache->lock);
6402 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04006403 if (++index < BTRFS_NR_RAID_TYPES)
6404 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006405 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006406}
Zheng Yane8569812008-09-26 10:05:48 -04006407
Yan Zheng11833d62009-09-11 16:11:19 -04006408int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6409 struct btrfs_root *root,
6410 u64 num_bytes, u64 min_alloc_size,
6411 u64 empty_size, u64 hint_byte,
David Sterbab6919a52013-04-29 13:39:40 +00006412 struct btrfs_key *ins, int is_data)
Chris Masonfec577f2007-02-26 10:40:21 -05006413{
Miao Xie9e622d62012-01-26 15:01:12 -05006414 bool final_tried = false;
David Sterbab6919a52013-04-29 13:39:40 +00006415 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05006416 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006417
David Sterbab6919a52013-04-29 13:39:40 +00006418 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04006419again:
Chris Masondb945352007-10-15 16:15:53 -04006420 WARN_ON(num_bytes < root->sectorsize);
6421 ret = find_free_extent(trans, root, num_bytes, empty_size,
David Sterbab6919a52013-04-29 13:39:40 +00006422 hint_byte, ins, flags);
Chris Mason3b951512008-04-17 11:29:12 -04006423
Miao Xie9e622d62012-01-26 15:01:12 -05006424 if (ret == -ENOSPC) {
6425 if (!final_tried) {
6426 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006427 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006428 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006429 if (num_bytes == min_alloc_size)
6430 final_tried = true;
6431 goto again;
6432 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6433 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006434
David Sterbab6919a52013-04-29 13:39:40 +00006435 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006436 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
David Sterbab6919a52013-04-29 13:39:40 +00006437 (unsigned long long)flags,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006438 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006439 if (sinfo)
6440 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006441 }
Chris Mason925baed2008-06-25 16:01:30 -04006442 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006443
liubo1abe9b82011-03-24 11:18:59 +00006444 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6445
Josef Bacik0f9dd462008-09-23 13:14:11 -04006446 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006447}
6448
Chris Masone688b7252011-10-31 20:52:39 -04006449static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6450 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006451{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006452 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006453 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006454
Josef Bacik0f9dd462008-09-23 13:14:11 -04006455 cache = btrfs_lookup_block_group(root->fs_info, start);
6456 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006457 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6458 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006459 return -ENOSPC;
6460 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006461
Li Dongyang5378e602011-03-24 10:24:27 +00006462 if (btrfs_test_opt(root, DISCARD))
6463 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006464
Chris Masone688b7252011-10-31 20:52:39 -04006465 if (pin)
6466 pin_down_extent(root, cache, start, len, 1);
6467 else {
6468 btrfs_add_free_space(cache, start, len);
6469 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6470 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006471 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006472
liubo1abe9b82011-03-24 11:18:59 +00006473 trace_btrfs_reserved_extent_free(root, start, len);
6474
Chris Masone6dcd2d2008-07-17 12:53:50 -04006475 return ret;
6476}
6477
Chris Masone688b7252011-10-31 20:52:39 -04006478int btrfs_free_reserved_extent(struct btrfs_root *root,
6479 u64 start, u64 len)
6480{
6481 return __btrfs_free_reserved_extent(root, start, len, 0);
6482}
6483
6484int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6485 u64 start, u64 len)
6486{
6487 return __btrfs_free_reserved_extent(root, start, len, 1);
6488}
6489
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006490static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6491 struct btrfs_root *root,
6492 u64 parent, u64 root_objectid,
6493 u64 flags, u64 owner, u64 offset,
6494 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006495{
6496 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006497 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006498 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006499 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006500 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006501 struct extent_buffer *leaf;
6502 int type;
6503 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006504
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006505 if (parent > 0)
6506 type = BTRFS_SHARED_DATA_REF_KEY;
6507 else
6508 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006509
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006510 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006511
6512 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006513 if (!path)
6514 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006515
Chris Masonb9473432009-03-13 11:00:37 -04006516 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006517 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6518 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006519 if (ret) {
6520 btrfs_free_path(path);
6521 return ret;
6522 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006524 leaf = path->nodes[0];
6525 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006526 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006527 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6528 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6529 btrfs_set_extent_flags(leaf, extent_item,
6530 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006531
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006532 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6533 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6534 if (parent > 0) {
6535 struct btrfs_shared_data_ref *ref;
6536 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6537 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6538 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6539 } else {
6540 struct btrfs_extent_data_ref *ref;
6541 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6542 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6543 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6544 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6545 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6546 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006547
6548 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006549 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006550
Liu Boc53d6132012-12-27 09:01:19 +00006551 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006552 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006553 btrfs_err(fs_info, "update block group failed for %llu %llu",
6554 (unsigned long long)ins->objectid,
6555 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006556 BUG();
6557 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006558 return ret;
6559}
6560
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006561static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6562 struct btrfs_root *root,
6563 u64 parent, u64 root_objectid,
6564 u64 flags, struct btrfs_disk_key *key,
6565 int level, struct btrfs_key *ins)
6566{
6567 int ret;
6568 struct btrfs_fs_info *fs_info = root->fs_info;
6569 struct btrfs_extent_item *extent_item;
6570 struct btrfs_tree_block_info *block_info;
6571 struct btrfs_extent_inline_ref *iref;
6572 struct btrfs_path *path;
6573 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006574 u32 size = sizeof(*extent_item) + sizeof(*iref);
6575 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6576 SKINNY_METADATA);
6577
6578 if (!skinny_metadata)
6579 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006580
6581 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006582 if (!path)
6583 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006584
6585 path->leave_spinning = 1;
6586 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6587 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006588 if (ret) {
6589 btrfs_free_path(path);
6590 return ret;
6591 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006592
6593 leaf = path->nodes[0];
6594 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6595 struct btrfs_extent_item);
6596 btrfs_set_extent_refs(leaf, extent_item, 1);
6597 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6598 btrfs_set_extent_flags(leaf, extent_item,
6599 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006600
Josef Bacik3173a182013-03-07 14:22:04 -05006601 if (skinny_metadata) {
6602 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6603 } else {
6604 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6605 btrfs_set_tree_block_key(leaf, block_info, key);
6606 btrfs_set_tree_block_level(leaf, block_info, level);
6607 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6608 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006609
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006610 if (parent > 0) {
6611 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6612 btrfs_set_extent_inline_ref_type(leaf, iref,
6613 BTRFS_SHARED_BLOCK_REF_KEY);
6614 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6615 } else {
6616 btrfs_set_extent_inline_ref_type(leaf, iref,
6617 BTRFS_TREE_BLOCK_REF_KEY);
6618 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6619 }
6620
6621 btrfs_mark_buffer_dirty(leaf);
6622 btrfs_free_path(path);
6623
Josef Bacik3173a182013-03-07 14:22:04 -05006624 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006625 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006626 btrfs_err(fs_info, "update block group failed for %llu %llu",
6627 (unsigned long long)ins->objectid,
6628 (unsigned long long)ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006629 BUG();
6630 }
6631 return ret;
6632}
6633
6634int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6635 struct btrfs_root *root,
6636 u64 root_objectid, u64 owner,
6637 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006638{
6639 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04006640
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006641 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006642
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006643 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6644 ins->offset, 0,
6645 root_objectid, owner, offset,
6646 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006647 return ret;
6648}
Chris Masone02119d2008-09-05 16:13:11 -04006649
6650/*
6651 * this is used by the tree logging recovery code. It records that
6652 * an extent has been allocated and makes sure to clear the free
6653 * space cache bits as well
6654 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006655int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6656 struct btrfs_root *root,
6657 u64 root_objectid, u64 owner, u64 offset,
6658 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006659{
6660 int ret;
6661 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006662
6663 /*
6664 * Mixed block groups will exclude before processing the log so we only
6665 * need to do the exlude dance if this fs isn't mixed.
6666 */
6667 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
6668 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
6669 if (ret)
6670 return ret;
6671 }
Chris Masone02119d2008-09-05 16:13:11 -04006672
Chris Masone02119d2008-09-05 16:13:11 -04006673 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006674 if (!block_group)
6675 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04006676
Josef Bacikfb25e912011-07-26 17:00:46 -04006677 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6678 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006679 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006680 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6681 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006682 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006683 return ret;
6684}
6685
Eric Sandeen48a3b632013-04-25 20:41:01 +00006686static struct extent_buffer *
6687btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6688 u64 bytenr, u32 blocksize, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006689{
6690 struct extent_buffer *buf;
6691
6692 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6693 if (!buf)
6694 return ERR_PTR(-ENOMEM);
6695 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006696 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006697 btrfs_tree_lock(buf);
6698 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006699 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006700
6701 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006702 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006703
Chris Masond0c803c2008-09-11 16:17:57 -04006704 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006705 /*
6706 * we allow two log transactions at a time, use different
6707 * EXENT bit to differentiate dirty pages.
6708 */
6709 if (root->log_transid % 2 == 0)
6710 set_extent_dirty(&root->dirty_log_pages, buf->start,
6711 buf->start + buf->len - 1, GFP_NOFS);
6712 else
6713 set_extent_new(&root->dirty_log_pages, buf->start,
6714 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006715 } else {
6716 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6717 buf->start + buf->len - 1, GFP_NOFS);
6718 }
Chris Mason65b51a02008-08-01 15:11:20 -04006719 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006720 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006721 return buf;
6722}
6723
Yan, Zhengf0486c62010-05-16 10:46:25 -04006724static struct btrfs_block_rsv *
6725use_block_rsv(struct btrfs_trans_handle *trans,
6726 struct btrfs_root *root, u32 blocksize)
6727{
6728 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006729 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006730 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00006731 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006732
6733 block_rsv = get_block_rsv(trans, root);
6734
Miao Xieb586b322013-05-13 13:55:10 +00006735 if (unlikely(block_rsv->size == 0))
6736 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00006737again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04006738 ret = block_rsv_use_bytes(block_rsv, blocksize);
6739 if (!ret)
6740 return block_rsv;
6741
Miao Xieb586b322013-05-13 13:55:10 +00006742 if (block_rsv->failfast)
6743 return ERR_PTR(ret);
6744
Miao Xied88033d2013-05-13 13:55:12 +00006745 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
6746 global_updated = true;
6747 update_global_block_rsv(root->fs_info);
6748 goto again;
6749 }
6750
Miao Xieb586b322013-05-13 13:55:10 +00006751 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6752 static DEFINE_RATELIMIT_STATE(_rs,
6753 DEFAULT_RATELIMIT_INTERVAL * 10,
6754 /*DEFAULT_RATELIMIT_BURST*/ 1);
6755 if (__ratelimit(&_rs))
6756 WARN(1, KERN_DEBUG
6757 "btrfs: block rsv returned %d\n", ret);
6758 }
6759try_reserve:
6760 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6761 BTRFS_RESERVE_NO_FLUSH);
6762 if (!ret)
6763 return block_rsv;
6764 /*
6765 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00006766 * the global reserve if its space type is the same as the global
6767 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00006768 */
Miao Xie5881cfc2013-05-13 13:55:11 +00006769 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
6770 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00006771 ret = block_rsv_use_bytes(global_rsv, blocksize);
6772 if (!ret)
6773 return global_rsv;
6774 }
6775 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006776}
6777
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006778static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6779 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006780{
6781 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006782 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006783}
6784
Chris Masonfec577f2007-02-26 10:40:21 -05006785/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006786 * finds a free extent and does all the dirty work required for allocation
6787 * returns the key for the extent through ins, and a tree buffer for
6788 * the first block of the extent through buf.
6789 *
Chris Masonfec577f2007-02-26 10:40:21 -05006790 * returns the tree buffer or NULL.
6791 */
Chris Mason5f39d392007-10-15 16:14:19 -04006792struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006793 struct btrfs_root *root, u32 blocksize,
6794 u64 parent, u64 root_objectid,
6795 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006796 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006797{
Chris Masone2fa7222007-03-12 16:22:34 -04006798 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006799 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006800 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006801 u64 flags = 0;
6802 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05006803 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6804 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006805
6806 block_rsv = use_block_rsv(trans, root, blocksize);
6807 if (IS_ERR(block_rsv))
6808 return ERR_CAST(block_rsv);
6809
6810 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006811 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006812 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006813 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006814 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006815 }
Chris Mason55c69072008-01-09 15:55:33 -05006816
Chris Mason4008c042009-02-12 14:09:45 -05006817 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6818 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006819 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006820
6821 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6822 if (parent == 0)
6823 parent = ins.objectid;
6824 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6825 } else
6826 BUG_ON(parent > 0);
6827
6828 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6829 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006830 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006831 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006832 if (key)
6833 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6834 else
6835 memset(&extent_op->key, 0, sizeof(extent_op->key));
6836 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05006837 if (skinny_metadata)
6838 extent_op->update_key = 0;
6839 else
6840 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006841 extent_op->update_flags = 1;
6842 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04006843 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006844
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006845 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6846 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006847 ins.offset, parent, root_objectid,
6848 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006849 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006850 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006851 }
Chris Masonfec577f2007-02-26 10:40:21 -05006852 return buf;
6853}
Chris Masona28ec192007-03-06 20:08:01 -05006854
Yan Zheng2c47e6052009-06-27 21:07:35 -04006855struct walk_control {
6856 u64 refs[BTRFS_MAX_LEVEL];
6857 u64 flags[BTRFS_MAX_LEVEL];
6858 struct btrfs_key update_progress;
6859 int stage;
6860 int level;
6861 int shared_level;
6862 int update_ref;
6863 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006864 int reada_slot;
6865 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006866 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006867};
6868
6869#define DROP_REFERENCE 1
6870#define UPDATE_BACKREF 2
6871
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006872static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6873 struct btrfs_root *root,
6874 struct walk_control *wc,
6875 struct btrfs_path *path)
6876{
6877 u64 bytenr;
6878 u64 generation;
6879 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006880 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006881 u32 nritems;
6882 u32 blocksize;
6883 struct btrfs_key key;
6884 struct extent_buffer *eb;
6885 int ret;
6886 int slot;
6887 int nread = 0;
6888
6889 if (path->slots[wc->level] < wc->reada_slot) {
6890 wc->reada_count = wc->reada_count * 2 / 3;
6891 wc->reada_count = max(wc->reada_count, 2);
6892 } else {
6893 wc->reada_count = wc->reada_count * 3 / 2;
6894 wc->reada_count = min_t(int, wc->reada_count,
6895 BTRFS_NODEPTRS_PER_BLOCK(root));
6896 }
6897
6898 eb = path->nodes[wc->level];
6899 nritems = btrfs_header_nritems(eb);
6900 blocksize = btrfs_level_size(root, wc->level - 1);
6901
6902 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
6903 if (nread >= wc->reada_count)
6904 break;
6905
6906 cond_resched();
6907 bytenr = btrfs_node_blockptr(eb, slot);
6908 generation = btrfs_node_ptr_generation(eb, slot);
6909
6910 if (slot == path->slots[wc->level])
6911 goto reada;
6912
6913 if (wc->stage == UPDATE_BACKREF &&
6914 generation <= root->root_key.offset)
6915 continue;
6916
Yan, Zheng94fcca92009-10-09 09:25:16 -04006917 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05006918 ret = btrfs_lookup_extent_info(trans, root, bytenr,
6919 wc->level - 1, 1, &refs,
6920 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006921 /* We don't care about errors in readahead. */
6922 if (ret < 0)
6923 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006924 BUG_ON(refs == 0);
6925
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006926 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006927 if (refs == 1)
6928 goto reada;
6929
Yan, Zheng94fcca92009-10-09 09:25:16 -04006930 if (wc->level == 1 &&
6931 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6932 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006933 if (!wc->update_ref ||
6934 generation <= root->root_key.offset)
6935 continue;
6936 btrfs_node_key_to_cpu(eb, &key, slot);
6937 ret = btrfs_comp_cpu_keys(&key,
6938 &wc->update_progress);
6939 if (ret < 0)
6940 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006941 } else {
6942 if (wc->level == 1 &&
6943 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6944 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006945 }
6946reada:
6947 ret = readahead_tree_block(root, bytenr, blocksize,
6948 generation);
6949 if (ret)
6950 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006951 nread++;
6952 }
6953 wc->reada_slot = slot;
6954}
6955
Chris Mason9aca1d52007-03-13 11:09:37 -04006956/*
Liu Bo2c016dc2012-12-26 15:32:17 +08006957 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04006958 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006959 * when wc->stage == UPDATE_BACKREF, this function updates
6960 * back refs for pointers in the block.
6961 *
6962 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04006963 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006964static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6965 struct btrfs_root *root,
6966 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006967 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006968{
6969 int level = wc->level;
6970 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006971 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6972 int ret;
6973
6974 if (wc->stage == UPDATE_BACKREF &&
6975 btrfs_header_owner(eb) != root->root_key.objectid)
6976 return 1;
6977
6978 /*
6979 * when reference count of tree block is 1, it won't increase
6980 * again. once full backref flag is set, we never clear it.
6981 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04006982 if (lookup_info &&
6983 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6984 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006985 BUG_ON(!path->locks[level]);
6986 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05006987 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006988 &wc->refs[level],
6989 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006990 BUG_ON(ret == -ENOMEM);
6991 if (ret)
6992 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006993 BUG_ON(wc->refs[level] == 0);
6994 }
6995
Yan Zheng2c47e6052009-06-27 21:07:35 -04006996 if (wc->stage == DROP_REFERENCE) {
6997 if (wc->refs[level] > 1)
6998 return 1;
6999
7000 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04007001 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007002 path->locks[level] = 0;
7003 }
7004 return 0;
7005 }
7006
7007 /* wc->stage == UPDATE_BACKREF */
7008 if (!(wc->flags[level] & flag)) {
7009 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007010 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007011 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007012 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007013 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007014 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04007015 eb->len, flag,
7016 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007017 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007018 wc->flags[level] |= flag;
7019 }
7020
7021 /*
7022 * the block is shared by multiple trees, so it's not good to
7023 * keep the tree lock
7024 */
7025 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04007026 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007027 path->locks[level] = 0;
7028 }
7029 return 0;
7030}
7031
7032/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007033 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007034 *
7035 * when wc->stage == DROP_REFERENCE, this function checks
7036 * reference count of the block pointed to. if the block
7037 * is shared and we need update back refs for the subtree
7038 * rooted at the block, this function changes wc->stage to
7039 * UPDATE_BACKREF. if the block is shared and there is no
7040 * need to update back, this function drops the reference
7041 * to the block.
7042 *
7043 * NOTE: return value 1 means we should stop walking down.
7044 */
7045static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7046 struct btrfs_root *root,
7047 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007048 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007049{
7050 u64 bytenr;
7051 u64 generation;
7052 u64 parent;
7053 u32 blocksize;
7054 struct btrfs_key key;
7055 struct extent_buffer *next;
7056 int level = wc->level;
7057 int reada = 0;
7058 int ret = 0;
7059
7060 generation = btrfs_node_ptr_generation(path->nodes[level],
7061 path->slots[level]);
7062 /*
7063 * if the lower level block was created before the snapshot
7064 * was created, we know there is no need to update back refs
7065 * for the subtree
7066 */
7067 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007068 generation <= root->root_key.offset) {
7069 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007070 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007071 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007072
7073 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7074 blocksize = btrfs_level_size(root, level - 1);
7075
7076 next = btrfs_find_tree_block(root, bytenr, blocksize);
7077 if (!next) {
7078 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00007079 if (!next)
7080 return -ENOMEM;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007081 reada = 1;
7082 }
7083 btrfs_tree_lock(next);
7084 btrfs_set_lock_blocking(next);
7085
Josef Bacik3173a182013-03-07 14:22:04 -05007086 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007087 &wc->refs[level - 1],
7088 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007089 if (ret < 0) {
7090 btrfs_tree_unlock(next);
7091 return ret;
7092 }
7093
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007094 if (unlikely(wc->refs[level - 1] == 0)) {
7095 btrfs_err(root->fs_info, "Missing references.");
7096 BUG();
7097 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007098 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007099
Yan, Zheng94fcca92009-10-09 09:25:16 -04007100 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007101 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007102 if (level == 1 &&
7103 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7104 goto skip;
7105
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007106 if (!wc->update_ref ||
7107 generation <= root->root_key.offset)
7108 goto skip;
7109
7110 btrfs_node_key_to_cpu(path->nodes[level], &key,
7111 path->slots[level]);
7112 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7113 if (ret < 0)
7114 goto skip;
7115
7116 wc->stage = UPDATE_BACKREF;
7117 wc->shared_level = level - 1;
7118 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007119 } else {
7120 if (level == 1 &&
7121 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7122 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007123 }
7124
Chris Masonb9fab912012-05-06 07:23:47 -04007125 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007126 btrfs_tree_unlock(next);
7127 free_extent_buffer(next);
7128 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007129 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007130 }
7131
7132 if (!next) {
7133 if (reada && level == 1)
7134 reada_walk_down(trans, root, wc, path);
7135 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007136 if (!next || !extent_buffer_uptodate(next)) {
7137 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007138 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007139 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007140 btrfs_tree_lock(next);
7141 btrfs_set_lock_blocking(next);
7142 }
7143
7144 level--;
7145 BUG_ON(level != btrfs_header_level(next));
7146 path->nodes[level] = next;
7147 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007148 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007149 wc->level = level;
7150 if (wc->level == 1)
7151 wc->reada_slot = 0;
7152 return 0;
7153skip:
7154 wc->refs[level - 1] = 0;
7155 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007156 if (wc->stage == DROP_REFERENCE) {
7157 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7158 parent = path->nodes[level]->start;
7159 } else {
7160 BUG_ON(root->root_key.objectid !=
7161 btrfs_header_owner(path->nodes[level]));
7162 parent = 0;
7163 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007164
Yan, Zheng94fcca92009-10-09 09:25:16 -04007165 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007166 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007167 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007168 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007169 btrfs_tree_unlock(next);
7170 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007171 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007172 return 1;
7173}
7174
7175/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007176 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007177 *
7178 * when wc->stage == DROP_REFERENCE, this function drops
7179 * reference count on the block.
7180 *
7181 * when wc->stage == UPDATE_BACKREF, this function changes
7182 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7183 * to UPDATE_BACKREF previously while processing the block.
7184 *
7185 * NOTE: return value 1 means we should stop walking up.
7186 */
7187static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7188 struct btrfs_root *root,
7189 struct btrfs_path *path,
7190 struct walk_control *wc)
7191{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007192 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007193 int level = wc->level;
7194 struct extent_buffer *eb = path->nodes[level];
7195 u64 parent = 0;
7196
7197 if (wc->stage == UPDATE_BACKREF) {
7198 BUG_ON(wc->shared_level < level);
7199 if (level < wc->shared_level)
7200 goto out;
7201
Yan Zheng2c47e6052009-06-27 21:07:35 -04007202 ret = find_next_key(path, level + 1, &wc->update_progress);
7203 if (ret > 0)
7204 wc->update_ref = 0;
7205
7206 wc->stage = DROP_REFERENCE;
7207 wc->shared_level = -1;
7208 path->slots[level] = 0;
7209
7210 /*
7211 * check reference count again if the block isn't locked.
7212 * we should start walking down the tree again if reference
7213 * count is one.
7214 */
7215 if (!path->locks[level]) {
7216 BUG_ON(level == 0);
7217 btrfs_tree_lock(eb);
7218 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007219 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007220
7221 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007222 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007223 &wc->refs[level],
7224 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007225 if (ret < 0) {
7226 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007227 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007228 return ret;
7229 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007230 BUG_ON(wc->refs[level] == 0);
7231 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007232 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007233 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007234 return 1;
7235 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007236 }
7237 }
7238
7239 /* wc->stage == DROP_REFERENCE */
7240 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7241
7242 if (wc->refs[level] == 1) {
7243 if (level == 0) {
7244 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007245 ret = btrfs_dec_ref(trans, root, eb, 1,
7246 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007247 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007248 ret = btrfs_dec_ref(trans, root, eb, 0,
7249 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007250 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007251 }
7252 /* make block locked assertion in clean_tree_block happy */
7253 if (!path->locks[level] &&
7254 btrfs_header_generation(eb) == trans->transid) {
7255 btrfs_tree_lock(eb);
7256 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007257 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007258 }
7259 clean_tree_block(trans, root, eb);
7260 }
7261
7262 if (eb == root->node) {
7263 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7264 parent = eb->start;
7265 else
7266 BUG_ON(root->root_key.objectid !=
7267 btrfs_header_owner(eb));
7268 } else {
7269 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7270 parent = path->nodes[level + 1]->start;
7271 else
7272 BUG_ON(root->root_key.objectid !=
7273 btrfs_header_owner(path->nodes[level + 1]));
7274 }
7275
Jan Schmidt5581a512012-05-16 17:04:52 +02007276 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007277out:
7278 wc->refs[level] = 0;
7279 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007280 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007281}
7282
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007283static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7284 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007285 struct btrfs_path *path,
7286 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007287{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007288 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007289 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007290 int ret;
7291
Yan Zheng2c47e6052009-06-27 21:07:35 -04007292 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007293 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007294 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007295 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007296
Yan Zheng2c47e6052009-06-27 21:07:35 -04007297 if (level == 0)
7298 break;
7299
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007300 if (path->slots[level] >=
7301 btrfs_header_nritems(path->nodes[level]))
7302 break;
7303
Yan, Zheng94fcca92009-10-09 09:25:16 -04007304 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007305 if (ret > 0) {
7306 path->slots[level]++;
7307 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00007308 } else if (ret < 0)
7309 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007310 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007311 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007312 return 0;
7313}
7314
Chris Masond3977122009-01-05 21:25:51 -05007315static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007316 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007317 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007318 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007319{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007320 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007321 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007322
Yan Zheng2c47e6052009-06-27 21:07:35 -04007323 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7324 while (level < max_level && path->nodes[level]) {
7325 wc->level = level;
7326 if (path->slots[level] + 1 <
7327 btrfs_header_nritems(path->nodes[level])) {
7328 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007329 return 0;
7330 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007331 ret = walk_up_proc(trans, root, path, wc);
7332 if (ret > 0)
7333 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007334
Yan Zheng2c47e6052009-06-27 21:07:35 -04007335 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007336 btrfs_tree_unlock_rw(path->nodes[level],
7337 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007338 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007339 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007340 free_extent_buffer(path->nodes[level]);
7341 path->nodes[level] = NULL;
7342 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007343 }
7344 }
7345 return 1;
7346}
7347
Chris Mason9aca1d52007-03-13 11:09:37 -04007348/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007349 * drop a subvolume tree.
7350 *
7351 * this function traverses the tree freeing any blocks that only
7352 * referenced by the tree.
7353 *
7354 * when a shared tree block is found. this function decreases its
7355 * reference count by one. if update_ref is true, this function
7356 * also make sure backrefs for the shared block and all lower level
7357 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007358 *
7359 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007360 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007361int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007362 struct btrfs_block_rsv *block_rsv, int update_ref,
7363 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007364{
Chris Mason5caf2a02007-04-02 11:20:42 -04007365 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007366 struct btrfs_trans_handle *trans;
7367 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007368 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007369 struct walk_control *wc;
7370 struct btrfs_key key;
7371 int err = 0;
7372 int ret;
7373 int level;
Chris Mason20524f02007-03-10 06:35:47 -05007374
Chris Mason5caf2a02007-04-02 11:20:42 -04007375 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007376 if (!path) {
7377 err = -ENOMEM;
7378 goto out;
7379 }
Chris Mason20524f02007-03-10 06:35:47 -05007380
Yan Zheng2c47e6052009-06-27 21:07:35 -04007381 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007382 if (!wc) {
7383 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007384 err = -ENOMEM;
7385 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007386 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007387
Yan, Zhenga22285a2010-05-16 10:48:46 -04007388 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007389 if (IS_ERR(trans)) {
7390 err = PTR_ERR(trans);
7391 goto out_free;
7392 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007393
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007394 if (block_rsv)
7395 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007396
Chris Mason9f3a7422007-08-07 15:52:19 -04007397 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007398 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007399 path->nodes[level] = btrfs_lock_root_node(root);
7400 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007401 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007402 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007403 memset(&wc->update_progress, 0,
7404 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007405 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007406 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007407 memcpy(&wc->update_progress, &key,
7408 sizeof(wc->update_progress));
7409
Chris Mason6702ed42007-08-07 16:15:09 -04007410 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007411 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007412 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7414 path->lowest_level = 0;
7415 if (ret < 0) {
7416 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007417 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007418 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007419 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007420
Chris Mason7d9eb122008-07-08 14:19:17 -04007421 /*
7422 * unlock our path, this is safe because only this
7423 * function is allowed to delete this snapshot
7424 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007425 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007426
Yan Zheng2c47e6052009-06-27 21:07:35 -04007427 level = btrfs_header_level(root->node);
7428 while (1) {
7429 btrfs_tree_lock(path->nodes[level]);
7430 btrfs_set_lock_blocking(path->nodes[level]);
7431
7432 ret = btrfs_lookup_extent_info(trans, root,
7433 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007434 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007435 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007436 if (ret < 0) {
7437 err = ret;
7438 goto out_end_trans;
7439 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007440 BUG_ON(wc->refs[level] == 0);
7441
7442 if (level == root_item->drop_level)
7443 break;
7444
7445 btrfs_tree_unlock(path->nodes[level]);
7446 WARN_ON(wc->refs[level] != 1);
7447 level--;
7448 }
7449 }
7450
7451 wc->level = level;
7452 wc->shared_level = -1;
7453 wc->stage = DROP_REFERENCE;
7454 wc->update_ref = update_ref;
7455 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007456 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007457 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007458
7459 while (1) {
Miao Xiebabbf1702013-05-14 10:20:43 +00007460 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007461 pr_debug("btrfs: drop snapshot early exit\n");
7462 err = -EAGAIN;
7463 goto out_end_trans;
7464 }
7465
Yan Zheng2c47e6052009-06-27 21:07:35 -04007466 ret = walk_down_tree(trans, root, path, wc);
7467 if (ret < 0) {
7468 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007469 break;
7470 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007471
7472 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7473 if (ret < 0) {
7474 err = ret;
7475 break;
7476 }
7477
7478 if (ret > 0) {
7479 BUG_ON(wc->stage != DROP_REFERENCE);
7480 break;
7481 }
7482
7483 if (wc->stage == DROP_REFERENCE) {
7484 level = wc->level;
7485 btrfs_node_key(path->nodes[level],
7486 &root_item->drop_progress,
7487 path->slots[level]);
7488 root_item->drop_level = level;
7489 }
7490
7491 BUG_ON(wc->level == 0);
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007492 if (btrfs_should_end_transaction(trans, tree_root)) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007493 ret = btrfs_update_root(trans, tree_root,
7494 &root->root_key,
7495 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007496 if (ret) {
7497 btrfs_abort_transaction(trans, tree_root, ret);
7498 err = ret;
7499 goto out_end_trans;
7500 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007501
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007502 btrfs_end_transaction_throttle(trans, tree_root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007503 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007504 if (IS_ERR(trans)) {
7505 err = PTR_ERR(trans);
7506 goto out_free;
7507 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007508 if (block_rsv)
7509 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007510 }
Chris Mason20524f02007-03-10 06:35:47 -05007511 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007512 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007513 if (err)
7514 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007515
7516 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007517 if (ret) {
7518 btrfs_abort_transaction(trans, tree_root, ret);
7519 goto out_end_trans;
7520 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007521
Yan, Zheng76dda932009-09-21 16:00:26 -04007522 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007523 ret = btrfs_find_root(tree_root, &root->root_key, path,
7524 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007525 if (ret < 0) {
7526 btrfs_abort_transaction(trans, tree_root, ret);
7527 err = ret;
7528 goto out_end_trans;
7529 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007530 /* if we fail to delete the orphan item this time
7531 * around, it'll get picked up the next time.
7532 *
7533 * The most common failure here is just -ENOENT.
7534 */
7535 btrfs_del_orphan_item(trans, tree_root,
7536 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007537 }
7538 }
7539
7540 if (root->in_radix) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007541 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007542 } else {
7543 free_extent_buffer(root->node);
7544 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00007545 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007546 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007547out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007548 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007549out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007550 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007551 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007552out:
7553 if (err)
7554 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007555 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007556}
Chris Mason9078a3e2007-04-26 16:46:15 -04007557
Yan Zheng2c47e6052009-06-27 21:07:35 -04007558/*
7559 * drop subtree rooted at tree block 'node'.
7560 *
7561 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007562 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007563 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007564int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7565 struct btrfs_root *root,
7566 struct extent_buffer *node,
7567 struct extent_buffer *parent)
7568{
7569 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007570 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007571 int level;
7572 int parent_level;
7573 int ret = 0;
7574 int wret;
7575
Yan Zheng2c47e6052009-06-27 21:07:35 -04007576 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7577
Yan Zhengf82d02d2008-10-29 14:49:05 -04007578 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007579 if (!path)
7580 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007581
Yan Zheng2c47e6052009-06-27 21:07:35 -04007582 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007583 if (!wc) {
7584 btrfs_free_path(path);
7585 return -ENOMEM;
7586 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007587
Chris Masonb9447ef82009-03-09 11:45:38 -04007588 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007589 parent_level = btrfs_header_level(parent);
7590 extent_buffer_get(parent);
7591 path->nodes[parent_level] = parent;
7592 path->slots[parent_level] = btrfs_header_nritems(parent);
7593
Chris Masonb9447ef82009-03-09 11:45:38 -04007594 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007595 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007596 path->nodes[level] = node;
7597 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007598 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007599
7600 wc->refs[parent_level] = 1;
7601 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7602 wc->level = level;
7603 wc->shared_level = -1;
7604 wc->stage = DROP_REFERENCE;
7605 wc->update_ref = 0;
7606 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007607 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007608 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007609
7610 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007611 wret = walk_down_tree(trans, root, path, wc);
7612 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007613 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007614 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007615 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007616
Yan Zheng2c47e6052009-06-27 21:07:35 -04007617 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007618 if (wret < 0)
7619 ret = wret;
7620 if (wret != 0)
7621 break;
7622 }
7623
Yan Zheng2c47e6052009-06-27 21:07:35 -04007624 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007625 btrfs_free_path(path);
7626 return ret;
7627}
7628
Chris Masonec44a352008-04-28 15:29:52 -04007629static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7630{
7631 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007632 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007633
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007634 /*
7635 * if restripe for this chunk_type is on pick target profile and
7636 * return, otherwise do the usual balance
7637 */
7638 stripped = get_restripe_target(root->fs_info, flags);
7639 if (stripped)
7640 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007641
Chris Masoncd02dca2010-12-13 14:56:23 -05007642 /*
7643 * we add in the count of missing devices because we want
7644 * to make sure that any RAID levels on a degraded FS
7645 * continue to be honored.
7646 */
7647 num_devices = root->fs_info->fs_devices->rw_devices +
7648 root->fs_info->fs_devices->missing_devices;
7649
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007650 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007651 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007652 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7653
Chris Masonec44a352008-04-28 15:29:52 -04007654 if (num_devices == 1) {
7655 stripped |= BTRFS_BLOCK_GROUP_DUP;
7656 stripped = flags & ~stripped;
7657
7658 /* turn raid0 into single device chunks */
7659 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7660 return stripped;
7661
7662 /* turn mirroring into duplication */
7663 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7664 BTRFS_BLOCK_GROUP_RAID10))
7665 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007666 } else {
7667 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007668 if (flags & stripped)
7669 return flags;
7670
7671 stripped |= BTRFS_BLOCK_GROUP_DUP;
7672 stripped = flags & ~stripped;
7673
7674 /* switch duplicated blocks with raid1 */
7675 if (flags & BTRFS_BLOCK_GROUP_DUP)
7676 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7677
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007678 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007679 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007680
Chris Masonec44a352008-04-28 15:29:52 -04007681 return flags;
7682}
7683
Miao Xie199c36e2011-07-15 10:34:36 +00007684static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007685{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007686 struct btrfs_space_info *sinfo = cache->space_info;
7687 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007688 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007689 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007690
Chris Masonc286ac42008-07-22 23:06:41 -04007691
Miao Xie199c36e2011-07-15 10:34:36 +00007692 /*
7693 * We need some metadata space and system metadata space for
7694 * allocating chunks in some corner cases until we force to set
7695 * it to be readonly.
7696 */
7697 if ((sinfo->flags &
7698 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7699 !force)
7700 min_allocable_bytes = 1 * 1024 * 1024;
7701 else
7702 min_allocable_bytes = 0;
7703
Yan, Zhengf0486c62010-05-16 10:46:25 -04007704 spin_lock(&sinfo->lock);
7705 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007706
7707 if (cache->ro) {
7708 ret = 0;
7709 goto out;
7710 }
7711
Yan, Zhengf0486c62010-05-16 10:46:25 -04007712 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7713 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007714
Yan, Zhengf0486c62010-05-16 10:46:25 -04007715 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007716 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7717 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007718 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007719 cache->ro = 1;
7720 ret = 0;
7721 }
WuBo61cfea92011-07-26 03:30:11 +00007722out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007723 spin_unlock(&cache->lock);
7724 spin_unlock(&sinfo->lock);
7725 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007726}
7727
Yan, Zhengf0486c62010-05-16 10:46:25 -04007728int btrfs_set_block_group_ro(struct btrfs_root *root,
7729 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007730
7731{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007732 struct btrfs_trans_handle *trans;
7733 u64 alloc_flags;
7734 int ret;
7735
7736 BUG_ON(cache->ro);
7737
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007738 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007739 if (IS_ERR(trans))
7740 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007741
7742 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007743 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007744 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007745 CHUNK_ALLOC_FORCE);
7746 if (ret < 0)
7747 goto out;
7748 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007749
Miao Xie199c36e2011-07-15 10:34:36 +00007750 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007751 if (!ret)
7752 goto out;
7753 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007754 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007755 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007756 if (ret < 0)
7757 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007758 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007759out:
7760 btrfs_end_transaction(trans, root);
7761 return ret;
7762}
7763
Chris Masonc87f08c2011-02-16 13:57:04 -05007764int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7765 struct btrfs_root *root, u64 type)
7766{
7767 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007768 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007769 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007770}
7771
Miao Xie6d07bce2011-01-05 10:07:31 +00007772/*
7773 * helper to account the unused space of all the readonly block group in the
7774 * list. takes mirrors into account.
7775 */
7776static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7777{
7778 struct btrfs_block_group_cache *block_group;
7779 u64 free_bytes = 0;
7780 int factor;
7781
7782 list_for_each_entry(block_group, groups_list, list) {
7783 spin_lock(&block_group->lock);
7784
7785 if (!block_group->ro) {
7786 spin_unlock(&block_group->lock);
7787 continue;
7788 }
7789
7790 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7791 BTRFS_BLOCK_GROUP_RAID10 |
7792 BTRFS_BLOCK_GROUP_DUP))
7793 factor = 2;
7794 else
7795 factor = 1;
7796
7797 free_bytes += (block_group->key.offset -
7798 btrfs_block_group_used(&block_group->item)) *
7799 factor;
7800
7801 spin_unlock(&block_group->lock);
7802 }
7803
7804 return free_bytes;
7805}
7806
7807/*
7808 * helper to account the unused space of all the readonly block group in the
7809 * space_info. takes mirrors into account.
7810 */
7811u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7812{
7813 int i;
7814 u64 free_bytes = 0;
7815
7816 spin_lock(&sinfo->lock);
7817
7818 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7819 if (!list_empty(&sinfo->block_groups[i]))
7820 free_bytes += __btrfs_get_ro_block_group_free_space(
7821 &sinfo->block_groups[i]);
7822
7823 spin_unlock(&sinfo->lock);
7824
7825 return free_bytes;
7826}
7827
Jeff Mahoney143bede2012-03-01 14:56:26 +01007828void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007829 struct btrfs_block_group_cache *cache)
7830{
7831 struct btrfs_space_info *sinfo = cache->space_info;
7832 u64 num_bytes;
7833
7834 BUG_ON(!cache->ro);
7835
7836 spin_lock(&sinfo->lock);
7837 spin_lock(&cache->lock);
7838 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7839 cache->bytes_super - btrfs_block_group_used(&cache->item);
7840 sinfo->bytes_readonly -= num_bytes;
7841 cache->ro = 0;
7842 spin_unlock(&cache->lock);
7843 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007844}
7845
Josef Bacikba1bf482009-09-11 16:11:19 -04007846/*
7847 * checks to see if its even possible to relocate this block group.
7848 *
7849 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7850 * ok to go ahead and try.
7851 */
7852int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007853{
Zheng Yan1a40e232008-09-26 10:09:34 -04007854 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007855 struct btrfs_space_info *space_info;
7856 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7857 struct btrfs_device *device;
liubocdcb7252011-08-03 10:15:25 +00007858 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04007859 u64 dev_min = 1;
7860 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007861 u64 target;
liubocdcb7252011-08-03 10:15:25 +00007862 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04007863 int full = 0;
7864 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05007865
Josef Bacikba1bf482009-09-11 16:11:19 -04007866 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04007867
Josef Bacikba1bf482009-09-11 16:11:19 -04007868 /* odd, couldn't find the block group, leave it alone */
7869 if (!block_group)
7870 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05007871
liubocdcb7252011-08-03 10:15:25 +00007872 min_free = btrfs_block_group_used(&block_group->item);
7873
Josef Bacikba1bf482009-09-11 16:11:19 -04007874 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00007875 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04007876 goto out;
Chris Mason323da792008-05-09 11:46:48 -04007877
Josef Bacikba1bf482009-09-11 16:11:19 -04007878 space_info = block_group->space_info;
7879 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04007880
Josef Bacikba1bf482009-09-11 16:11:19 -04007881 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04007882
Josef Bacikba1bf482009-09-11 16:11:19 -04007883 /*
7884 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04007885 * relocate it unless we're able to allocate a new chunk below.
7886 *
7887 * Otherwise, we need to make sure we have room in the space to handle
7888 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04007889 */
Chris Mason7ce618d2009-09-22 14:48:44 -04007890 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00007891 (space_info->bytes_used + space_info->bytes_reserved +
7892 space_info->bytes_pinned + space_info->bytes_readonly +
7893 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04007894 spin_unlock(&space_info->lock);
7895 goto out;
7896 }
7897 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007898
Josef Bacikba1bf482009-09-11 16:11:19 -04007899 /*
7900 * ok we don't have enough space, but maybe we have free space on our
7901 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007902 * alloc devices and guess if we have enough space. if this block
7903 * group is going to be restriped, run checks against the target
7904 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04007905 */
7906 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05007907
liubocdcb7252011-08-03 10:15:25 +00007908 /*
7909 * index:
7910 * 0: raid10
7911 * 1: raid1
7912 * 2: dup
7913 * 3: raid0
7914 * 4: single
7915 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007916 target = get_restripe_target(root->fs_info, block_group->flags);
7917 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00007918 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03007919 } else {
7920 /*
7921 * this is just a balance, so if we were marked as full
7922 * we know there is no space for a new chunk
7923 */
7924 if (full)
7925 goto out;
7926
7927 index = get_block_group_index(block_group);
7928 }
7929
Miao Xiee6ec7162013-01-17 05:38:51 +00007930 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00007931 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04007932 /* Divide by 2 */
7933 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007934 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00007935 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00007936 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04007937 /* Multiply by 2 */
7938 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00007939 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00007940 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04007941 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00007942 }
7943
Josef Bacikba1bf482009-09-11 16:11:19 -04007944 mutex_lock(&root->fs_info->chunk_mutex);
7945 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00007946 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04007947
Josef Bacikba1bf482009-09-11 16:11:19 -04007948 /*
7949 * check to make sure we can actually find a chunk with enough
7950 * space to fit our block group in.
7951 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01007952 if (device->total_bytes > device->bytes_used + min_free &&
7953 !device->is_tgtdev_for_dev_replace) {
Li Zefan125ccb02011-12-08 15:07:24 +08007954 ret = find_free_dev_extent(device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00007955 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04007956 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00007957 dev_nr++;
7958
7959 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05007960 break;
liubocdcb7252011-08-03 10:15:25 +00007961
Josef Bacikba1bf482009-09-11 16:11:19 -04007962 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05007963 }
Chris Masonedbd8d42007-12-21 16:27:24 -05007964 }
Josef Bacikba1bf482009-09-11 16:11:19 -04007965 mutex_unlock(&root->fs_info->chunk_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05007966out:
Josef Bacikba1bf482009-09-11 16:11:19 -04007967 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05007968 return ret;
7969}
7970
Christoph Hellwigb2950862008-12-02 09:54:17 -05007971static int find_first_block_group(struct btrfs_root *root,
7972 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007973{
Chris Mason925baed2008-06-25 16:01:30 -04007974 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007975 struct btrfs_key found_key;
7976 struct extent_buffer *leaf;
7977 int slot;
7978
7979 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7980 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007981 goto out;
7982
Chris Masond3977122009-01-05 21:25:51 -05007983 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007984 slot = path->slots[0];
7985 leaf = path->nodes[0];
7986 if (slot >= btrfs_header_nritems(leaf)) {
7987 ret = btrfs_next_leaf(root, path);
7988 if (ret == 0)
7989 continue;
7990 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007991 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007992 break;
7993 }
7994 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7995
7996 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007997 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7998 ret = 0;
7999 goto out;
8000 }
Chris Mason0b86a832008-03-24 15:01:56 -04008001 path->slots[0]++;
8002 }
Chris Mason925baed2008-06-25 16:01:30 -04008003out:
Chris Mason0b86a832008-03-24 15:01:56 -04008004 return ret;
8005}
8006
Josef Bacik0af3d002010-06-21 14:48:16 -04008007void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8008{
8009 struct btrfs_block_group_cache *block_group;
8010 u64 last = 0;
8011
8012 while (1) {
8013 struct inode *inode;
8014
8015 block_group = btrfs_lookup_first_block_group(info, last);
8016 while (block_group) {
8017 spin_lock(&block_group->lock);
8018 if (block_group->iref)
8019 break;
8020 spin_unlock(&block_group->lock);
8021 block_group = next_block_group(info->tree_root,
8022 block_group);
8023 }
8024 if (!block_group) {
8025 if (last == 0)
8026 break;
8027 last = 0;
8028 continue;
8029 }
8030
8031 inode = block_group->inode;
8032 block_group->iref = 0;
8033 block_group->inode = NULL;
8034 spin_unlock(&block_group->lock);
8035 iput(inode);
8036 last = block_group->key.objectid + block_group->key.offset;
8037 btrfs_put_block_group(block_group);
8038 }
8039}
8040
Zheng Yan1a40e232008-09-26 10:09:34 -04008041int btrfs_free_block_groups(struct btrfs_fs_info *info)
8042{
8043 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008044 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008045 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008046 struct rb_node *n;
8047
Yan Zheng11833d62009-09-11 16:11:19 -04008048 down_write(&info->extent_commit_sem);
8049 while (!list_empty(&info->caching_block_groups)) {
8050 caching_ctl = list_entry(info->caching_block_groups.next,
8051 struct btrfs_caching_control, list);
8052 list_del(&caching_ctl->list);
8053 put_caching_control(caching_ctl);
8054 }
8055 up_write(&info->extent_commit_sem);
8056
Zheng Yan1a40e232008-09-26 10:09:34 -04008057 spin_lock(&info->block_group_cache_lock);
8058 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8059 block_group = rb_entry(n, struct btrfs_block_group_cache,
8060 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008061 rb_erase(&block_group->cache_node,
8062 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008063 spin_unlock(&info->block_group_cache_lock);
8064
Josef Bacik80eb2342008-10-29 14:49:05 -04008065 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008066 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008067 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008068
Josef Bacik817d52f2009-07-13 21:29:25 -04008069 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008070 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008071
Josef Bacik3c148742011-02-02 15:53:47 +00008072 /*
8073 * We haven't cached this block group, which means we could
8074 * possibly have excluded extents on this block group.
8075 */
8076 if (block_group->cached == BTRFS_CACHE_NO)
8077 free_excluded_extents(info->extent_root, block_group);
8078
Josef Bacik817d52f2009-07-13 21:29:25 -04008079 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008080 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008081
8082 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008083 }
8084 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008085
8086 /* now that all the block groups are freed, go through and
8087 * free all the space_info structs. This is only called during
8088 * the final stages of unmount, and so we know nobody is
8089 * using them. We call synchronize_rcu() once before we start,
8090 * just to be on the safe side.
8091 */
8092 synchronize_rcu();
8093
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008094 release_global_block_rsv(info);
8095
Chris Mason4184ea72009-03-10 12:39:20 -04008096 while(!list_empty(&info->space_info)) {
8097 space_info = list_entry(info->space_info.next,
8098 struct btrfs_space_info,
8099 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008100 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8101 if (space_info->bytes_pinned > 0 ||
8102 space_info->bytes_reserved > 0 ||
8103 space_info->bytes_may_use > 0) {
8104 WARN_ON(1);
8105 dump_space_info(space_info, 0, 0);
8106 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008107 }
Chris Mason4184ea72009-03-10 12:39:20 -04008108 list_del(&space_info->list);
8109 kfree(space_info);
8110 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008111 return 0;
8112}
8113
Yan, Zhengb742bb822010-05-16 10:46:24 -04008114static void __link_block_group(struct btrfs_space_info *space_info,
8115 struct btrfs_block_group_cache *cache)
8116{
8117 int index = get_block_group_index(cache);
8118
8119 down_write(&space_info->groups_sem);
8120 list_add_tail(&cache->list, &space_info->block_groups[index]);
8121 up_write(&space_info->groups_sem);
8122}
8123
Chris Mason9078a3e2007-04-26 16:46:15 -04008124int btrfs_read_block_groups(struct btrfs_root *root)
8125{
8126 struct btrfs_path *path;
8127 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008128 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008129 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008130 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008131 struct btrfs_key key;
8132 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008133 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008134 int need_clear = 0;
8135 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008136
Chris Masonbe744172007-05-06 10:15:01 -04008137 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008138 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008139 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008140 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008141 path = btrfs_alloc_path();
8142 if (!path)
8143 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008144 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008145
David Sterba6c417612011-04-13 15:41:04 +02008146 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008147 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008148 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008149 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008150 if (btrfs_test_opt(root, CLEAR_CACHE))
8151 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008152
Chris Masond3977122009-01-05 21:25:51 -05008153 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008154 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008155 if (ret > 0)
8156 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008157 if (ret != 0)
8158 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008159 leaf = path->nodes[0];
8160 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008161 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008162 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008163 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008164 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008165 }
Li Zefan34d52cb2011-03-29 13:46:06 +08008166 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8167 GFP_NOFS);
8168 if (!cache->free_space_ctl) {
8169 kfree(cache);
8170 ret = -ENOMEM;
8171 goto error;
8172 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008173
Yan Zhengd2fb3432008-12-11 16:30:39 -05008174 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008175 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008176 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008177 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008178 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008179
Liu Bocf7c1ef2012-07-06 03:31:34 -06008180 if (need_clear) {
8181 /*
8182 * When we mount with old space cache, we need to
8183 * set BTRFS_DC_CLEAR and set dirty flag.
8184 *
8185 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8186 * truncate the old free space cache inode and
8187 * setup a new one.
8188 * b) Setting 'dirty flag' makes sure that we flush
8189 * the new space cache info onto disk.
8190 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008191 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008192 if (btrfs_test_opt(root, SPACE_CACHE))
8193 cache->dirty = 1;
8194 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008195
Chris Mason5f39d392007-10-15 16:14:19 -04008196 read_extent_buffer(leaf, &cache->item,
8197 btrfs_item_ptr_offset(leaf, path->slots[0]),
8198 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008199 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008200
Chris Mason9078a3e2007-04-26 16:46:15 -04008201 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008202 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008203 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008204 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05008205 cache->full_stripe_len = btrfs_full_stripe_len(root,
8206 &root->fs_info->mapping_tree,
8207 found_key.objectid);
Li Zefan34d52cb2011-03-29 13:46:06 +08008208 btrfs_init_free_space_ctl(cache);
8209
Josef Bacik817d52f2009-07-13 21:29:25 -04008210 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008211 * We need to exclude the super stripes now so that the space
8212 * info has super bytes accounted for, otherwise we'll think
8213 * we have more space than we actually do.
8214 */
Josef Bacik835d9742013-03-19 12:13:25 -04008215 ret = exclude_super_stripes(root, cache);
8216 if (ret) {
8217 /*
8218 * We may have excluded something, so call this just in
8219 * case.
8220 */
8221 free_excluded_extents(root, cache);
8222 kfree(cache->free_space_ctl);
8223 kfree(cache);
8224 goto error;
8225 }
Josef Bacik3c148742011-02-02 15:53:47 +00008226
8227 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008228 * check for two cases, either we are full, and therefore
8229 * don't need to bother with the caching work since we won't
8230 * find any space, or we are empty, and we can just add all
8231 * the space in and be done with it. This saves us _alot_ of
8232 * time, particularly in the full case.
8233 */
8234 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008235 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008236 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008237 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008238 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008239 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008240 cache->cached = BTRFS_CACHE_FINISHED;
8241 add_new_free_space(cache, root->fs_info,
8242 found_key.objectid,
8243 found_key.objectid +
8244 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008245 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008246 }
Chris Mason96b51792007-10-15 16:15:19 -04008247
Josef Bacik8c579fe2013-04-02 12:40:42 -04008248 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8249 if (ret) {
8250 btrfs_remove_free_space_cache(cache);
8251 btrfs_put_block_group(cache);
8252 goto error;
8253 }
8254
Chris Mason6324fbf2008-03-24 15:01:59 -04008255 ret = update_space_info(info, cache->flags, found_key.offset,
8256 btrfs_block_group_used(&cache->item),
8257 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008258 if (ret) {
8259 btrfs_remove_free_space_cache(cache);
8260 spin_lock(&info->block_group_cache_lock);
8261 rb_erase(&cache->cache_node,
8262 &info->block_group_cache_tree);
8263 spin_unlock(&info->block_group_cache_lock);
8264 btrfs_put_block_group(cache);
8265 goto error;
8266 }
8267
Chris Mason6324fbf2008-03-24 15:01:59 -04008268 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008269 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008270 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008271 spin_unlock(&cache->space_info->lock);
8272
Yan, Zhengb742bb822010-05-16 10:46:24 -04008273 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008274
Chris Mason75ccf472008-09-30 19:24:06 -04008275 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008276 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008277 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008278 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04008279
8280 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8281 if (!(get_alloc_profile(root, space_info->flags) &
8282 (BTRFS_BLOCK_GROUP_RAID10 |
8283 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008284 BTRFS_BLOCK_GROUP_RAID5 |
8285 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb822010-05-16 10:46:24 -04008286 BTRFS_BLOCK_GROUP_DUP)))
8287 continue;
8288 /*
8289 * avoid allocating from un-mirrored block group if there are
8290 * mirrored block groups.
8291 */
8292 list_for_each_entry(cache, &space_info->block_groups[3], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008293 set_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008294 list_for_each_entry(cache, &space_info->block_groups[4], list)
Miao Xie199c36e2011-07-15 10:34:36 +00008295 set_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008296 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008297
8298 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008299 ret = 0;
8300error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008301 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008302 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008303}
Chris Mason6324fbf2008-03-24 15:01:59 -04008304
Josef Bacikea658ba2012-09-11 16:57:25 -04008305void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8306 struct btrfs_root *root)
8307{
8308 struct btrfs_block_group_cache *block_group, *tmp;
8309 struct btrfs_root *extent_root = root->fs_info->extent_root;
8310 struct btrfs_block_group_item item;
8311 struct btrfs_key key;
8312 int ret = 0;
8313
8314 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8315 new_bg_list) {
8316 list_del_init(&block_group->new_bg_list);
8317
8318 if (ret)
8319 continue;
8320
8321 spin_lock(&block_group->lock);
8322 memcpy(&item, &block_group->item, sizeof(item));
8323 memcpy(&key, &block_group->key, sizeof(key));
8324 spin_unlock(&block_group->lock);
8325
8326 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8327 sizeof(item));
8328 if (ret)
8329 btrfs_abort_transaction(trans, extent_root, ret);
8330 }
8331}
8332
Chris Mason6324fbf2008-03-24 15:01:59 -04008333int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8334 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008335 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008336 u64 size)
8337{
8338 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008339 struct btrfs_root *extent_root;
8340 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008341
8342 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008343
Chris Mason12fcfd22009-03-24 10:24:20 -04008344 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008345
Chris Mason8f18cf12008-04-25 16:53:30 -04008346 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008347 if (!cache)
8348 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008349 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8350 GFP_NOFS);
8351 if (!cache->free_space_ctl) {
8352 kfree(cache);
8353 return -ENOMEM;
8354 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008355
Chris Masone17cade2008-04-15 15:41:47 -04008356 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008357 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008358 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008359 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008360 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008361 cache->full_stripe_len = btrfs_full_stripe_len(root,
8362 &root->fs_info->mapping_tree,
8363 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008364
Yan Zhengd2fb3432008-12-11 16:30:39 -05008365 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008366 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008367 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008368 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008369 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008370
Li Zefan34d52cb2011-03-29 13:46:06 +08008371 btrfs_init_free_space_ctl(cache);
8372
Chris Mason6324fbf2008-03-24 15:01:59 -04008373 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008374 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8375 cache->flags = type;
8376 btrfs_set_block_group_flags(&cache->item, type);
8377
Yan Zheng11833d62009-09-11 16:11:19 -04008378 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008379 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008380 ret = exclude_super_stripes(root, cache);
8381 if (ret) {
8382 /*
8383 * We may have excluded something, so call this just in
8384 * case.
8385 */
8386 free_excluded_extents(root, cache);
8387 kfree(cache->free_space_ctl);
8388 kfree(cache);
8389 return ret;
8390 }
Josef Bacik96303082009-07-13 21:29:25 -04008391
Josef Bacik817d52f2009-07-13 21:29:25 -04008392 add_new_free_space(cache, root->fs_info, chunk_offset,
8393 chunk_offset + size);
8394
Yan Zheng11833d62009-09-11 16:11:19 -04008395 free_excluded_extents(root, cache);
8396
Josef Bacik8c579fe2013-04-02 12:40:42 -04008397 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8398 if (ret) {
8399 btrfs_remove_free_space_cache(cache);
8400 btrfs_put_block_group(cache);
8401 return ret;
8402 }
8403
Chris Mason6324fbf2008-03-24 15:01:59 -04008404 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8405 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008406 if (ret) {
8407 btrfs_remove_free_space_cache(cache);
8408 spin_lock(&root->fs_info->block_group_cache_lock);
8409 rb_erase(&cache->cache_node,
8410 &root->fs_info->block_group_cache_tree);
8411 spin_unlock(&root->fs_info->block_group_cache_lock);
8412 btrfs_put_block_group(cache);
8413 return ret;
8414 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008415 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008416
8417 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008418 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008419 spin_unlock(&cache->space_info->lock);
8420
Yan, Zhengb742bb822010-05-16 10:46:24 -04008421 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008422
Josef Bacikea658ba2012-09-11 16:57:25 -04008423 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008424
Chris Masond18a2c42008-04-04 15:40:00 -04008425 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008426
Chris Mason6324fbf2008-03-24 15:01:59 -04008427 return 0;
8428}
Zheng Yan1a40e232008-09-26 10:09:34 -04008429
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008430static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8431{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008432 u64 extra_flags = chunk_to_extended(flags) &
8433 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008434
Miao Xiede98ced2013-01-29 10:13:12 +00008435 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008436 if (flags & BTRFS_BLOCK_GROUP_DATA)
8437 fs_info->avail_data_alloc_bits &= ~extra_flags;
8438 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8439 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8440 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8441 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008442 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008443}
8444
Zheng Yan1a40e232008-09-26 10:09:34 -04008445int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8446 struct btrfs_root *root, u64 group_start)
8447{
8448 struct btrfs_path *path;
8449 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008450 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008451 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008452 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008453 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008454 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008455 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008456 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008457
Zheng Yan1a40e232008-09-26 10:09:34 -04008458 root = root->fs_info->extent_root;
8459
8460 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8461 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008462 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008463
liubo9f7c43c2011-03-07 02:13:33 +00008464 /*
8465 * Free the reserved super bytes from this block group before
8466 * remove it.
8467 */
8468 free_excluded_extents(root, block_group);
8469
Zheng Yan1a40e232008-09-26 10:09:34 -04008470 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008471 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008472 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8473 BTRFS_BLOCK_GROUP_RAID1 |
8474 BTRFS_BLOCK_GROUP_RAID10))
8475 factor = 2;
8476 else
8477 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008478
Chris Mason44fb5512009-06-04 15:34:51 -04008479 /* make sure this block group isn't part of an allocation cluster */
8480 cluster = &root->fs_info->data_alloc_cluster;
8481 spin_lock(&cluster->refill_lock);
8482 btrfs_return_cluster_to_free_space(block_group, cluster);
8483 spin_unlock(&cluster->refill_lock);
8484
8485 /*
8486 * make sure this block group isn't part of a metadata
8487 * allocation cluster
8488 */
8489 cluster = &root->fs_info->meta_alloc_cluster;
8490 spin_lock(&cluster->refill_lock);
8491 btrfs_return_cluster_to_free_space(block_group, cluster);
8492 spin_unlock(&cluster->refill_lock);
8493
Zheng Yan1a40e232008-09-26 10:09:34 -04008494 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008495 if (!path) {
8496 ret = -ENOMEM;
8497 goto out;
8498 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008499
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008500 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008501 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008502 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008503 if (ret) {
8504 btrfs_add_delayed_iput(inode);
8505 goto out;
8506 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008507 clear_nlink(inode);
8508 /* One for the block groups ref */
8509 spin_lock(&block_group->lock);
8510 if (block_group->iref) {
8511 block_group->iref = 0;
8512 block_group->inode = NULL;
8513 spin_unlock(&block_group->lock);
8514 iput(inode);
8515 } else {
8516 spin_unlock(&block_group->lock);
8517 }
8518 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008519 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008520 }
8521
8522 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8523 key.offset = block_group->key.objectid;
8524 key.type = 0;
8525
8526 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8527 if (ret < 0)
8528 goto out;
8529 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008530 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008531 if (ret == 0) {
8532 ret = btrfs_del_item(trans, tree_root, path);
8533 if (ret)
8534 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008535 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008536 }
8537
Yan Zheng3dfdb932009-01-21 10:49:16 -05008538 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008539 rb_erase(&block_group->cache_node,
8540 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008541
8542 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8543 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008544 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008545
Josef Bacik80eb2342008-10-29 14:49:05 -04008546 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008547 /*
8548 * we must use list_del_init so people can check to see if they
8549 * are still on the list after taking the semaphore
8550 */
8551 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008552 if (list_empty(&block_group->space_info->block_groups[index]))
8553 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008554 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008555
Josef Bacik817d52f2009-07-13 21:29:25 -04008556 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008557 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008558
8559 btrfs_remove_free_space_cache(block_group);
8560
Yan Zhengc146afa2008-11-12 14:34:12 -05008561 spin_lock(&block_group->space_info->lock);
8562 block_group->space_info->total_bytes -= block_group->key.offset;
8563 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008564 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008565 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008566
Josef Bacik0af3d002010-06-21 14:48:16 -04008567 memcpy(&key, &block_group->key, sizeof(key));
8568
Chris Mason283bb192009-07-24 16:30:55 -04008569 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008570
Chris Masonfa9c0d792009-04-03 09:47:43 -04008571 btrfs_put_block_group(block_group);
8572 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008573
8574 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8575 if (ret > 0)
8576 ret = -EIO;
8577 if (ret < 0)
8578 goto out;
8579
8580 ret = btrfs_del_item(trans, root, path);
8581out:
8582 btrfs_free_path(path);
8583 return ret;
8584}
liuboacce9522011-01-06 19:30:25 +08008585
liuboc59021f2011-03-07 02:13:14 +00008586int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8587{
8588 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008589 struct btrfs_super_block *disk_super;
8590 u64 features;
8591 u64 flags;
8592 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008593 int ret;
8594
David Sterba6c417612011-04-13 15:41:04 +02008595 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008596 if (!btrfs_super_root(disk_super))
8597 return 1;
liuboc59021f2011-03-07 02:13:14 +00008598
liubo1aba86d2011-04-08 08:44:37 +00008599 features = btrfs_super_incompat_flags(disk_super);
8600 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8601 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008602
liubo1aba86d2011-04-08 08:44:37 +00008603 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8604 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008605 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008606 goto out;
liuboc59021f2011-03-07 02:13:14 +00008607
liubo1aba86d2011-04-08 08:44:37 +00008608 if (mixed) {
8609 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8610 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8611 } else {
8612 flags = BTRFS_BLOCK_GROUP_METADATA;
8613 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8614 if (ret)
8615 goto out;
8616
8617 flags = BTRFS_BLOCK_GROUP_DATA;
8618 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8619 }
8620out:
liuboc59021f2011-03-07 02:13:14 +00008621 return ret;
8622}
8623
liuboacce9522011-01-06 19:30:25 +08008624int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8625{
8626 return unpin_extent_range(root, start, end);
8627}
8628
8629int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008630 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008631{
Li Dongyang5378e602011-03-24 10:24:27 +00008632 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008633}
Li Dongyangf7039b12011-03-24 10:24:28 +00008634
8635int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8636{
8637 struct btrfs_fs_info *fs_info = root->fs_info;
8638 struct btrfs_block_group_cache *cache = NULL;
8639 u64 group_trimmed;
8640 u64 start;
8641 u64 end;
8642 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008643 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008644 int ret = 0;
8645
Liu Bo2cac13e2012-02-09 18:17:41 +08008646 /*
8647 * try to trim all FS space, our block group may start from non-zero.
8648 */
8649 if (range->len == total_bytes)
8650 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8651 else
8652 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008653
8654 while (cache) {
8655 if (cache->key.objectid >= (range->start + range->len)) {
8656 btrfs_put_block_group(cache);
8657 break;
8658 }
8659
8660 start = max(range->start, cache->key.objectid);
8661 end = min(range->start + range->len,
8662 cache->key.objectid + cache->key.offset);
8663
8664 if (end - start >= range->minlen) {
8665 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008666 ret = cache_block_group(cache, 0);
Li Dongyangf7039b12011-03-24 10:24:28 +00008667 if (!ret)
8668 wait_block_group_cache_done(cache);
8669 }
8670 ret = btrfs_trim_block_group(cache,
8671 &group_trimmed,
8672 start,
8673 end,
8674 range->minlen);
8675
8676 trimmed += group_trimmed;
8677 if (ret) {
8678 btrfs_put_block_group(cache);
8679 break;
8680 }
8681 }
8682
8683 cache = next_block_group(fs_info->tree_root, cache);
8684 }
8685
8686 range->len = trimmed;
8687 return ret;
8688}