blob: 9c01509dd8abfb0fddc5480b7f4cb3b73002aad4 [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>
Josef Bacikb150a4f2013-06-19 15:00:04 -040027#include <linux/percpu_counter.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 Masonfa9c0d72009-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();
Josef Bacik36cce922013-08-05 11:15:21 -0400115 return cache->cached == BTRFS_CACHE_FINISHED ||
116 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -0400117}
118
Josef Bacik0f9dd462008-09-23 13:14:11 -0400119static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
120{
121 return (cache->flags & bits) == bits;
122}
123
David Sterba62a45b62011-04-20 15:52:26 +0200124static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000125{
126 atomic_inc(&cache->count);
127}
128
129void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
130{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400131 if (atomic_dec_and_test(&cache->count)) {
132 WARN_ON(cache->pinned > 0);
133 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800134 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000135 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400136 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000137}
138
Josef Bacik0f9dd462008-09-23 13:14:11 -0400139/*
140 * this adds the block group to the fs_info rb tree for the block group
141 * cache
142 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500143static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400144 struct btrfs_block_group_cache *block_group)
145{
146 struct rb_node **p;
147 struct rb_node *parent = NULL;
148 struct btrfs_block_group_cache *cache;
149
150 spin_lock(&info->block_group_cache_lock);
151 p = &info->block_group_cache_tree.rb_node;
152
153 while (*p) {
154 parent = *p;
155 cache = rb_entry(parent, struct btrfs_block_group_cache,
156 cache_node);
157 if (block_group->key.objectid < cache->key.objectid) {
158 p = &(*p)->rb_left;
159 } else if (block_group->key.objectid > cache->key.objectid) {
160 p = &(*p)->rb_right;
161 } else {
162 spin_unlock(&info->block_group_cache_lock);
163 return -EEXIST;
164 }
165 }
166
167 rb_link_node(&block_group->cache_node, parent, p);
168 rb_insert_color(&block_group->cache_node,
169 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000170
171 if (info->first_logical_byte > block_group->key.objectid)
172 info->first_logical_byte = block_group->key.objectid;
173
Josef Bacik0f9dd462008-09-23 13:14:11 -0400174 spin_unlock(&info->block_group_cache_lock);
175
176 return 0;
177}
178
179/*
180 * This will return the block group at or after bytenr if contains is 0, else
181 * it will return the block group that contains the bytenr
182 */
183static struct btrfs_block_group_cache *
184block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
185 int contains)
186{
187 struct btrfs_block_group_cache *cache, *ret = NULL;
188 struct rb_node *n;
189 u64 end, start;
190
191 spin_lock(&info->block_group_cache_lock);
192 n = info->block_group_cache_tree.rb_node;
193
194 while (n) {
195 cache = rb_entry(n, struct btrfs_block_group_cache,
196 cache_node);
197 end = cache->key.objectid + cache->key.offset - 1;
198 start = cache->key.objectid;
199
200 if (bytenr < start) {
201 if (!contains && (!ret || start < ret->key.objectid))
202 ret = cache;
203 n = n->rb_left;
204 } else if (bytenr > start) {
205 if (contains && bytenr <= end) {
206 ret = cache;
207 break;
208 }
209 n = n->rb_right;
210 } else {
211 ret = cache;
212 break;
213 }
214 }
Liu Boa1897fd2012-12-27 09:01:23 +0000215 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000216 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000217 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
218 info->first_logical_byte = ret->key.objectid;
219 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400220 spin_unlock(&info->block_group_cache_lock);
221
222 return ret;
223}
224
Yan Zheng11833d62009-09-11 16:11:19 -0400225static int add_excluded_extent(struct btrfs_root *root,
226 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400227{
Yan Zheng11833d62009-09-11 16:11:19 -0400228 u64 end = start + num_bytes - 1;
229 set_extent_bits(&root->fs_info->freed_extents[0],
230 start, end, EXTENT_UPTODATE, GFP_NOFS);
231 set_extent_bits(&root->fs_info->freed_extents[1],
232 start, end, EXTENT_UPTODATE, GFP_NOFS);
233 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400234}
235
Yan Zheng11833d62009-09-11 16:11:19 -0400236static void free_excluded_extents(struct btrfs_root *root,
237 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400238{
Yan Zheng11833d62009-09-11 16:11:19 -0400239 u64 start, end;
240
241 start = cache->key.objectid;
242 end = start + cache->key.offset - 1;
243
244 clear_extent_bits(&root->fs_info->freed_extents[0],
245 start, end, EXTENT_UPTODATE, GFP_NOFS);
246 clear_extent_bits(&root->fs_info->freed_extents[1],
247 start, end, EXTENT_UPTODATE, GFP_NOFS);
248}
249
250static int exclude_super_stripes(struct btrfs_root *root,
251 struct btrfs_block_group_cache *cache)
252{
Josef Bacik817d52f2009-07-13 21:29:25 -0400253 u64 bytenr;
254 u64 *logical;
255 int stripe_len;
256 int i, nr, ret;
257
Yan, Zheng06b23312009-11-26 09:31:11 +0000258 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
259 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
260 cache->bytes_super += stripe_len;
261 ret = add_excluded_extent(root, cache->key.objectid,
262 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400263 if (ret)
264 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000265 }
266
Josef Bacik817d52f2009-07-13 21:29:25 -0400267 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
268 bytenr = btrfs_sb_offset(i);
269 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
270 cache->key.objectid, bytenr,
271 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400272 if (ret)
273 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400274
Josef Bacik817d52f2009-07-13 21:29:25 -0400275 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400276 u64 start, len;
277
278 if (logical[nr] > cache->key.objectid +
279 cache->key.offset)
280 continue;
281
282 if (logical[nr] + stripe_len <= cache->key.objectid)
283 continue;
284
285 start = logical[nr];
286 if (start < cache->key.objectid) {
287 start = cache->key.objectid;
288 len = (logical[nr] + stripe_len) - start;
289 } else {
290 len = min_t(u64, stripe_len,
291 cache->key.objectid +
292 cache->key.offset - start);
293 }
294
295 cache->bytes_super += len;
296 ret = add_excluded_extent(root, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400297 if (ret) {
298 kfree(logical);
299 return ret;
300 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400301 }
Yan Zheng11833d62009-09-11 16:11:19 -0400302
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 kfree(logical);
304 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 return 0;
306}
307
Yan Zheng11833d62009-09-11 16:11:19 -0400308static struct btrfs_caching_control *
309get_caching_control(struct btrfs_block_group_cache *cache)
310{
311 struct btrfs_caching_control *ctl;
312
313 spin_lock(&cache->lock);
314 if (cache->cached != BTRFS_CACHE_STARTED) {
315 spin_unlock(&cache->lock);
316 return NULL;
317 }
318
Josef Bacikdde5abe2010-09-16 16:17:03 -0400319 /* We're loading it the fast way, so we don't have a caching_ctl. */
320 if (!cache->caching_ctl) {
321 spin_unlock(&cache->lock);
322 return NULL;
323 }
324
Yan Zheng11833d62009-09-11 16:11:19 -0400325 ctl = cache->caching_ctl;
326 atomic_inc(&ctl->count);
327 spin_unlock(&cache->lock);
328 return ctl;
329}
330
331static void put_caching_control(struct btrfs_caching_control *ctl)
332{
333 if (atomic_dec_and_test(&ctl->count))
334 kfree(ctl);
335}
336
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337/*
338 * this is only called by cache_block_group, since we could have freed extents
339 * we need to check the pinned_extents for any extents that can't be used yet
340 * since their free space will be released as soon as the transaction commits.
341 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400342static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343 struct btrfs_fs_info *info, u64 start, u64 end)
344{
Josef Bacik817d52f2009-07-13 21:29:25 -0400345 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400346 int ret;
347
348 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400349 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400350 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400351 EXTENT_DIRTY | EXTENT_UPTODATE,
352 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400353 if (ret)
354 break;
355
Yan, Zheng06b23312009-11-26 09:31:11 +0000356 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357 start = extent_end + 1;
358 } else if (extent_start > start && extent_start < end) {
359 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400360 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500361 ret = btrfs_add_free_space(block_group, start,
362 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100363 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400364 start = extent_end + 1;
365 } else {
366 break;
367 }
368 }
369
370 if (start < end) {
371 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400372 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500373 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100374 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400375 }
376
Josef Bacik817d52f2009-07-13 21:29:25 -0400377 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378}
379
Josef Bacikbab39bf2011-06-30 14:42:28 -0400380static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400381{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400382 struct btrfs_block_group_cache *block_group;
383 struct btrfs_fs_info *fs_info;
384 struct btrfs_caching_control *caching_ctl;
385 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400386 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400387 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400388 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400389 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400390 u64 last = 0;
391 u32 nritems;
Josef Bacik36cce922013-08-05 11:15:21 -0400392 int ret = -ENOMEM;
Chris Masonf510cfe2007-10-15 16:14:48 -0400393
Josef Bacikbab39bf2011-06-30 14:42:28 -0400394 caching_ctl = container_of(work, struct btrfs_caching_control, work);
395 block_group = caching_ctl->block_group;
396 fs_info = block_group->fs_info;
397 extent_root = fs_info->extent_root;
398
Chris Masone37c9e62007-05-09 20:13:14 -0400399 path = btrfs_alloc_path();
400 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400401 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400402
Josef Bacik817d52f2009-07-13 21:29:25 -0400403 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400404
Chris Mason5cd57b22008-06-25 16:01:30 -0400405 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400406 * We don't want to deadlock with somebody trying to allocate a new
407 * extent for the extent root while also trying to search the extent
408 * root to add free space. So we skip locking and search the commit
409 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400410 */
411 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400412 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400413 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400414
Yan Zhenge4404d62008-12-12 10:03:26 -0500415 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400416 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400417 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400418again:
Yan Zheng11833d62009-09-11 16:11:19 -0400419 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400420 /* need to make sure the commit_root doesn't disappear */
421 down_read(&fs_info->extent_commit_sem);
422
Liu Bo52ee28d2013-07-11 17:51:15 +0800423next:
Yan Zheng11833d62009-09-11 16:11:19 -0400424 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400425 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400426 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500427
Yan Zheng11833d62009-09-11 16:11:19 -0400428 leaf = path->nodes[0];
429 nritems = btrfs_header_nritems(leaf);
430
Chris Masond3977122009-01-05 21:25:51 -0500431 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200432 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400433 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400434 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400435 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400436
Yan Zheng11833d62009-09-11 16:11:19 -0400437 if (path->slots[0] < nritems) {
438 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
439 } else {
440 ret = find_next_key(path, 0, &key);
441 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400442 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400443
Josef Bacik0a3896d2013-04-19 14:37:26 -0400444 if (need_resched()) {
Josef Bacik589d8ad2011-05-11 17:30:53 -0400445 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400446 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400447 up_read(&fs_info->extent_commit_sem);
448 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400449 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400450 goto again;
451 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400452
453 ret = btrfs_next_leaf(extent_root, path);
454 if (ret < 0)
455 goto err;
456 if (ret)
457 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400458 leaf = path->nodes[0];
459 nritems = btrfs_header_nritems(leaf);
460 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400461 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400462
Liu Bo52ee28d2013-07-11 17:51:15 +0800463 if (key.objectid < last) {
464 key.objectid = last;
465 key.offset = 0;
466 key.type = BTRFS_EXTENT_ITEM_KEY;
467
468 caching_ctl->progress = last;
469 btrfs_release_path(path);
470 goto next;
471 }
472
Yan Zheng11833d62009-09-11 16:11:19 -0400473 if (key.objectid < block_group->key.objectid) {
474 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400475 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400476 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400477
Chris Masone37c9e62007-05-09 20:13:14 -0400478 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400479 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400480 break;
Yan7d7d6062007-09-14 16:15:28 -0400481
Josef Bacik3173a182013-03-07 14:22:04 -0500482 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
483 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400484 total_found += add_new_free_space(block_group,
485 fs_info, last,
486 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500487 if (key.type == BTRFS_METADATA_ITEM_KEY)
488 last = key.objectid +
489 fs_info->tree_root->leafsize;
490 else
491 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400492
Yan Zheng11833d62009-09-11 16:11:19 -0400493 if (total_found > (1024 * 1024 * 2)) {
494 total_found = 0;
495 wake_up(&caching_ctl->wait);
496 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400497 }
Chris Masone37c9e62007-05-09 20:13:14 -0400498 path->slots[0]++;
499 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400500 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400501
502 total_found += add_new_free_space(block_group, fs_info, last,
503 block_group->key.objectid +
504 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400505 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400506
507 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400508 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400509 block_group->cached = BTRFS_CACHE_FINISHED;
510 spin_unlock(&block_group->lock);
511
Chris Mason54aa1f42007-06-22 14:16:25 -0400512err:
Chris Masone37c9e62007-05-09 20:13:14 -0400513 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400514 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400515
Yan Zheng11833d62009-09-11 16:11:19 -0400516 free_excluded_extents(extent_root, block_group);
517
518 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400519out:
Josef Bacik36cce922013-08-05 11:15:21 -0400520 if (ret) {
521 spin_lock(&block_group->lock);
522 block_group->caching_ctl = NULL;
523 block_group->cached = BTRFS_CACHE_ERROR;
524 spin_unlock(&block_group->lock);
525 }
Yan Zheng11833d62009-09-11 16:11:19 -0400526 wake_up(&caching_ctl->wait);
527
528 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000529 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400530}
531
Josef Bacik9d66e232010-08-25 16:54:15 -0400532static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400533 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400534{
Josef Bacik291c7d22011-11-14 13:52:14 -0500535 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400536 struct btrfs_fs_info *fs_info = cache->fs_info;
537 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400538 int ret = 0;
539
Josef Bacik291c7d22011-11-14 13:52:14 -0500540 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100541 if (!caching_ctl)
542 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500543
544 INIT_LIST_HEAD(&caching_ctl->list);
545 mutex_init(&caching_ctl->mutex);
546 init_waitqueue_head(&caching_ctl->wait);
547 caching_ctl->block_group = cache;
548 caching_ctl->progress = cache->key.objectid;
549 atomic_set(&caching_ctl->count, 1);
550 caching_ctl->work.func = caching_thread;
551
552 spin_lock(&cache->lock);
553 /*
554 * This should be a rare occasion, but this could happen I think in the
555 * case where one thread starts to load the space cache info, and then
556 * some other thread starts a transaction commit which tries to do an
557 * allocation while the other thread is still loading the space cache
558 * info. The previous loop should have kept us from choosing this block
559 * group, but if we've moved to the state where we will wait on caching
560 * block groups we need to first check if we're doing a fast load here,
561 * so we can wait for it to finish, otherwise we could end up allocating
562 * from a block group who's cache gets evicted for one reason or
563 * another.
564 */
565 while (cache->cached == BTRFS_CACHE_FAST) {
566 struct btrfs_caching_control *ctl;
567
568 ctl = cache->caching_ctl;
569 atomic_inc(&ctl->count);
570 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
571 spin_unlock(&cache->lock);
572
573 schedule();
574
575 finish_wait(&ctl->wait, &wait);
576 put_caching_control(ctl);
577 spin_lock(&cache->lock);
578 }
579
580 if (cache->cached != BTRFS_CACHE_NO) {
581 spin_unlock(&cache->lock);
582 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400583 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500584 }
585 WARN_ON(cache->caching_ctl);
586 cache->caching_ctl = caching_ctl;
587 cache->cached = BTRFS_CACHE_FAST;
588 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400589
Josef Bacikd53ba472012-04-12 16:03:57 -0400590 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400591 ret = load_free_space_cache(fs_info, cache);
592
593 spin_lock(&cache->lock);
594 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500595 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400596 cache->cached = BTRFS_CACHE_FINISHED;
597 cache->last_byte_to_unpin = (u64)-1;
598 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500599 if (load_cache_only) {
600 cache->caching_ctl = NULL;
601 cache->cached = BTRFS_CACHE_NO;
602 } else {
603 cache->cached = BTRFS_CACHE_STARTED;
604 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400605 }
606 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500607 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000608 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500609 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000610 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400611 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000612 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500613 } else {
614 /*
615 * We are not going to do the fast caching, set cached to the
616 * appropriate value and wakeup any waiters.
617 */
618 spin_lock(&cache->lock);
619 if (load_cache_only) {
620 cache->caching_ctl = NULL;
621 cache->cached = BTRFS_CACHE_NO;
622 } else {
623 cache->cached = BTRFS_CACHE_STARTED;
624 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400625 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500626 wake_up(&caching_ctl->wait);
627 }
628
629 if (load_cache_only) {
630 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400631 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400632 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400633
Yan Zheng11833d62009-09-11 16:11:19 -0400634 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500635 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400636 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
637 up_write(&fs_info->extent_commit_sem);
638
Josef Bacik11dfe352009-11-13 20:12:59 +0000639 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400640
Josef Bacikbab39bf2011-06-30 14:42:28 -0400641 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400642
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400643 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400644}
645
Josef Bacik0f9dd462008-09-23 13:14:11 -0400646/*
647 * return the block group that starts at or after bytenr
648 */
Chris Masond3977122009-01-05 21:25:51 -0500649static struct btrfs_block_group_cache *
650btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400651{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400652 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400653
Josef Bacik0f9dd462008-09-23 13:14:11 -0400654 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400655
Josef Bacik0f9dd462008-09-23 13:14:11 -0400656 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400657}
658
Josef Bacik0f9dd462008-09-23 13:14:11 -0400659/*
Sankar P9f556842009-05-14 13:52:22 -0400660 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400661 */
Chris Masond3977122009-01-05 21:25:51 -0500662struct btrfs_block_group_cache *btrfs_lookup_block_group(
663 struct btrfs_fs_info *info,
664 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400665{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400666 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400667
Josef Bacik0f9dd462008-09-23 13:14:11 -0400668 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400669
Josef Bacik0f9dd462008-09-23 13:14:11 -0400670 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400671}
Chris Mason0b86a832008-03-24 15:01:56 -0400672
Josef Bacik0f9dd462008-09-23 13:14:11 -0400673static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
674 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400675{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400676 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400677 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400678
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200679 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400680
Chris Mason4184ea72009-03-10 12:39:20 -0400681 rcu_read_lock();
682 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400683 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400684 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400685 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400686 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400687 }
Chris Mason4184ea72009-03-10 12:39:20 -0400688 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400689 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400690}
691
Chris Mason4184ea72009-03-10 12:39:20 -0400692/*
693 * after adding space to the filesystem, we need to clear the full flags
694 * on all the space infos.
695 */
696void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
697{
698 struct list_head *head = &info->space_info;
699 struct btrfs_space_info *found;
700
701 rcu_read_lock();
702 list_for_each_entry_rcu(found, head, list)
703 found->full = 0;
704 rcu_read_unlock();
705}
706
Chris Masone02119d2008-09-05 16:13:11 -0400707/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400708int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400709{
710 int ret;
711 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400712 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400713
Zheng Yan31840ae2008-09-23 13:14:14 -0400714 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700715 if (!path)
716 return -ENOMEM;
717
Chris Masone02119d2008-09-05 16:13:11 -0400718 key.objectid = start;
719 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500720 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400721 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
722 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500723 if (ret > 0) {
724 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
725 if (key.objectid == start &&
726 key.type == BTRFS_METADATA_ITEM_KEY)
727 ret = 0;
728 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400729 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500730 return ret;
731}
732
Chris Masond8d5f3e2007-12-11 12:42:00 -0500733/*
Josef Bacik3173a182013-03-07 14:22:04 -0500734 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400735 *
736 * the head node for delayed ref is used to store the sum of all the
737 * reference count modifications queued up in the rbtree. the head
738 * node may also store the extent flags to set. This way you can check
739 * to see what the reference count and extent flags would be if all of
740 * the delayed refs are not processed.
741 */
742int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500744 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400745{
746 struct btrfs_delayed_ref_head *head;
747 struct btrfs_delayed_ref_root *delayed_refs;
748 struct btrfs_path *path;
749 struct btrfs_extent_item *ei;
750 struct extent_buffer *leaf;
751 struct btrfs_key key;
752 u32 item_size;
753 u64 num_refs;
754 u64 extent_flags;
755 int ret;
756
Josef Bacik3173a182013-03-07 14:22:04 -0500757 /*
758 * If we don't have skinny metadata, don't bother doing anything
759 * different
760 */
761 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
762 offset = root->leafsize;
763 metadata = 0;
764 }
765
Yan, Zhenga22285a2010-05-16 10:48:46 -0400766 path = btrfs_alloc_path();
767 if (!path)
768 return -ENOMEM;
769
Yan, Zhenga22285a2010-05-16 10:48:46 -0400770 if (!trans) {
771 path->skip_locking = 1;
772 path->search_commit_root = 1;
773 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000774
775search_again:
776 key.objectid = bytenr;
777 key.offset = offset;
778 if (metadata)
779 key.type = BTRFS_METADATA_ITEM_KEY;
780 else
781 key.type = BTRFS_EXTENT_ITEM_KEY;
782
Yan, Zhenga22285a2010-05-16 10:48:46 -0400783again:
784 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
785 &key, path, 0, 0);
786 if (ret < 0)
787 goto out_free;
788
Josef Bacik3173a182013-03-07 14:22:04 -0500789 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100790 if (path->slots[0]) {
791 path->slots[0]--;
792 btrfs_item_key_to_cpu(path->nodes[0], &key,
793 path->slots[0]);
794 if (key.objectid == bytenr &&
795 key.type == BTRFS_EXTENT_ITEM_KEY &&
796 key.offset == root->leafsize)
797 ret = 0;
798 }
799 if (ret) {
800 key.objectid = bytenr;
801 key.type = BTRFS_EXTENT_ITEM_KEY;
802 key.offset = root->leafsize;
803 btrfs_release_path(path);
804 goto again;
805 }
Josef Bacik3173a182013-03-07 14:22:04 -0500806 }
807
Yan, Zhenga22285a2010-05-16 10:48:46 -0400808 if (ret == 0) {
809 leaf = path->nodes[0];
810 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
811 if (item_size >= sizeof(*ei)) {
812 ei = btrfs_item_ptr(leaf, path->slots[0],
813 struct btrfs_extent_item);
814 num_refs = btrfs_extent_refs(leaf, ei);
815 extent_flags = btrfs_extent_flags(leaf, ei);
816 } else {
817#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
818 struct btrfs_extent_item_v0 *ei0;
819 BUG_ON(item_size != sizeof(*ei0));
820 ei0 = btrfs_item_ptr(leaf, path->slots[0],
821 struct btrfs_extent_item_v0);
822 num_refs = btrfs_extent_refs_v0(leaf, ei0);
823 /* FIXME: this isn't correct for data */
824 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
825#else
826 BUG();
827#endif
828 }
829 BUG_ON(num_refs == 0);
830 } else {
831 num_refs = 0;
832 extent_flags = 0;
833 ret = 0;
834 }
835
836 if (!trans)
837 goto out;
838
839 delayed_refs = &trans->transaction->delayed_refs;
840 spin_lock(&delayed_refs->lock);
841 head = btrfs_find_delayed_ref_head(trans, bytenr);
842 if (head) {
843 if (!mutex_trylock(&head->mutex)) {
844 atomic_inc(&head->node.refs);
845 spin_unlock(&delayed_refs->lock);
846
David Sterbab3b4aa72011-04-21 01:20:15 +0200847 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400848
David Sterba8cc33e52011-05-02 15:29:25 +0200849 /*
850 * Mutex was contended, block until it's released and try
851 * again
852 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400853 mutex_lock(&head->mutex);
854 mutex_unlock(&head->mutex);
855 btrfs_put_delayed_ref(&head->node);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000856 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400857 }
858 if (head->extent_op && head->extent_op->update_flags)
859 extent_flags |= head->extent_op->flags_to_set;
860 else
861 BUG_ON(num_refs == 0);
862
863 num_refs += head->node.ref_mod;
864 mutex_unlock(&head->mutex);
865 }
866 spin_unlock(&delayed_refs->lock);
867out:
868 WARN_ON(num_refs == 0);
869 if (refs)
870 *refs = num_refs;
871 if (flags)
872 *flags = extent_flags;
873out_free:
874 btrfs_free_path(path);
875 return ret;
876}
877
878/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500879 * Back reference rules. Back refs have three main goals:
880 *
881 * 1) differentiate between all holders of references to an extent so that
882 * when a reference is dropped we can make sure it was a valid reference
883 * before freeing the extent.
884 *
885 * 2) Provide enough information to quickly find the holders of an extent
886 * if we notice a given block is corrupted or bad.
887 *
888 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
889 * maintenance. This is actually the same as #2, but with a slightly
890 * different use case.
891 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400892 * There are two kinds of back refs. The implicit back refs is optimized
893 * for pointers in non-shared tree blocks. For a given pointer in a block,
894 * back refs of this kind provide information about the block's owner tree
895 * and the pointer's key. These information allow us to find the block by
896 * b-tree searching. The full back refs is for pointers in tree blocks not
897 * referenced by their owner trees. The location of tree block is recorded
898 * in the back refs. Actually the full back refs is generic, and can be
899 * used in all cases the implicit back refs is used. The major shortcoming
900 * of the full back refs is its overhead. Every time a tree block gets
901 * COWed, we have to update back refs entry for all pointers in it.
902 *
903 * For a newly allocated tree block, we use implicit back refs for
904 * pointers in it. This means most tree related operations only involve
905 * implicit back refs. For a tree block created in old transaction, the
906 * only way to drop a reference to it is COW it. So we can detect the
907 * event that tree block loses its owner tree's reference and do the
908 * back refs conversion.
909 *
910 * When a tree block is COW'd through a tree, there are four cases:
911 *
912 * The reference count of the block is one and the tree is the block's
913 * owner tree. Nothing to do in this case.
914 *
915 * The reference count of the block is one and the tree is not the
916 * block's owner tree. In this case, full back refs is used for pointers
917 * in the block. Remove these full back refs, add implicit back refs for
918 * every pointers in the new block.
919 *
920 * The reference count of the block is greater than one and the tree is
921 * the block's owner tree. In this case, implicit back refs is used for
922 * pointers in the block. Add full back refs for every pointers in the
923 * block, increase lower level extents' reference counts. The original
924 * implicit back refs are entailed to the new block.
925 *
926 * The reference count of the block is greater than one and the tree is
927 * not the block's owner tree. Add implicit back refs for every pointer in
928 * the new block, increase lower level extents' reference count.
929 *
930 * Back Reference Key composing:
931 *
932 * The key objectid corresponds to the first byte in the extent,
933 * The key type is used to differentiate between types of back refs.
934 * There are different meanings of the key offset for different types
935 * of back refs.
936 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500937 * File extents can be referenced by:
938 *
939 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400940 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500941 * - different offsets inside a file (bookend extents in file.c)
942 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400943 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500944 *
945 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500946 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947 * - original offset in the file
948 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400949 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 * The key offset for the implicit back refs is hash of the first
951 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500952 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500954 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400955 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500956 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957 * The key offset for the implicit back refs is the first byte of
958 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500959 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400960 * When a file extent is allocated, The implicit back refs is used.
961 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500962 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 * (root_key.objectid, inode objectid, offset in file, 1)
964 *
965 * When a file extent is removed file truncation, we find the
966 * corresponding implicit back refs and check the following fields:
967 *
968 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500969 *
970 * Btree extents can be referenced by:
971 *
972 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500973 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400974 * Both the implicit back refs and the full back refs for tree blocks
975 * only consist of key. The key offset for the implicit back refs is
976 * objectid of block's owner tree. The key offset for the full back refs
977 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500978 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400979 * When implicit back refs is used, information about the lowest key and
980 * level of the tree block are required. These information are stored in
981 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500982 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400983
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400984#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
985static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 struct btrfs_path *path,
988 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500989{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990 struct btrfs_extent_item *item;
991 struct btrfs_extent_item_v0 *ei0;
992 struct btrfs_extent_ref_v0 *ref0;
993 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400994 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 struct btrfs_key key;
996 struct btrfs_key found_key;
997 u32 new_size = sizeof(*item);
998 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500999 int ret;
1000
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001 leaf = path->nodes[0];
1002 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001003
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1005 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_extent_item_v0);
1007 refs = btrfs_extent_refs_v0(leaf, ei0);
1008
1009 if (owner == (u64)-1) {
1010 while (1) {
1011 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1012 ret = btrfs_next_leaf(root, path);
1013 if (ret < 0)
1014 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001015 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001016 leaf = path->nodes[0];
1017 }
1018 btrfs_item_key_to_cpu(leaf, &found_key,
1019 path->slots[0]);
1020 BUG_ON(key.objectid != found_key.objectid);
1021 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1022 path->slots[0]++;
1023 continue;
1024 }
1025 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1026 struct btrfs_extent_ref_v0);
1027 owner = btrfs_ref_objectid_v0(leaf, ref0);
1028 break;
1029 }
1030 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001031 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001032
1033 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1034 new_size += sizeof(*bi);
1035
1036 new_size -= sizeof(*ei0);
1037 ret = btrfs_search_slot(trans, root, &key, path,
1038 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001039 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001041 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001042
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001043 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044
1045 leaf = path->nodes[0];
1046 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1047 btrfs_set_extent_refs(leaf, item, refs);
1048 /* FIXME: get real generation */
1049 btrfs_set_extent_generation(leaf, item, 0);
1050 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1051 btrfs_set_extent_flags(leaf, item,
1052 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1053 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1054 bi = (struct btrfs_tree_block_info *)(item + 1);
1055 /* FIXME: get first key of the block */
1056 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1057 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1058 } else {
1059 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1060 }
1061 btrfs_mark_buffer_dirty(leaf);
1062 return 0;
1063}
1064#endif
1065
1066static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1067{
1068 u32 high_crc = ~(u32)0;
1069 u32 low_crc = ~(u32)0;
1070 __le64 lenum;
1071
1072 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001073 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001074 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001075 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001076 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001077 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078
1079 return ((u64)high_crc << 31) ^ (u64)low_crc;
1080}
1081
1082static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1083 struct btrfs_extent_data_ref *ref)
1084{
1085 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1086 btrfs_extent_data_ref_objectid(leaf, ref),
1087 btrfs_extent_data_ref_offset(leaf, ref));
1088}
1089
1090static int match_extent_data_ref(struct extent_buffer *leaf,
1091 struct btrfs_extent_data_ref *ref,
1092 u64 root_objectid, u64 owner, u64 offset)
1093{
1094 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1095 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1096 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1097 return 0;
1098 return 1;
1099}
1100
1101static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *root,
1103 struct btrfs_path *path,
1104 u64 bytenr, u64 parent,
1105 u64 root_objectid,
1106 u64 owner, u64 offset)
1107{
1108 struct btrfs_key key;
1109 struct btrfs_extent_data_ref *ref;
1110 struct extent_buffer *leaf;
1111 u32 nritems;
1112 int ret;
1113 int recow;
1114 int err = -ENOENT;
1115
1116 key.objectid = bytenr;
1117 if (parent) {
1118 key.type = BTRFS_SHARED_DATA_REF_KEY;
1119 key.offset = parent;
1120 } else {
1121 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1122 key.offset = hash_extent_data_ref(root_objectid,
1123 owner, offset);
1124 }
1125again:
1126 recow = 0;
1127 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1128 if (ret < 0) {
1129 err = ret;
1130 goto fail;
1131 }
1132
1133 if (parent) {
1134 if (!ret)
1135 return 0;
1136#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1137 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001138 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001139 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1140 if (ret < 0) {
1141 err = ret;
1142 goto fail;
1143 }
1144 if (!ret)
1145 return 0;
1146#endif
1147 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001148 }
1149
1150 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001151 nritems = btrfs_header_nritems(leaf);
1152 while (1) {
1153 if (path->slots[0] >= nritems) {
1154 ret = btrfs_next_leaf(root, path);
1155 if (ret < 0)
1156 err = ret;
1157 if (ret)
1158 goto fail;
1159
1160 leaf = path->nodes[0];
1161 nritems = btrfs_header_nritems(leaf);
1162 recow = 1;
1163 }
1164
1165 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1166 if (key.objectid != bytenr ||
1167 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1168 goto fail;
1169
1170 ref = btrfs_item_ptr(leaf, path->slots[0],
1171 struct btrfs_extent_data_ref);
1172
1173 if (match_extent_data_ref(leaf, ref, root_objectid,
1174 owner, offset)) {
1175 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001176 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001177 goto again;
1178 }
1179 err = 0;
1180 break;
1181 }
1182 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001183 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001184fail:
1185 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001186}
1187
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001188static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root,
1190 struct btrfs_path *path,
1191 u64 bytenr, u64 parent,
1192 u64 root_objectid, u64 owner,
1193 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001194{
1195 struct btrfs_key key;
1196 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001197 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001198 u32 num_refs;
1199 int ret;
1200
1201 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001202 if (parent) {
1203 key.type = BTRFS_SHARED_DATA_REF_KEY;
1204 key.offset = parent;
1205 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001206 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001207 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1208 key.offset = hash_extent_data_ref(root_objectid,
1209 owner, offset);
1210 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001211 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001212
1213 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1214 if (ret && ret != -EEXIST)
1215 goto fail;
1216
1217 leaf = path->nodes[0];
1218 if (parent) {
1219 struct btrfs_shared_data_ref *ref;
1220 ref = btrfs_item_ptr(leaf, path->slots[0],
1221 struct btrfs_shared_data_ref);
1222 if (ret == 0) {
1223 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1224 } else {
1225 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1226 num_refs += refs_to_add;
1227 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1228 }
1229 } else {
1230 struct btrfs_extent_data_ref *ref;
1231 while (ret == -EEXIST) {
1232 ref = btrfs_item_ptr(leaf, path->slots[0],
1233 struct btrfs_extent_data_ref);
1234 if (match_extent_data_ref(leaf, ref, root_objectid,
1235 owner, offset))
1236 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001237 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001238 key.offset++;
1239 ret = btrfs_insert_empty_item(trans, root, path, &key,
1240 size);
1241 if (ret && ret != -EEXIST)
1242 goto fail;
1243
1244 leaf = path->nodes[0];
1245 }
1246 ref = btrfs_item_ptr(leaf, path->slots[0],
1247 struct btrfs_extent_data_ref);
1248 if (ret == 0) {
1249 btrfs_set_extent_data_ref_root(leaf, ref,
1250 root_objectid);
1251 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1252 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1253 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1254 } else {
1255 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1256 num_refs += refs_to_add;
1257 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1258 }
1259 }
1260 btrfs_mark_buffer_dirty(leaf);
1261 ret = 0;
1262fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001263 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001264 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001265}
1266
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001267static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1268 struct btrfs_root *root,
1269 struct btrfs_path *path,
1270 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001271{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001272 struct btrfs_key key;
1273 struct btrfs_extent_data_ref *ref1 = NULL;
1274 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001275 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001276 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001277 int ret = 0;
1278
1279 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001280 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1281
1282 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1283 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1284 struct btrfs_extent_data_ref);
1285 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1286 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1287 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1288 struct btrfs_shared_data_ref);
1289 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1290#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1291 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1292 struct btrfs_extent_ref_v0 *ref0;
1293 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1294 struct btrfs_extent_ref_v0);
1295 num_refs = btrfs_ref_count_v0(leaf, ref0);
1296#endif
1297 } else {
1298 BUG();
1299 }
1300
Chris Mason56bec292009-03-13 10:10:06 -04001301 BUG_ON(num_refs < refs_to_drop);
1302 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001303
Zheng Yan31840ae2008-09-23 13:14:14 -04001304 if (num_refs == 0) {
1305 ret = btrfs_del_item(trans, root, path);
1306 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001307 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1308 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1309 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1310 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1311#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1312 else {
1313 struct btrfs_extent_ref_v0 *ref0;
1314 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1315 struct btrfs_extent_ref_v0);
1316 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1317 }
1318#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001319 btrfs_mark_buffer_dirty(leaf);
1320 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001321 return ret;
1322}
1323
1324static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1325 struct btrfs_path *path,
1326 struct btrfs_extent_inline_ref *iref)
1327{
1328 struct btrfs_key key;
1329 struct extent_buffer *leaf;
1330 struct btrfs_extent_data_ref *ref1;
1331 struct btrfs_shared_data_ref *ref2;
1332 u32 num_refs = 0;
1333
1334 leaf = path->nodes[0];
1335 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1336 if (iref) {
1337 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1338 BTRFS_EXTENT_DATA_REF_KEY) {
1339 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1340 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1341 } else {
1342 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1343 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1344 }
1345 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1346 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1347 struct btrfs_extent_data_ref);
1348 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1349 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1350 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1351 struct btrfs_shared_data_ref);
1352 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1353#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1354 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1355 struct btrfs_extent_ref_v0 *ref0;
1356 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1357 struct btrfs_extent_ref_v0);
1358 num_refs = btrfs_ref_count_v0(leaf, ref0);
1359#endif
1360 } else {
1361 WARN_ON(1);
1362 }
1363 return num_refs;
1364}
1365
1366static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1367 struct btrfs_root *root,
1368 struct btrfs_path *path,
1369 u64 bytenr, u64 parent,
1370 u64 root_objectid)
1371{
1372 struct btrfs_key key;
1373 int ret;
1374
1375 key.objectid = bytenr;
1376 if (parent) {
1377 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1378 key.offset = parent;
1379 } else {
1380 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1381 key.offset = root_objectid;
1382 }
1383
1384 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1385 if (ret > 0)
1386 ret = -ENOENT;
1387#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1388 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001389 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001390 key.type = BTRFS_EXTENT_REF_V0_KEY;
1391 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1392 if (ret > 0)
1393 ret = -ENOENT;
1394 }
1395#endif
1396 return ret;
1397}
1398
1399static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1400 struct btrfs_root *root,
1401 struct btrfs_path *path,
1402 u64 bytenr, u64 parent,
1403 u64 root_objectid)
1404{
1405 struct btrfs_key key;
1406 int ret;
1407
1408 key.objectid = bytenr;
1409 if (parent) {
1410 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1411 key.offset = parent;
1412 } else {
1413 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1414 key.offset = root_objectid;
1415 }
1416
1417 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001418 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001419 return ret;
1420}
1421
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001422static inline int extent_ref_type(u64 parent, u64 owner)
1423{
1424 int type;
1425 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1426 if (parent > 0)
1427 type = BTRFS_SHARED_BLOCK_REF_KEY;
1428 else
1429 type = BTRFS_TREE_BLOCK_REF_KEY;
1430 } else {
1431 if (parent > 0)
1432 type = BTRFS_SHARED_DATA_REF_KEY;
1433 else
1434 type = BTRFS_EXTENT_DATA_REF_KEY;
1435 }
1436 return type;
1437}
1438
Yan Zheng2c47e6052009-06-27 21:07:35 -04001439static int find_next_key(struct btrfs_path *path, int level,
1440 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441
1442{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001443 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001444 if (!path->nodes[level])
1445 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001446 if (path->slots[level] + 1 >=
1447 btrfs_header_nritems(path->nodes[level]))
1448 continue;
1449 if (level == 0)
1450 btrfs_item_key_to_cpu(path->nodes[level], key,
1451 path->slots[level] + 1);
1452 else
1453 btrfs_node_key_to_cpu(path->nodes[level], key,
1454 path->slots[level] + 1);
1455 return 0;
1456 }
1457 return 1;
1458}
1459
1460/*
1461 * look for inline back ref. if back ref is found, *ref_ret is set
1462 * to the address of inline back ref, and 0 is returned.
1463 *
1464 * if back ref isn't found, *ref_ret is set to the address where it
1465 * should be inserted, and -ENOENT is returned.
1466 *
1467 * if insert is true and there are too many inline back refs, the path
1468 * points to the extent item, and -EAGAIN is returned.
1469 *
1470 * NOTE: inline back refs are ordered in the same way that back ref
1471 * items in the tree are ordered.
1472 */
1473static noinline_for_stack
1474int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1475 struct btrfs_root *root,
1476 struct btrfs_path *path,
1477 struct btrfs_extent_inline_ref **ref_ret,
1478 u64 bytenr, u64 num_bytes,
1479 u64 parent, u64 root_objectid,
1480 u64 owner, u64 offset, int insert)
1481{
1482 struct btrfs_key key;
1483 struct extent_buffer *leaf;
1484 struct btrfs_extent_item *ei;
1485 struct btrfs_extent_inline_ref *iref;
1486 u64 flags;
1487 u64 item_size;
1488 unsigned long ptr;
1489 unsigned long end;
1490 int extra_size;
1491 int type;
1492 int want;
1493 int ret;
1494 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001495 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1496 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001497
1498 key.objectid = bytenr;
1499 key.type = BTRFS_EXTENT_ITEM_KEY;
1500 key.offset = num_bytes;
1501
1502 want = extent_ref_type(parent, owner);
1503 if (insert) {
1504 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001505 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 } else
1507 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001508
1509 /*
1510 * Owner is our parent level, so we can just add one to get the level
1511 * for the block we are interested in.
1512 */
1513 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1514 key.type = BTRFS_METADATA_ITEM_KEY;
1515 key.offset = owner;
1516 }
1517
1518again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001519 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1520 if (ret < 0) {
1521 err = ret;
1522 goto out;
1523 }
Josef Bacik3173a182013-03-07 14:22:04 -05001524
1525 /*
1526 * We may be a newly converted file system which still has the old fat
1527 * extent entries for metadata, so try and see if we have one of those.
1528 */
1529 if (ret > 0 && skinny_metadata) {
1530 skinny_metadata = false;
1531 if (path->slots[0]) {
1532 path->slots[0]--;
1533 btrfs_item_key_to_cpu(path->nodes[0], &key,
1534 path->slots[0]);
1535 if (key.objectid == bytenr &&
1536 key.type == BTRFS_EXTENT_ITEM_KEY &&
1537 key.offset == num_bytes)
1538 ret = 0;
1539 }
1540 if (ret) {
1541 key.type = BTRFS_EXTENT_ITEM_KEY;
1542 key.offset = num_bytes;
1543 btrfs_release_path(path);
1544 goto again;
1545 }
1546 }
1547
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001548 if (ret && !insert) {
1549 err = -ENOENT;
1550 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301551 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001552 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001553 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001554 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001555
1556 leaf = path->nodes[0];
1557 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1558#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1559 if (item_size < sizeof(*ei)) {
1560 if (!insert) {
1561 err = -ENOENT;
1562 goto out;
1563 }
1564 ret = convert_extent_item_v0(trans, root, path, owner,
1565 extra_size);
1566 if (ret < 0) {
1567 err = ret;
1568 goto out;
1569 }
1570 leaf = path->nodes[0];
1571 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1572 }
1573#endif
1574 BUG_ON(item_size < sizeof(*ei));
1575
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001576 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1577 flags = btrfs_extent_flags(leaf, ei);
1578
1579 ptr = (unsigned long)(ei + 1);
1580 end = (unsigned long)ei + item_size;
1581
Josef Bacik3173a182013-03-07 14:22:04 -05001582 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001583 ptr += sizeof(struct btrfs_tree_block_info);
1584 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001585 }
1586
1587 err = -ENOENT;
1588 while (1) {
1589 if (ptr >= end) {
1590 WARN_ON(ptr > end);
1591 break;
1592 }
1593 iref = (struct btrfs_extent_inline_ref *)ptr;
1594 type = btrfs_extent_inline_ref_type(leaf, iref);
1595 if (want < type)
1596 break;
1597 if (want > type) {
1598 ptr += btrfs_extent_inline_ref_size(type);
1599 continue;
1600 }
1601
1602 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1603 struct btrfs_extent_data_ref *dref;
1604 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1605 if (match_extent_data_ref(leaf, dref, root_objectid,
1606 owner, offset)) {
1607 err = 0;
1608 break;
1609 }
1610 if (hash_extent_data_ref_item(leaf, dref) <
1611 hash_extent_data_ref(root_objectid, owner, offset))
1612 break;
1613 } else {
1614 u64 ref_offset;
1615 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1616 if (parent > 0) {
1617 if (parent == ref_offset) {
1618 err = 0;
1619 break;
1620 }
1621 if (ref_offset < parent)
1622 break;
1623 } else {
1624 if (root_objectid == ref_offset) {
1625 err = 0;
1626 break;
1627 }
1628 if (ref_offset < root_objectid)
1629 break;
1630 }
1631 }
1632 ptr += btrfs_extent_inline_ref_size(type);
1633 }
1634 if (err == -ENOENT && insert) {
1635 if (item_size + extra_size >=
1636 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1637 err = -EAGAIN;
1638 goto out;
1639 }
1640 /*
1641 * To add new inline back ref, we have to make sure
1642 * there is no corresponding back ref item.
1643 * For simplicity, we just do not add new inline back
1644 * ref if there is any kind of item for this block
1645 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001646 if (find_next_key(path, 0, &key) == 0 &&
1647 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001648 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001649 err = -EAGAIN;
1650 goto out;
1651 }
1652 }
1653 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1654out:
Yan Zheng85d41982009-06-11 08:51:10 -04001655 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001656 path->keep_locks = 0;
1657 btrfs_unlock_up_safe(path, 1);
1658 }
1659 return err;
1660}
1661
1662/*
1663 * helper to add new inline back ref
1664 */
1665static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001666void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001667 struct btrfs_path *path,
1668 struct btrfs_extent_inline_ref *iref,
1669 u64 parent, u64 root_objectid,
1670 u64 owner, u64 offset, int refs_to_add,
1671 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001672{
1673 struct extent_buffer *leaf;
1674 struct btrfs_extent_item *ei;
1675 unsigned long ptr;
1676 unsigned long end;
1677 unsigned long item_offset;
1678 u64 refs;
1679 int size;
1680 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001681
1682 leaf = path->nodes[0];
1683 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1684 item_offset = (unsigned long)iref - (unsigned long)ei;
1685
1686 type = extent_ref_type(parent, owner);
1687 size = btrfs_extent_inline_ref_size(type);
1688
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001689 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001690
1691 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1692 refs = btrfs_extent_refs(leaf, ei);
1693 refs += refs_to_add;
1694 btrfs_set_extent_refs(leaf, ei, refs);
1695 if (extent_op)
1696 __run_delayed_extent_op(extent_op, leaf, ei);
1697
1698 ptr = (unsigned long)ei + item_offset;
1699 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1700 if (ptr < end - size)
1701 memmove_extent_buffer(leaf, ptr + size, ptr,
1702 end - size - ptr);
1703
1704 iref = (struct btrfs_extent_inline_ref *)ptr;
1705 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1706 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1707 struct btrfs_extent_data_ref *dref;
1708 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1709 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1710 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1711 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1712 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1713 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1714 struct btrfs_shared_data_ref *sref;
1715 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1716 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1717 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1718 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1719 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1720 } else {
1721 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1722 }
1723 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001724}
1725
1726static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root,
1728 struct btrfs_path *path,
1729 struct btrfs_extent_inline_ref **ref_ret,
1730 u64 bytenr, u64 num_bytes, u64 parent,
1731 u64 root_objectid, u64 owner, u64 offset)
1732{
1733 int ret;
1734
1735 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1736 bytenr, num_bytes, parent,
1737 root_objectid, owner, offset, 0);
1738 if (ret != -ENOENT)
1739 return ret;
1740
David Sterbab3b4aa72011-04-21 01:20:15 +02001741 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001742 *ref_ret = NULL;
1743
1744 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1745 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1746 root_objectid);
1747 } else {
1748 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1749 root_objectid, owner, offset);
1750 }
1751 return ret;
1752}
1753
1754/*
1755 * helper to update/remove inline back ref
1756 */
1757static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001758void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001759 struct btrfs_path *path,
1760 struct btrfs_extent_inline_ref *iref,
1761 int refs_to_mod,
1762 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001763{
1764 struct extent_buffer *leaf;
1765 struct btrfs_extent_item *ei;
1766 struct btrfs_extent_data_ref *dref = NULL;
1767 struct btrfs_shared_data_ref *sref = NULL;
1768 unsigned long ptr;
1769 unsigned long end;
1770 u32 item_size;
1771 int size;
1772 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001773 u64 refs;
1774
1775 leaf = path->nodes[0];
1776 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1777 refs = btrfs_extent_refs(leaf, ei);
1778 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1779 refs += refs_to_mod;
1780 btrfs_set_extent_refs(leaf, ei, refs);
1781 if (extent_op)
1782 __run_delayed_extent_op(extent_op, leaf, ei);
1783
1784 type = btrfs_extent_inline_ref_type(leaf, iref);
1785
1786 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1787 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1788 refs = btrfs_extent_data_ref_count(leaf, dref);
1789 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1790 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1791 refs = btrfs_shared_data_ref_count(leaf, sref);
1792 } else {
1793 refs = 1;
1794 BUG_ON(refs_to_mod != -1);
1795 }
1796
1797 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1798 refs += refs_to_mod;
1799
1800 if (refs > 0) {
1801 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1802 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1803 else
1804 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1805 } else {
1806 size = btrfs_extent_inline_ref_size(type);
1807 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1808 ptr = (unsigned long)iref;
1809 end = (unsigned long)ei + item_size;
1810 if (ptr + size < end)
1811 memmove_extent_buffer(leaf, ptr, ptr + size,
1812 end - ptr - size);
1813 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001814 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001815 }
1816 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001817}
1818
1819static noinline_for_stack
1820int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1821 struct btrfs_root *root,
1822 struct btrfs_path *path,
1823 u64 bytenr, u64 num_bytes, u64 parent,
1824 u64 root_objectid, u64 owner,
1825 u64 offset, int refs_to_add,
1826 struct btrfs_delayed_extent_op *extent_op)
1827{
1828 struct btrfs_extent_inline_ref *iref;
1829 int ret;
1830
1831 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1832 bytenr, num_bytes, parent,
1833 root_objectid, owner, offset, 1);
1834 if (ret == 0) {
1835 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001836 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001837 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001838 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001839 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001840 root_objectid, owner, offset,
1841 refs_to_add, extent_op);
1842 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001843 }
1844 return ret;
1845}
1846
1847static int insert_extent_backref(struct btrfs_trans_handle *trans,
1848 struct btrfs_root *root,
1849 struct btrfs_path *path,
1850 u64 bytenr, u64 parent, u64 root_objectid,
1851 u64 owner, u64 offset, int refs_to_add)
1852{
1853 int ret;
1854 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1855 BUG_ON(refs_to_add != 1);
1856 ret = insert_tree_block_ref(trans, root, path, bytenr,
1857 parent, root_objectid);
1858 } else {
1859 ret = insert_extent_data_ref(trans, root, path, bytenr,
1860 parent, root_objectid,
1861 owner, offset, refs_to_add);
1862 }
1863 return ret;
1864}
1865
1866static int remove_extent_backref(struct btrfs_trans_handle *trans,
1867 struct btrfs_root *root,
1868 struct btrfs_path *path,
1869 struct btrfs_extent_inline_ref *iref,
1870 int refs_to_drop, int is_data)
1871{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001872 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001873
1874 BUG_ON(!is_data && refs_to_drop != 1);
1875 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001876 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001877 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 } else if (is_data) {
1879 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1880 } else {
1881 ret = btrfs_del_item(trans, root, path);
1882 }
1883 return ret;
1884}
1885
Li Dongyang5378e602011-03-24 10:24:27 +00001886static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001887 u64 start, u64 len)
1888{
Li Dongyang5378e602011-03-24 10:24:27 +00001889 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001890}
Chris Mason15916de2008-11-19 21:17:22 -05001891
Liu Hui1f3c79a2009-01-05 15:57:51 -05001892static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001893 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001894{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001895 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001896 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001897 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001898
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001899
Liu Hui1f3c79a2009-01-05 15:57:51 -05001900 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001901 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001902 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001903 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001904 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001905 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001906 int i;
1907
Liu Hui1f3c79a2009-01-05 15:57:51 -05001908
Jan Schmidta1d3c472011-08-04 17:15:33 +02001909 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001910 if (!stripe->dev->can_discard)
1911 continue;
1912
Li Dongyang5378e602011-03-24 10:24:27 +00001913 ret = btrfs_issue_discard(stripe->dev->bdev,
1914 stripe->physical,
1915 stripe->length);
1916 if (!ret)
1917 discarded_bytes += stripe->length;
1918 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001919 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001920
1921 /*
1922 * Just in case we get back EOPNOTSUPP for some reason,
1923 * just ignore the return value so we don't screw up
1924 * people calling discard_extent.
1925 */
1926 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001927 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001928 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001929 }
Li Dongyang5378e602011-03-24 10:24:27 +00001930
1931 if (actual_bytes)
1932 *actual_bytes = discarded_bytes;
1933
Liu Hui1f3c79a2009-01-05 15:57:51 -05001934
David Woodhouse53b381b2013-01-29 18:40:14 -05001935 if (ret == -EOPNOTSUPP)
1936 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001937 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001938}
1939
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001940/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001941int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1942 struct btrfs_root *root,
1943 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001944 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001945{
1946 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001947 struct btrfs_fs_info *fs_info = root->fs_info;
1948
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001949 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1950 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001951
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001953 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1954 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001955 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001956 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001957 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001958 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1959 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001960 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001961 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001962 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001963 return ret;
1964}
1965
Chris Mason925baed2008-06-25 16:01:30 -04001966static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001967 struct btrfs_root *root,
1968 u64 bytenr, u64 num_bytes,
1969 u64 parent, u64 root_objectid,
1970 u64 owner, u64 offset, int refs_to_add,
1971 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001972{
Chris Mason5caf2a02007-04-02 11:20:42 -04001973 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001974 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001975 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 u64 refs;
1977 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001978
Chris Mason5caf2a02007-04-02 11:20:42 -04001979 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001980 if (!path)
1981 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001982
Chris Mason3c12ac72008-04-21 12:01:38 -04001983 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001984 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001985 /* this will setup the path even if it fails to insert the back ref */
1986 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1987 path, bytenr, num_bytes, parent,
1988 root_objectid, owner, offset,
1989 refs_to_add, extent_op);
Liu Bo30d133f2013-10-11 16:30:23 +08001990 if (ret != -EAGAIN)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001991 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001992
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001993 leaf = path->nodes[0];
1994 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1995 refs = btrfs_extent_refs(leaf, item);
1996 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1997 if (extent_op)
1998 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04001999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002000 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002001 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002002
Chris Mason3c12ac72008-04-21 12:01:38 -04002003 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002004 path->leave_spinning = 1;
2005
Chris Mason56bec292009-03-13 10:10:06 -04002006 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002007 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008 path, bytenr, parent, root_objectid,
2009 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002010 if (ret)
2011 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002012out:
Chris Mason74493f72007-12-11 09:25:06 -05002013 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002014 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002015}
2016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002017static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2018 struct btrfs_root *root,
2019 struct btrfs_delayed_ref_node *node,
2020 struct btrfs_delayed_extent_op *extent_op,
2021 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002022{
Chris Mason56bec292009-03-13 10:10:06 -04002023 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002024 struct btrfs_delayed_data_ref *ref;
2025 struct btrfs_key ins;
2026 u64 parent = 0;
2027 u64 ref_root = 0;
2028 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002029
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002030 ins.objectid = node->bytenr;
2031 ins.offset = node->num_bytes;
2032 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002033
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002034 ref = btrfs_delayed_node_to_data_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002035 trace_run_delayed_data_ref(node, ref, node->action);
2036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2038 parent = ref->parent;
2039 else
2040 ref_root = ref->root;
2041
2042 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002043 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002045 ret = alloc_reserved_file_extent(trans, root,
2046 parent, ref_root, flags,
2047 ref->objectid, ref->offset,
2048 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002049 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2050 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2051 node->num_bytes, parent,
2052 ref_root, ref->objectid,
2053 ref->offset, node->ref_mod,
2054 extent_op);
2055 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2056 ret = __btrfs_free_extent(trans, root, node->bytenr,
2057 node->num_bytes, parent,
2058 ref_root, ref->objectid,
2059 ref->offset, node->ref_mod,
2060 extent_op);
2061 } else {
2062 BUG();
2063 }
Chris Mason56bec292009-03-13 10:10:06 -04002064 return ret;
2065}
2066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002067static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2068 struct extent_buffer *leaf,
2069 struct btrfs_extent_item *ei)
2070{
2071 u64 flags = btrfs_extent_flags(leaf, ei);
2072 if (extent_op->update_flags) {
2073 flags |= extent_op->flags_to_set;
2074 btrfs_set_extent_flags(leaf, ei, flags);
2075 }
2076
2077 if (extent_op->update_key) {
2078 struct btrfs_tree_block_info *bi;
2079 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2080 bi = (struct btrfs_tree_block_info *)(ei + 1);
2081 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2082 }
2083}
2084
2085static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2086 struct btrfs_root *root,
2087 struct btrfs_delayed_ref_node *node,
2088 struct btrfs_delayed_extent_op *extent_op)
2089{
2090 struct btrfs_key key;
2091 struct btrfs_path *path;
2092 struct btrfs_extent_item *ei;
2093 struct extent_buffer *leaf;
2094 u32 item_size;
2095 int ret;
2096 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002097 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002098
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002099 if (trans->aborted)
2100 return 0;
2101
Josef Bacik3173a182013-03-07 14:22:04 -05002102 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2103 metadata = 0;
2104
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002105 path = btrfs_alloc_path();
2106 if (!path)
2107 return -ENOMEM;
2108
2109 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002110
Josef Bacik3173a182013-03-07 14:22:04 -05002111 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002112 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002113 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002114 } else {
2115 key.type = BTRFS_EXTENT_ITEM_KEY;
2116 key.offset = node->num_bytes;
2117 }
2118
2119again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002120 path->reada = 1;
2121 path->leave_spinning = 1;
2122 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2123 path, 0, 1);
2124 if (ret < 0) {
2125 err = ret;
2126 goto out;
2127 }
2128 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002129 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002130 if (path->slots[0] > 0) {
2131 path->slots[0]--;
2132 btrfs_item_key_to_cpu(path->nodes[0], &key,
2133 path->slots[0]);
2134 if (key.objectid == node->bytenr &&
2135 key.type == BTRFS_EXTENT_ITEM_KEY &&
2136 key.offset == node->num_bytes)
2137 ret = 0;
2138 }
2139 if (ret > 0) {
2140 btrfs_release_path(path);
2141 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002142
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002143 key.objectid = node->bytenr;
2144 key.offset = node->num_bytes;
2145 key.type = BTRFS_EXTENT_ITEM_KEY;
2146 goto again;
2147 }
2148 } else {
2149 err = -EIO;
2150 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002151 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002152 }
2153
2154 leaf = path->nodes[0];
2155 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2156#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2157 if (item_size < sizeof(*ei)) {
2158 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2159 path, (u64)-1, 0);
2160 if (ret < 0) {
2161 err = ret;
2162 goto out;
2163 }
2164 leaf = path->nodes[0];
2165 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2166 }
2167#endif
2168 BUG_ON(item_size < sizeof(*ei));
2169 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2170 __run_delayed_extent_op(extent_op, leaf, ei);
2171
2172 btrfs_mark_buffer_dirty(leaf);
2173out:
2174 btrfs_free_path(path);
2175 return err;
2176}
2177
2178static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2179 struct btrfs_root *root,
2180 struct btrfs_delayed_ref_node *node,
2181 struct btrfs_delayed_extent_op *extent_op,
2182 int insert_reserved)
2183{
2184 int ret = 0;
2185 struct btrfs_delayed_tree_ref *ref;
2186 struct btrfs_key ins;
2187 u64 parent = 0;
2188 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002189 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2190 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002191
2192 ref = btrfs_delayed_node_to_tree_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002193 trace_run_delayed_tree_ref(node, ref, node->action);
2194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2196 parent = ref->parent;
2197 else
2198 ref_root = ref->root;
2199
Josef Bacik3173a182013-03-07 14:22:04 -05002200 ins.objectid = node->bytenr;
2201 if (skinny_metadata) {
2202 ins.offset = ref->level;
2203 ins.type = BTRFS_METADATA_ITEM_KEY;
2204 } else {
2205 ins.offset = node->num_bytes;
2206 ins.type = BTRFS_EXTENT_ITEM_KEY;
2207 }
2208
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002209 BUG_ON(node->ref_mod != 1);
2210 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002211 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002212 ret = alloc_reserved_tree_block(trans, root,
2213 parent, ref_root,
2214 extent_op->flags_to_set,
2215 &extent_op->key,
2216 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2218 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2219 node->num_bytes, parent, ref_root,
2220 ref->level, 0, 1, extent_op);
2221 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2222 ret = __btrfs_free_extent(trans, root, node->bytenr,
2223 node->num_bytes, parent, ref_root,
2224 ref->level, 0, 1, extent_op);
2225 } else {
2226 BUG();
2227 }
2228 return ret;
2229}
2230
Chris Mason56bec292009-03-13 10:10:06 -04002231/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002232static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2233 struct btrfs_root *root,
2234 struct btrfs_delayed_ref_node *node,
2235 struct btrfs_delayed_extent_op *extent_op,
2236 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002237{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002238 int ret = 0;
2239
Josef Bacik857cc2f2013-10-07 15:21:08 -04002240 if (trans->aborted) {
2241 if (insert_reserved)
2242 btrfs_pin_extent(root, node->bytenr,
2243 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002244 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002245 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002246
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002248 struct btrfs_delayed_ref_head *head;
2249 /*
2250 * we've hit the end of the chain and we were supposed
2251 * to insert this extent into the tree. But, it got
2252 * deleted before we ever needed to insert it, so all
2253 * we have to do is clean up the accounting
2254 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002255 BUG_ON(extent_op);
2256 head = btrfs_delayed_node_to_head(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002257 trace_run_delayed_ref_head(node, head, node->action);
2258
Chris Mason56bec292009-03-13 10:10:06 -04002259 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002260 btrfs_pin_extent(root, node->bytenr,
2261 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002262 if (head->is_data) {
2263 ret = btrfs_del_csums(trans, root,
2264 node->bytenr,
2265 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002266 }
Chris Mason56bec292009-03-13 10:10:06 -04002267 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002268 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002269 }
Josef Bacikeb099672009-02-12 09:27:38 -05002270
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002271 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2272 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2273 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2274 insert_reserved);
2275 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2276 node->type == BTRFS_SHARED_DATA_REF_KEY)
2277 ret = run_delayed_data_ref(trans, root, node, extent_op,
2278 insert_reserved);
2279 else
2280 BUG();
2281 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002282}
2283
Chris Mason56bec292009-03-13 10:10:06 -04002284static noinline struct btrfs_delayed_ref_node *
2285select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002286{
Chris Mason56bec292009-03-13 10:10:06 -04002287 struct rb_node *node;
2288 struct btrfs_delayed_ref_node *ref;
2289 int action = BTRFS_ADD_DELAYED_REF;
2290again:
2291 /*
2292 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2293 * this prevents ref count from going down to zero when
2294 * there still are pending delayed ref.
2295 */
2296 node = rb_prev(&head->node.rb_node);
2297 while (1) {
2298 if (!node)
2299 break;
2300 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2301 rb_node);
2302 if (ref->bytenr != head->node.bytenr)
2303 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002304 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002305 return ref;
2306 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002307 }
Chris Mason56bec292009-03-13 10:10:06 -04002308 if (action == BTRFS_ADD_DELAYED_REF) {
2309 action = BTRFS_DROP_DELAYED_REF;
2310 goto again;
2311 }
2312 return NULL;
2313}
2314
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002315/*
2316 * Returns 0 on success or if called with an already aborted transaction.
2317 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2318 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002319static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2320 struct btrfs_root *root,
2321 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002322{
Chris Mason56bec292009-03-13 10:10:06 -04002323 struct btrfs_delayed_ref_root *delayed_refs;
2324 struct btrfs_delayed_ref_node *ref;
2325 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002326 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002327 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002328 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002329 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002330 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002331
2332 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002333 while (1) {
2334 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002335 /* pick a new head ref from the cluster list */
2336 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002337 break;
Chris Mason56bec292009-03-13 10:10:06 -04002338
Chris Masonc3e69d52009-03-13 10:17:05 -04002339 locked_ref = list_entry(cluster->next,
2340 struct btrfs_delayed_ref_head, cluster);
2341
2342 /* grab the lock that says we are going to process
2343 * all the refs for this head */
2344 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2345
2346 /*
2347 * we may have dropped the spin lock to get the head
2348 * mutex lock, and that might have given someone else
2349 * time to free the head. If that's true, it has been
2350 * removed from our list and we can move on.
2351 */
2352 if (ret == -EAGAIN) {
2353 locked_ref = NULL;
2354 count++;
2355 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002356 }
2357 }
2358
2359 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002360 * We need to try and merge add/drops of the same ref since we
2361 * can run into issues with relocate dropping the implicit ref
2362 * and then it being added back again before the drop can
2363 * finish. If we merged anything we need to re-loop so we can
2364 * get a good ref.
2365 */
2366 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2367 locked_ref);
2368
2369 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002370 * locked_ref is the head node, so we have to go one
2371 * node back for any delayed ref updates
2372 */
2373 ref = select_delayed_ref(locked_ref);
2374
2375 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002376 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002377 /*
2378 * there are still refs with lower seq numbers in the
2379 * process of being added. Don't run this ref yet.
2380 */
2381 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002382 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002383 locked_ref = NULL;
2384 delayed_refs->num_heads_ready++;
2385 spin_unlock(&delayed_refs->lock);
2386 cond_resched();
2387 spin_lock(&delayed_refs->lock);
2388 continue;
2389 }
2390
2391 /*
Chris Mason56bec292009-03-13 10:10:06 -04002392 * record the must insert reserved flag before we
2393 * drop the spin lock.
2394 */
2395 must_insert_reserved = locked_ref->must_insert_reserved;
2396 locked_ref->must_insert_reserved = 0;
2397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002398 extent_op = locked_ref->extent_op;
2399 locked_ref->extent_op = NULL;
2400
Chris Mason56bec292009-03-13 10:10:06 -04002401 if (!ref) {
2402 /* All delayed refs have been processed, Go ahead
2403 * and send the head node to run_one_delayed_ref,
2404 * so that any accounting fixes can happen
2405 */
2406 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002407
2408 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002409 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002410 extent_op = NULL;
2411 }
2412
2413 if (extent_op) {
2414 spin_unlock(&delayed_refs->lock);
2415
2416 ret = run_delayed_extent_op(trans, root,
2417 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002418 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002419
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002420 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002421 /*
2422 * Need to reset must_insert_reserved if
2423 * there was an error so the abort stuff
2424 * can cleanup the reserved space
2425 * properly.
2426 */
2427 if (must_insert_reserved)
2428 locked_ref->must_insert_reserved = 1;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002429 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002430 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002431 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002432 return ret;
2433 }
2434
Chris Mason203bf282012-01-06 15:23:57 -05002435 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002436 }
Chris Mason56bec292009-03-13 10:10:06 -04002437 }
2438
2439 ref->in_tree = 0;
2440 rb_erase(&ref->rb_node, &delayed_refs->root);
2441 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002442 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002443 /*
2444 * when we play the delayed ref, also correct the
2445 * ref_mod on head
2446 */
2447 switch (ref->action) {
2448 case BTRFS_ADD_DELAYED_REF:
2449 case BTRFS_ADD_DELAYED_EXTENT:
2450 locked_ref->node.ref_mod -= ref->ref_mod;
2451 break;
2452 case BTRFS_DROP_DELAYED_REF:
2453 locked_ref->node.ref_mod += ref->ref_mod;
2454 break;
2455 default:
2456 WARN_ON(1);
2457 }
Josef Bacikb8d0c692013-08-22 17:03:29 -04002458 } else {
2459 list_del_init(&locked_ref->cluster);
Arne Jansen22cd2e72012-08-09 00:16:53 -06002460 }
Chris Mason56bec292009-03-13 10:10:06 -04002461 spin_unlock(&delayed_refs->lock);
2462
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002463 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002464 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002465
Miao Xie78a61842012-11-21 02:21:28 +00002466 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002467 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002468 btrfs_delayed_ref_unlock(locked_ref);
2469 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002470 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002471 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002472 return ret;
2473 }
2474
Miao Xie093486c2012-12-19 08:10:10 +00002475 /*
2476 * If this node is a head, that means all the refs in this head
2477 * have been dealt with, and we will pick the next head to deal
2478 * with, so we must unlock the head and drop it from the cluster
2479 * list before we release it.
2480 */
2481 if (btrfs_delayed_ref_is_head(ref)) {
Miao Xie093486c2012-12-19 08:10:10 +00002482 btrfs_delayed_ref_unlock(locked_ref);
2483 locked_ref = NULL;
2484 }
2485 btrfs_put_delayed_ref(ref);
2486 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002487next:
Chris Mason1887be62009-03-13 10:11:24 -04002488 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002489 spin_lock(&delayed_refs->lock);
2490 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002491 return count;
2492}
2493
Arne Jansen709c0482011-09-12 12:22:57 +02002494#ifdef SCRAMBLE_DELAYED_REFS
2495/*
2496 * Normally delayed refs get processed in ascending bytenr order. This
2497 * correlates in most cases to the order added. To expose dependencies on this
2498 * order, we start to process the tree in the middle instead of the beginning
2499 */
2500static u64 find_middle(struct rb_root *root)
2501{
2502 struct rb_node *n = root->rb_node;
2503 struct btrfs_delayed_ref_node *entry;
2504 int alt = 1;
2505 u64 middle;
2506 u64 first = 0, last = 0;
2507
2508 n = rb_first(root);
2509 if (n) {
2510 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2511 first = entry->bytenr;
2512 }
2513 n = rb_last(root);
2514 if (n) {
2515 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2516 last = entry->bytenr;
2517 }
2518 n = root->rb_node;
2519
2520 while (n) {
2521 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2522 WARN_ON(!entry->in_tree);
2523
2524 middle = entry->bytenr;
2525
2526 if (alt)
2527 n = n->rb_left;
2528 else
2529 n = n->rb_right;
2530
2531 alt = 1 - alt;
2532 }
2533 return middle;
2534}
2535#endif
2536
Arne Jansenbed92ea2012-06-28 18:03:02 +02002537int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2538 struct btrfs_fs_info *fs_info)
2539{
2540 struct qgroup_update *qgroup_update;
2541 int ret = 0;
2542
2543 if (list_empty(&trans->qgroup_ref_list) !=
2544 !trans->delayed_ref_elem.seq) {
2545 /* list without seq or seq without list */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002546 btrfs_err(fs_info,
Jan Schmidtfc36ed72013-04-24 16:57:33 +00002547 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002548 list_empty(&trans->qgroup_ref_list) ? "" : " not",
Jan Schmidtfc36ed72013-04-24 16:57:33 +00002549 (u32)(trans->delayed_ref_elem.seq >> 32),
2550 (u32)trans->delayed_ref_elem.seq);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002551 BUG();
2552 }
2553
2554 if (!trans->delayed_ref_elem.seq)
2555 return 0;
2556
2557 while (!list_empty(&trans->qgroup_ref_list)) {
2558 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2559 struct qgroup_update, list);
2560 list_del(&qgroup_update->list);
2561 if (!ret)
2562 ret = btrfs_qgroup_account_ref(
2563 trans, fs_info, qgroup_update->node,
2564 qgroup_update->extent_op);
2565 kfree(qgroup_update);
2566 }
2567
2568 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2569
2570 return ret;
2571}
2572
Chris Masonbb721702013-01-29 18:44:12 -05002573static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2574 int count)
2575{
2576 int val = atomic_read(&delayed_refs->ref_seq);
2577
2578 if (val < seq || val >= seq + count)
2579 return 1;
2580 return 0;
2581}
2582
Josef Bacik1be41b72013-06-12 13:56:06 -04002583static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2584{
2585 u64 num_bytes;
2586
2587 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2588 sizeof(struct btrfs_extent_inline_ref));
2589 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2590 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2591
2592 /*
2593 * We don't ever fill up leaves all the way so multiply by 2 just to be
2594 * closer to what we're really going to want to ouse.
2595 */
2596 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2597}
2598
2599int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2600 struct btrfs_root *root)
2601{
2602 struct btrfs_block_rsv *global_rsv;
2603 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2604 u64 num_bytes;
2605 int ret = 0;
2606
2607 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2608 num_heads = heads_to_leaves(root, num_heads);
2609 if (num_heads > 1)
2610 num_bytes += (num_heads - 1) * root->leafsize;
2611 num_bytes <<= 1;
2612 global_rsv = &root->fs_info->global_block_rsv;
2613
2614 /*
2615 * If we can't allocate any more chunks lets make sure we have _lots_ of
2616 * wiggle room since running delayed refs can create more delayed refs.
2617 */
2618 if (global_rsv->space_info->full)
2619 num_bytes <<= 1;
2620
2621 spin_lock(&global_rsv->lock);
2622 if (global_rsv->reserved <= num_bytes)
2623 ret = 1;
2624 spin_unlock(&global_rsv->lock);
2625 return ret;
2626}
2627
Chris Masonc3e69d52009-03-13 10:17:05 -04002628/*
2629 * this starts processing the delayed reference count updates and
2630 * extent insertions we have queued up so far. count can be
2631 * 0, which means to process everything in the tree at the start
2632 * of the run (but not newly added entries), or it can be some target
2633 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002634 *
2635 * Returns 0 on success or if called with an aborted transaction
2636 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002637 */
2638int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2639 struct btrfs_root *root, unsigned long count)
2640{
2641 struct rb_node *node;
2642 struct btrfs_delayed_ref_root *delayed_refs;
2643 struct btrfs_delayed_ref_node *ref;
2644 struct list_head cluster;
2645 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002646 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002647 int run_all = count == (unsigned long)-1;
2648 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002649 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002650
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002651 /* We'll clean this up in btrfs_cleanup_transaction */
2652 if (trans->aborted)
2653 return 0;
2654
Chris Masonc3e69d52009-03-13 10:17:05 -04002655 if (root == root->fs_info->extent_root)
2656 root = root->fs_info->tree_root;
2657
Jan Schmidtedf39272012-06-28 18:04:55 +02002658 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2659
Chris Masonc3e69d52009-03-13 10:17:05 -04002660 delayed_refs = &trans->transaction->delayed_refs;
2661 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002662 if (count == 0) {
2663 count = delayed_refs->num_entries * 2;
2664 run_most = 1;
2665 }
2666
2667 if (!run_all && !run_most) {
2668 int old;
2669 int seq = atomic_read(&delayed_refs->ref_seq);
2670
2671progress:
2672 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2673 if (old) {
2674 DEFINE_WAIT(__wait);
Josef Bacik1be41b72013-06-12 13:56:06 -04002675 if (delayed_refs->flushing ||
2676 !btrfs_should_throttle_delayed_refs(trans, root))
Chris Masonbb721702013-01-29 18:44:12 -05002677 return 0;
2678
2679 prepare_to_wait(&delayed_refs->wait, &__wait,
2680 TASK_UNINTERRUPTIBLE);
2681
2682 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2683 if (old) {
2684 schedule();
2685 finish_wait(&delayed_refs->wait, &__wait);
2686
2687 if (!refs_newer(delayed_refs, seq, 256))
2688 goto progress;
2689 else
2690 return 0;
2691 } else {
2692 finish_wait(&delayed_refs->wait, &__wait);
2693 goto again;
2694 }
2695 }
2696
2697 } else {
2698 atomic_inc(&delayed_refs->procs_running_refs);
2699 }
2700
Chris Masonc3e69d52009-03-13 10:17:05 -04002701again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002702 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002703 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002704
Arne Jansen709c0482011-09-12 12:22:57 +02002705#ifdef SCRAMBLE_DELAYED_REFS
2706 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2707#endif
2708
Chris Masonc3e69d52009-03-13 10:17:05 -04002709 while (1) {
2710 if (!(run_all || run_most) &&
Josef Bacik1be41b72013-06-12 13:56:06 -04002711 !btrfs_should_throttle_delayed_refs(trans, root))
Chris Masonc3e69d52009-03-13 10:17:05 -04002712 break;
2713
2714 /*
2715 * go find something we can process in the rbtree. We start at
2716 * the beginning of the tree, and then build a cluster
2717 * of refs to process starting at the first one we are able to
2718 * lock
2719 */
Jan Schmidta1686502011-12-12 16:10:07 +01002720 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002721 ret = btrfs_find_ref_cluster(trans, &cluster,
2722 delayed_refs->run_delayed_start);
2723 if (ret)
2724 break;
2725
2726 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002727 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002728 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002729 spin_unlock(&delayed_refs->lock);
2730 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002731 atomic_dec(&delayed_refs->procs_running_refs);
Josef Bacikf971fe22013-06-10 11:52:32 -04002732 wake_up(&delayed_refs->wait);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002733 return ret;
2734 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002735
Chris Masonbb721702013-01-29 18:44:12 -05002736 atomic_add(ret, &delayed_refs->ref_seq);
2737
Chris Masonc3e69d52009-03-13 10:17:05 -04002738 count -= min_t(unsigned long, ret, count);
2739
2740 if (count == 0)
2741 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002742
Arne Jansen1fa11e22012-08-06 14:18:51 -06002743 if (delayed_start >= delayed_refs->run_delayed_start) {
2744 if (loops == 0) {
2745 /*
2746 * btrfs_find_ref_cluster looped. let's do one
2747 * more cycle. if we don't run any delayed ref
2748 * during that cycle (because we can't because
2749 * all of them are blocked), bail out.
2750 */
2751 loops = 1;
2752 } else {
2753 /*
2754 * no runnable refs left, stop trying
2755 */
2756 BUG_ON(run_all);
2757 break;
2758 }
2759 }
2760 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002761 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002762 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002763 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002764 }
2765
Chris Mason56bec292009-03-13 10:10:06 -04002766 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002767 if (!list_empty(&trans->new_bgs)) {
2768 spin_unlock(&delayed_refs->lock);
2769 btrfs_create_pending_block_groups(trans, root);
2770 spin_lock(&delayed_refs->lock);
2771 }
2772
Chris Mason56bec292009-03-13 10:10:06 -04002773 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002774 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002775 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002776 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002777
2778 while (node) {
2779 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2780 rb_node);
2781 if (btrfs_delayed_ref_is_head(ref)) {
2782 struct btrfs_delayed_ref_head *head;
2783
2784 head = btrfs_delayed_node_to_head(ref);
2785 atomic_inc(&ref->refs);
2786
2787 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002788 /*
2789 * Mutex was contended, block until it's
2790 * released and try again
2791 */
Chris Mason56bec292009-03-13 10:10:06 -04002792 mutex_lock(&head->mutex);
2793 mutex_unlock(&head->mutex);
2794
2795 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002796 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002797 goto again;
2798 }
2799 node = rb_next(node);
2800 }
2801 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002802 schedule_timeout(1);
2803 goto again;
2804 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002805out:
Chris Masonbb721702013-01-29 18:44:12 -05002806 atomic_dec(&delayed_refs->procs_running_refs);
2807 smp_mb();
2808 if (waitqueue_active(&delayed_refs->wait))
2809 wake_up(&delayed_refs->wait);
2810
Chris Masonc3e69d52009-03-13 10:17:05 -04002811 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002812 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002813 return 0;
2814}
2815
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002816int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2817 struct btrfs_root *root,
2818 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002819 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002820{
2821 struct btrfs_delayed_extent_op *extent_op;
2822 int ret;
2823
Miao Xie78a61842012-11-21 02:21:28 +00002824 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002825 if (!extent_op)
2826 return -ENOMEM;
2827
2828 extent_op->flags_to_set = flags;
2829 extent_op->update_flags = 1;
2830 extent_op->update_key = 0;
2831 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002832 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002833
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002834 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2835 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002836 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002837 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002838 return ret;
2839}
2840
2841static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2842 struct btrfs_root *root,
2843 struct btrfs_path *path,
2844 u64 objectid, u64 offset, u64 bytenr)
2845{
2846 struct btrfs_delayed_ref_head *head;
2847 struct btrfs_delayed_ref_node *ref;
2848 struct btrfs_delayed_data_ref *data_ref;
2849 struct btrfs_delayed_ref_root *delayed_refs;
2850 struct rb_node *node;
2851 int ret = 0;
2852
2853 ret = -ENOENT;
2854 delayed_refs = &trans->transaction->delayed_refs;
2855 spin_lock(&delayed_refs->lock);
2856 head = btrfs_find_delayed_ref_head(trans, bytenr);
2857 if (!head)
2858 goto out;
2859
2860 if (!mutex_trylock(&head->mutex)) {
2861 atomic_inc(&head->node.refs);
2862 spin_unlock(&delayed_refs->lock);
2863
David Sterbab3b4aa72011-04-21 01:20:15 +02002864 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002865
David Sterba8cc33e52011-05-02 15:29:25 +02002866 /*
2867 * Mutex was contended, block until it's released and let
2868 * caller try again
2869 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002870 mutex_lock(&head->mutex);
2871 mutex_unlock(&head->mutex);
2872 btrfs_put_delayed_ref(&head->node);
2873 return -EAGAIN;
2874 }
2875
2876 node = rb_prev(&head->node.rb_node);
2877 if (!node)
2878 goto out_unlock;
2879
2880 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2881
2882 if (ref->bytenr != bytenr)
2883 goto out_unlock;
2884
2885 ret = 1;
2886 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2887 goto out_unlock;
2888
2889 data_ref = btrfs_delayed_node_to_data_ref(ref);
2890
2891 node = rb_prev(node);
2892 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002893 int seq = ref->seq;
2894
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002895 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002896 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002897 goto out_unlock;
2898 }
2899
2900 if (data_ref->root != root->root_key.objectid ||
2901 data_ref->objectid != objectid || data_ref->offset != offset)
2902 goto out_unlock;
2903
2904 ret = 0;
2905out_unlock:
2906 mutex_unlock(&head->mutex);
2907out:
2908 spin_unlock(&delayed_refs->lock);
2909 return ret;
2910}
2911
2912static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2913 struct btrfs_root *root,
2914 struct btrfs_path *path,
2915 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002916{
2917 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002918 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002919 struct btrfs_extent_data_ref *ref;
2920 struct btrfs_extent_inline_ref *iref;
2921 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002922 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002923 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002924 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002925
Chris Masonbe20aa92007-12-17 20:14:01 -05002926 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002927 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002928 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002929
Chris Masonbe20aa92007-12-17 20:14:01 -05002930 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2931 if (ret < 0)
2932 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002933 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002934
2935 ret = -ENOENT;
2936 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002937 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002938
Zheng Yan31840ae2008-09-23 13:14:14 -04002939 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002940 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002941 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002942
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002943 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002944 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002945
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002946 ret = 1;
2947 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2948#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2949 if (item_size < sizeof(*ei)) {
2950 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2951 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002952 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002953#endif
2954 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2955
2956 if (item_size != sizeof(*ei) +
2957 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2958 goto out;
2959
2960 if (btrfs_extent_generation(leaf, ei) <=
2961 btrfs_root_last_snapshot(&root->root_item))
2962 goto out;
2963
2964 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2965 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2966 BTRFS_EXTENT_DATA_REF_KEY)
2967 goto out;
2968
2969 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2970 if (btrfs_extent_refs(leaf, ei) !=
2971 btrfs_extent_data_ref_count(leaf, ref) ||
2972 btrfs_extent_data_ref_root(leaf, ref) !=
2973 root->root_key.objectid ||
2974 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2975 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2976 goto out;
2977
Yan Zhengf321e492008-07-30 09:26:11 -04002978 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002979out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002980 return ret;
2981}
2982
2983int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2984 struct btrfs_root *root,
2985 u64 objectid, u64 offset, u64 bytenr)
2986{
2987 struct btrfs_path *path;
2988 int ret;
2989 int ret2;
2990
2991 path = btrfs_alloc_path();
2992 if (!path)
2993 return -ENOENT;
2994
2995 do {
2996 ret = check_committed_ref(trans, root, path, objectid,
2997 offset, bytenr);
2998 if (ret && ret != -ENOENT)
2999 goto out;
3000
3001 ret2 = check_delayed_ref(trans, root, path, objectid,
3002 offset, bytenr);
3003 } while (ret2 == -EAGAIN);
3004
3005 if (ret2 && ret2 != -ENOENT) {
3006 ret = ret2;
3007 goto out;
3008 }
3009
3010 if (ret != -ENOENT || ret2 != -ENOENT)
3011 ret = 0;
3012out:
Yan Zhengf321e492008-07-30 09:26:11 -04003013 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003014 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3015 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003016 return ret;
3017}
3018
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003019static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003020 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003021 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003022 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04003023{
3024 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003025 u64 num_bytes;
3026 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003027 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003028 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003029 struct btrfs_key key;
3030 struct btrfs_file_extent_item *fi;
3031 int i;
3032 int level;
3033 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003034 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003035 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04003036
3037 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003038 nritems = btrfs_header_nritems(buf);
3039 level = btrfs_header_level(buf);
3040
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003041 if (!root->ref_cows && level == 0)
3042 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003043
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003044 if (inc)
3045 process_func = btrfs_inc_extent_ref;
3046 else
3047 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003048
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003049 if (full_backref)
3050 parent = buf->start;
3051 else
3052 parent = 0;
3053
Zheng Yan31840ae2008-09-23 13:14:14 -04003054 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003055 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003056 btrfs_item_key_to_cpu(buf, &key, i);
3057 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003058 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003059 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003060 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003061 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003062 BTRFS_FILE_EXTENT_INLINE)
3063 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003064 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3065 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003066 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003067
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003068 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3069 key.offset -= btrfs_file_extent_offset(buf, fi);
3070 ret = process_func(trans, root, bytenr, num_bytes,
3071 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003072 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003073 if (ret)
3074 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003075 } else {
3076 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003077 num_bytes = btrfs_level_size(root, level - 1);
3078 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003079 parent, ref_root, level - 1, 0,
3080 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003081 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003082 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003083 }
3084 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003085 return 0;
3086fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003087 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003088}
3089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003090int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003091 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04003092{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003093 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003094}
Zheng Yan31840ae2008-09-23 13:14:14 -04003095
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003096int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003097 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003098{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003099 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04003100}
3101
Chris Mason9078a3e2007-04-26 16:46:15 -04003102static int write_one_cache_group(struct btrfs_trans_handle *trans,
3103 struct btrfs_root *root,
3104 struct btrfs_path *path,
3105 struct btrfs_block_group_cache *cache)
3106{
3107 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003108 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003109 unsigned long bi;
3110 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003111
Chris Mason9078a3e2007-04-26 16:46:15 -04003112 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003113 if (ret < 0)
3114 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003115 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003116
3117 leaf = path->nodes[0];
3118 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3119 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3120 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003121 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003122fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003123 if (ret) {
3124 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003125 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003126 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003127 return 0;
3128
3129}
3130
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003131static struct btrfs_block_group_cache *
3132next_block_group(struct btrfs_root *root,
3133 struct btrfs_block_group_cache *cache)
3134{
3135 struct rb_node *node;
3136 spin_lock(&root->fs_info->block_group_cache_lock);
3137 node = rb_next(&cache->cache_node);
3138 btrfs_put_block_group(cache);
3139 if (node) {
3140 cache = rb_entry(node, struct btrfs_block_group_cache,
3141 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003142 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003143 } else
3144 cache = NULL;
3145 spin_unlock(&root->fs_info->block_group_cache_lock);
3146 return cache;
3147}
3148
Josef Bacik0af3d002010-06-21 14:48:16 -04003149static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3150 struct btrfs_trans_handle *trans,
3151 struct btrfs_path *path)
3152{
3153 struct btrfs_root *root = block_group->fs_info->tree_root;
3154 struct inode *inode = NULL;
3155 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003156 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003157 int num_pages = 0;
3158 int retries = 0;
3159 int ret = 0;
3160
3161 /*
3162 * If this block group is smaller than 100 megs don't bother caching the
3163 * block group.
3164 */
3165 if (block_group->key.offset < (100 * 1024 * 1024)) {
3166 spin_lock(&block_group->lock);
3167 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3168 spin_unlock(&block_group->lock);
3169 return 0;
3170 }
3171
3172again:
3173 inode = lookup_free_space_inode(root, block_group, path);
3174 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3175 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003176 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003177 goto out;
3178 }
3179
3180 if (IS_ERR(inode)) {
3181 BUG_ON(retries);
3182 retries++;
3183
3184 if (block_group->ro)
3185 goto out_free;
3186
3187 ret = create_free_space_inode(root, trans, block_group, path);
3188 if (ret)
3189 goto out_free;
3190 goto again;
3191 }
3192
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003193 /* We've already setup this transaction, go ahead and exit */
3194 if (block_group->cache_generation == trans->transid &&
3195 i_size_read(inode)) {
3196 dcs = BTRFS_DC_SETUP;
3197 goto out_put;
3198 }
3199
Josef Bacik0af3d002010-06-21 14:48:16 -04003200 /*
3201 * We want to set the generation to 0, that way if anything goes wrong
3202 * from here on out we know not to trust this cache when we load up next
3203 * time.
3204 */
3205 BTRFS_I(inode)->generation = 0;
3206 ret = btrfs_update_inode(trans, root, inode);
3207 WARN_ON(ret);
3208
3209 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003210 ret = btrfs_check_trunc_cache_free_space(root,
3211 &root->fs_info->global_block_rsv);
3212 if (ret)
3213 goto out_put;
3214
Filipe David Borba Manana74514322013-09-20 14:46:51 +01003215 ret = btrfs_truncate_free_space_cache(root, trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003216 if (ret)
3217 goto out_put;
3218 }
3219
3220 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003221 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3222 !btrfs_test_opt(root, SPACE_CACHE)) {
3223 /*
3224 * don't bother trying to write stuff out _if_
3225 * a) we're not cached,
3226 * b) we're with nospace_cache mount option.
3227 */
Josef Bacik2b209822010-12-03 13:17:53 -05003228 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003229 spin_unlock(&block_group->lock);
3230 goto out_put;
3231 }
3232 spin_unlock(&block_group->lock);
3233
Josef Bacik6fc823b2012-08-06 13:46:38 -06003234 /*
3235 * Try to preallocate enough space based on how big the block group is.
3236 * Keep in mind this has to include any pinned space which could end up
3237 * taking up quite a bit since it's not folded into the other space
3238 * cache.
3239 */
3240 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003241 if (!num_pages)
3242 num_pages = 1;
3243
Josef Bacik0af3d002010-06-21 14:48:16 -04003244 num_pages *= 16;
3245 num_pages *= PAGE_CACHE_SIZE;
3246
3247 ret = btrfs_check_data_free_space(inode, num_pages);
3248 if (ret)
3249 goto out_put;
3250
3251 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3252 num_pages, num_pages,
3253 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003254 if (!ret)
3255 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003256 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003257
Josef Bacik0af3d002010-06-21 14:48:16 -04003258out_put:
3259 iput(inode);
3260out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003261 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003262out:
3263 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003264 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003265 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003266 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003267 spin_unlock(&block_group->lock);
3268
3269 return ret;
3270}
3271
Chris Mason96b51792007-10-15 16:15:19 -04003272int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3273 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003274{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003275 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003276 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003277 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003278 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003279
3280 path = btrfs_alloc_path();
3281 if (!path)
3282 return -ENOMEM;
3283
Josef Bacik0af3d002010-06-21 14:48:16 -04003284again:
3285 while (1) {
3286 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3287 while (cache) {
3288 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3289 break;
3290 cache = next_block_group(root, cache);
3291 }
3292 if (!cache) {
3293 if (last == 0)
3294 break;
3295 last = 0;
3296 continue;
3297 }
3298 err = cache_save_setup(cache, trans, path);
3299 last = cache->key.objectid + cache->key.offset;
3300 btrfs_put_block_group(cache);
3301 }
3302
Chris Masond3977122009-01-05 21:25:51 -05003303 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003304 if (last == 0) {
3305 err = btrfs_run_delayed_refs(trans, root,
3306 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003307 if (err) /* File system offline */
3308 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003309 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003310
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003311 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3312 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003313 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3314 btrfs_put_block_group(cache);
3315 goto again;
3316 }
3317
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003318 if (cache->dirty)
3319 break;
3320 cache = next_block_group(root, cache);
3321 }
3322 if (!cache) {
3323 if (last == 0)
3324 break;
3325 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003326 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003327 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003328
Josef Bacik0cb59c92010-07-02 12:14:14 -04003329 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3330 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003331 cache->dirty = 0;
3332 last = cache->key.objectid + cache->key.offset;
3333
3334 err = write_one_cache_group(trans, root, path, cache);
Filipe David Borba Mananae84cc142013-09-09 19:49:43 +01003335 btrfs_put_block_group(cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003336 if (err) /* File system offline */
3337 goto out;
Chris Mason9078a3e2007-04-26 16:46:15 -04003338 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003339
Josef Bacik0cb59c92010-07-02 12:14:14 -04003340 while (1) {
3341 /*
3342 * I don't think this is needed since we're just marking our
3343 * preallocated extent as written, but just in case it can't
3344 * hurt.
3345 */
3346 if (last == 0) {
3347 err = btrfs_run_delayed_refs(trans, root,
3348 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003349 if (err) /* File system offline */
3350 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003351 }
3352
3353 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3354 while (cache) {
3355 /*
3356 * Really this shouldn't happen, but it could if we
3357 * couldn't write the entire preallocated extent and
3358 * splitting the extent resulted in a new block.
3359 */
3360 if (cache->dirty) {
3361 btrfs_put_block_group(cache);
3362 goto again;
3363 }
3364 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3365 break;
3366 cache = next_block_group(root, cache);
3367 }
3368 if (!cache) {
3369 if (last == 0)
3370 break;
3371 last = 0;
3372 continue;
3373 }
3374
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003375 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003376
3377 /*
3378 * If we didn't have an error then the cache state is still
3379 * NEED_WRITE, so we can set it to WRITTEN.
3380 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003381 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003382 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3383 last = cache->key.objectid + cache->key.offset;
3384 btrfs_put_block_group(cache);
3385 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003386out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003387
Chris Mason9078a3e2007-04-26 16:46:15 -04003388 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003389 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003390}
3391
Yan Zhengd2fb3432008-12-11 16:30:39 -05003392int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3393{
3394 struct btrfs_block_group_cache *block_group;
3395 int readonly = 0;
3396
3397 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3398 if (!block_group || block_group->ro)
3399 readonly = 1;
3400 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003401 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003402 return readonly;
3403}
3404
Chris Mason593060d2008-03-25 16:50:33 -04003405static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3406 u64 total_bytes, u64 bytes_used,
3407 struct btrfs_space_info **space_info)
3408{
3409 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003410 int i;
3411 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003412 int ret;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003413
3414 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3415 BTRFS_BLOCK_GROUP_RAID10))
3416 factor = 2;
3417 else
3418 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003419
3420 found = __find_space_info(info, flags);
3421 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003422 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003423 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003424 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003425 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003426 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003427 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003428 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003429 *space_info = found;
3430 return 0;
3431 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003432 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003433 if (!found)
3434 return -ENOMEM;
3435
Josef Bacikb150a4f2013-06-19 15:00:04 -04003436 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3437 if (ret) {
3438 kfree(found);
3439 return ret;
3440 }
3441
Yan, Zhengb742bb82010-05-16 10:46:24 -04003442 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3443 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003444 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003445 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003446 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003447 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003448 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003449 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003450 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003451 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003452 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003453 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003454 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003455 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003456 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003457 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003458 found->flush = 0;
3459 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003460 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003461 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003462 if (flags & BTRFS_BLOCK_GROUP_DATA)
3463 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003464 return 0;
3465}
3466
Chris Mason8790d502008-04-03 16:29:03 -04003467static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3468{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003469 u64 extra_flags = chunk_to_extended(flags) &
3470 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003471
Miao Xiede98ced2013-01-29 10:13:12 +00003472 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003473 if (flags & BTRFS_BLOCK_GROUP_DATA)
3474 fs_info->avail_data_alloc_bits |= extra_flags;
3475 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3476 fs_info->avail_metadata_alloc_bits |= extra_flags;
3477 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3478 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003479 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003480}
Chris Mason593060d2008-03-25 16:50:33 -04003481
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003482/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003483 * returns target flags in extended format or 0 if restripe for this
3484 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003485 *
3486 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003487 */
3488static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3489{
3490 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3491 u64 target = 0;
3492
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003493 if (!bctl)
3494 return 0;
3495
3496 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3497 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3498 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3499 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3500 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3501 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3502 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3503 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3504 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3505 }
3506
3507 return target;
3508}
3509
3510/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003511 * @flags: available profiles in extended format (see ctree.h)
3512 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003513 * Returns reduced profile in chunk format. If profile changing is in
3514 * progress (either running or paused) picks the target profile (if it's
3515 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003516 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003517static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003518{
Chris Masoncd02dca2010-12-13 14:56:23 -05003519 /*
3520 * we add in the count of missing devices because we want
3521 * to make sure that any RAID levels on a degraded FS
3522 * continue to be honored.
3523 */
3524 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3525 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003526 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003527 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003528
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003529 /*
3530 * see if restripe for this chunk_type is in progress, if so
3531 * try to reduce to the target profile
3532 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003533 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003534 target = get_restripe_target(root->fs_info, flags);
3535 if (target) {
3536 /* pick target profile only if it's already available */
3537 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003538 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003539 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003540 }
3541 }
3542 spin_unlock(&root->fs_info->balance_lock);
3543
David Woodhouse53b381b2013-01-29 18:40:14 -05003544 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003545 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003546 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3547 BTRFS_BLOCK_GROUP_RAID5);
3548 if (num_devices < 3)
3549 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003550 if (num_devices < 4)
3551 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3552
David Woodhouse53b381b2013-01-29 18:40:14 -05003553 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3554 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3555 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3556 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003557
David Woodhouse53b381b2013-01-29 18:40:14 -05003558 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3559 tmp = BTRFS_BLOCK_GROUP_RAID6;
3560 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3561 tmp = BTRFS_BLOCK_GROUP_RAID5;
3562 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3563 tmp = BTRFS_BLOCK_GROUP_RAID10;
3564 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3565 tmp = BTRFS_BLOCK_GROUP_RAID1;
3566 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3567 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003568
David Woodhouse53b381b2013-01-29 18:40:14 -05003569 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003570}
3571
Yan, Zhengb742bb82010-05-16 10:46:24 -04003572static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003573{
Miao Xiede98ced2013-01-29 10:13:12 +00003574 unsigned seq;
3575
3576 do {
3577 seq = read_seqbegin(&root->fs_info->profiles_lock);
3578
3579 if (flags & BTRFS_BLOCK_GROUP_DATA)
3580 flags |= root->fs_info->avail_data_alloc_bits;
3581 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3582 flags |= root->fs_info->avail_system_alloc_bits;
3583 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3584 flags |= root->fs_info->avail_metadata_alloc_bits;
3585 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003586
Yan, Zhengb742bb82010-05-16 10:46:24 -04003587 return btrfs_reduce_alloc_profile(root, flags);
3588}
Josef Bacik6a632092009-02-20 11:00:09 -05003589
Miao Xie6d07bce2011-01-05 10:07:31 +00003590u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003591{
3592 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003593 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003594
Yan, Zhengb742bb82010-05-16 10:46:24 -04003595 if (data)
3596 flags = BTRFS_BLOCK_GROUP_DATA;
3597 else if (root == root->fs_info->chunk_root)
3598 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3599 else
3600 flags = BTRFS_BLOCK_GROUP_METADATA;
3601
David Woodhouse53b381b2013-01-29 18:40:14 -05003602 ret = get_alloc_profile(root, flags);
3603 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003604}
3605
Josef Bacik6a632092009-02-20 11:00:09 -05003606/*
3607 * This will check the space that the inode allocates from to make sure we have
3608 * enough space for bytes.
3609 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003610int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003611{
3612 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003613 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003614 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003615 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003616 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003617
3618 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003619 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003620
Miao Xie9dced182013-10-25 17:33:36 +08003621 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003622 committed = 1;
Miao Xie9dced182013-10-25 17:33:36 +08003623 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04003624 }
3625
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003626 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003627 if (!data_sinfo)
3628 goto alloc;
3629
Josef Bacik6a632092009-02-20 11:00:09 -05003630again:
3631 /* make sure we have enough space to handle the data first */
3632 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003633 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3634 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3635 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003636
3637 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003638 struct btrfs_trans_handle *trans;
3639
Josef Bacik6a632092009-02-20 11:00:09 -05003640 /*
3641 * if we don't have enough free bytes in this space then we need
3642 * to alloc a new chunk.
3643 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003644 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003645 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003646
Chris Mason0e4f8f82011-04-15 16:05:44 -04003647 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003648 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003649alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003650 alloc_target = btrfs_get_alloc_profile(root, 1);
Miao Xie9dced182013-10-25 17:33:36 +08003651 /*
3652 * It is ugly that we don't call nolock join
3653 * transaction for the free space inode case here.
3654 * But it is safe because we only do the data space
3655 * reservation for the free space cache in the
3656 * transaction context, the common join transaction
3657 * just increase the counter of the current transaction
3658 * handler, doesn't try to acquire the trans_lock of
3659 * the fs.
3660 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003661 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003662 if (IS_ERR(trans))
3663 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003664
3665 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003666 alloc_target,
3667 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003668 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003669 if (ret < 0) {
3670 if (ret != -ENOSPC)
3671 return ret;
3672 else
3673 goto commit_trans;
3674 }
Chris Mason33b4d472009-09-22 14:45:50 -04003675
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003676 if (!data_sinfo)
3677 data_sinfo = fs_info->data_sinfo;
3678
Josef Bacik6a632092009-02-20 11:00:09 -05003679 goto again;
3680 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003681
3682 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04003683 * If we don't have enough pinned space to deal with this
3684 * allocation don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003685 */
Josef Bacikb150a4f2013-06-19 15:00:04 -04003686 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3687 bytes) < 0)
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003688 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003689 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003690
3691 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003692commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003693 if (!committed &&
3694 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003695 committed = 1;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003696
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003697 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003698 if (IS_ERR(trans))
3699 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003700 ret = btrfs_commit_transaction(trans, root);
3701 if (ret)
3702 return ret;
3703 goto again;
3704 }
3705
Jeff Mahoneycab45e22013-10-16 16:27:01 -04003706 trace_btrfs_space_reservation(root->fs_info,
3707 "space_info:enospc",
3708 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003709 return -ENOSPC;
3710 }
3711 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003712 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003713 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003714 spin_unlock(&data_sinfo->lock);
3715
Josef Bacik9ed74f22009-09-11 16:12:44 -04003716 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003717}
3718
3719/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003720 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003721 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003722void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003723{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003724 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003725 struct btrfs_space_info *data_sinfo;
3726
3727 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003728 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003729
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003730 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003731 spin_lock(&data_sinfo->lock);
Josef Bacik7ee9e442013-06-21 16:37:03 -04003732 WARN_ON(data_sinfo->bytes_may_use < bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003733 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003734 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003735 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003736 spin_unlock(&data_sinfo->lock);
3737}
3738
Josef Bacik97e728d2009-04-21 17:40:57 -04003739static void force_metadata_allocation(struct btrfs_fs_info *info)
3740{
3741 struct list_head *head = &info->space_info;
3742 struct btrfs_space_info *found;
3743
3744 rcu_read_lock();
3745 list_for_each_entry_rcu(found, head, list) {
3746 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003747 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003748 }
3749 rcu_read_unlock();
3750}
3751
Miao Xie3c76cd82013-04-25 10:12:38 +00003752static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3753{
3754 return (global->size << 1);
3755}
3756
Chris Masone5bc2452010-10-26 13:37:56 -04003757static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003758 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003759{
Josef Bacikfb25e912011-07-26 17:00:46 -04003760 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003761 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003762 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003763 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003764
Chris Mason0e4f8f82011-04-15 16:05:44 -04003765 if (force == CHUNK_ALLOC_FORCE)
3766 return 1;
3767
3768 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003769 * We need to take into account the global rsv because for all intents
3770 * and purposes it's used space. Don't worry about locking the
3771 * global_rsv, it doesn't change except when the transaction commits.
3772 */
Josef Bacik54338b52012-08-14 16:20:52 -04003773 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003774 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003775
3776 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003777 * in limited mode, we want to have some free space up to
3778 * about 1% of the FS size.
3779 */
3780 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003781 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003782 thresh = max_t(u64, 64 * 1024 * 1024,
3783 div_factor_fine(thresh, 1));
3784
3785 if (num_bytes - num_allocated < thresh)
3786 return 1;
3787 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003788
Josef Bacik698d0082012-09-12 14:08:47 -04003789 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003790 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003791 return 1;
3792}
3793
Liu Bo15d1ff82012-03-29 09:57:44 -04003794static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3795{
3796 u64 num_dev;
3797
David Woodhouse53b381b2013-01-29 18:40:14 -05003798 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3799 BTRFS_BLOCK_GROUP_RAID0 |
3800 BTRFS_BLOCK_GROUP_RAID5 |
3801 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003802 num_dev = root->fs_info->fs_devices->rw_devices;
3803 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3804 num_dev = 2;
3805 else
3806 num_dev = 1; /* DUP or single */
3807
3808 /* metadata for updaing devices and chunk tree */
3809 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3810}
3811
3812static void check_system_chunk(struct btrfs_trans_handle *trans,
3813 struct btrfs_root *root, u64 type)
3814{
3815 struct btrfs_space_info *info;
3816 u64 left;
3817 u64 thresh;
3818
3819 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3820 spin_lock(&info->lock);
3821 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3822 info->bytes_reserved - info->bytes_readonly;
3823 spin_unlock(&info->lock);
3824
3825 thresh = get_system_chunk_thresh(root, type);
3826 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003827 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3828 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003829 dump_space_info(info, 0, 0);
3830 }
3831
3832 if (left < thresh) {
3833 u64 flags;
3834
3835 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3836 btrfs_alloc_chunk(trans, root, flags);
3837 }
3838}
3839
Chris Mason6324fbf2008-03-24 15:01:59 -04003840static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003841 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003842{
3843 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003844 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003845 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003846 int ret = 0;
3847
Josef Bacikc6b305a2012-12-18 09:16:16 -05003848 /* Don't re-enter if we're already allocating a chunk */
3849 if (trans->allocating_chunk)
3850 return -ENOSPC;
3851
Chris Mason6324fbf2008-03-24 15:01:59 -04003852 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003853 if (!space_info) {
3854 ret = update_space_info(extent_root->fs_info, flags,
3855 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003856 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003857 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003858 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003859
Josef Bacik6d741192011-04-11 20:20:11 -04003860again:
Josef Bacik25179202008-10-29 14:49:05 -04003861 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003862 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003863 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003864 if (space_info->full) {
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01003865 if (should_alloc_chunk(extent_root, space_info, force))
3866 ret = -ENOSPC;
3867 else
3868 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003869 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01003870 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04003871 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003872
Josef Bacik698d0082012-09-12 14:08:47 -04003873 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003874 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003875 return 0;
3876 } else if (space_info->chunk_alloc) {
3877 wait_for_alloc = 1;
3878 } else {
3879 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003880 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003881
Josef Bacik25179202008-10-29 14:49:05 -04003882 spin_unlock(&space_info->lock);
3883
Josef Bacik6d741192011-04-11 20:20:11 -04003884 mutex_lock(&fs_info->chunk_mutex);
3885
3886 /*
3887 * The chunk_mutex is held throughout the entirety of a chunk
3888 * allocation, so once we've acquired the chunk_mutex we know that the
3889 * other guy is done and we need to recheck and see if we should
3890 * allocate.
3891 */
3892 if (wait_for_alloc) {
3893 mutex_unlock(&fs_info->chunk_mutex);
3894 wait_for_alloc = 0;
3895 goto again;
3896 }
3897
Josef Bacikc6b305a2012-12-18 09:16:16 -05003898 trans->allocating_chunk = true;
3899
Josef Bacik97e728d2009-04-21 17:40:57 -04003900 /*
Josef Bacik67377732010-09-16 16:19:09 -04003901 * If we have mixed data/metadata chunks we want to make sure we keep
3902 * allocating mixed chunks instead of individual chunks.
3903 */
3904 if (btrfs_mixed_space_info(space_info))
3905 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3906
3907 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003908 * if we're doing a data chunk, go ahead and make sure that
3909 * we keep a reasonable number of metadata chunks allocated in the
3910 * FS as well.
3911 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003912 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003913 fs_info->data_chunk_allocations++;
3914 if (!(fs_info->data_chunk_allocations %
3915 fs_info->metadata_ratio))
3916 force_metadata_allocation(fs_info);
3917 }
3918
Liu Bo15d1ff82012-03-29 09:57:44 -04003919 /*
3920 * Check if we have enough space in SYSTEM chunk because we may need
3921 * to update devices.
3922 */
3923 check_system_chunk(trans, extent_root, flags);
3924
Yan Zheng2b820322008-11-17 21:11:30 -05003925 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003926 trans->allocating_chunk = false;
Mark Fasheh92b8e892011-07-12 10:57:59 -07003927
Josef Bacik9ed74f22009-09-11 16:12:44 -04003928 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003929 if (ret < 0 && ret != -ENOSPC)
3930 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003931 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003932 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003933 else
3934 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003935
Chris Mason0e4f8f82011-04-15 16:05:44 -04003936 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003937out:
Josef Bacik6d741192011-04-11 20:20:11 -04003938 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003939 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003940 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003941 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003942}
3943
Josef Bacika80c8dc2012-09-06 16:59:33 -04003944static int can_overcommit(struct btrfs_root *root,
3945 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003946 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003947{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003948 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003949 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00003950 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003951 u64 avail;
3952 u64 used;
3953
3954 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003955 space_info->bytes_pinned + space_info->bytes_readonly;
3956
Josef Bacik96f1bb52013-01-30 17:02:51 -05003957 /*
3958 * We only want to allow over committing if we have lots of actual space
3959 * free, but if we don't have enough space to handle the global reserve
3960 * space then we could end up having a real enospc problem when trying
3961 * to allocate a chunk or some other such important allocation.
3962 */
Miao Xie3c76cd82013-04-25 10:12:38 +00003963 spin_lock(&global_rsv->lock);
3964 space_size = calc_global_rsv_need_space(global_rsv);
3965 spin_unlock(&global_rsv->lock);
3966 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05003967 return 0;
3968
3969 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003970
3971 spin_lock(&root->fs_info->free_chunk_lock);
3972 avail = root->fs_info->free_chunk_space;
3973 spin_unlock(&root->fs_info->free_chunk_lock);
3974
3975 /*
3976 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003977 * space is actually useable. For raid56, the space info used
3978 * doesn't include the parity drive, so we don't have to
3979 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003980 */
3981 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3982 BTRFS_BLOCK_GROUP_RAID1 |
3983 BTRFS_BLOCK_GROUP_RAID10))
3984 avail >>= 1;
3985
3986 /*
Miao Xie561c2942012-10-16 11:32:18 +00003987 * If we aren't flushing all things, let us overcommit up to
3988 * 1/2th of the space. If we can flush, don't let us overcommit
3989 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003990 */
Miao Xie08e007d2012-10-16 11:33:38 +00003991 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04003992 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003993 else
Josef Bacik14575ae2013-09-17 10:48:00 -04003994 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003995
Josef Bacik14575ae2013-09-17 10:48:00 -04003996 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003997 return 1;
3998 return 0;
3999}
4000
Eric Sandeen48a3b632013-04-25 20:41:01 +00004001static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
4002 unsigned long nr_pages)
Miao Xieda633a42012-12-20 11:19:09 +00004003{
4004 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004005
Josef Bacik925a6ef2013-06-20 12:31:27 -04004006 if (down_read_trylock(&sb->s_umount)) {
4007 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4008 up_read(&sb->s_umount);
4009 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004010 /*
4011 * We needn't worry the filesystem going from r/w to r/o though
4012 * we don't acquire ->s_umount mutex, because the filesystem
4013 * should guarantee the delalloc inodes list be empty after
4014 * the filesystem is readonly(all dirty pages are written to
4015 * the disk).
4016 */
Miao Xie91aef862013-11-04 23:13:26 +08004017 btrfs_start_delalloc_roots(root->fs_info, 0);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004018 if (!current->journal_info)
Miao Xieb0244192013-11-04 23:13:25 +08004019 btrfs_wait_ordered_roots(root->fs_info, -1);
Miao Xieda633a42012-12-20 11:19:09 +00004020 }
4021}
4022
Miao Xie18cd8ea2013-11-04 23:13:22 +08004023static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4024{
4025 u64 bytes;
4026 int nr;
4027
4028 bytes = btrfs_calc_trans_metadata_size(root, 1);
4029 nr = (int)div64_u64(to_reclaim, bytes);
4030 if (!nr)
4031 nr = 1;
4032 return nr;
4033}
4034
Miao Xiec61a16a2013-11-04 23:13:23 +08004035#define EXTENT_SIZE_PER_ITEM (256 * 1024)
4036
Yan, Zheng5da9d012010-05-16 10:46:25 -04004037/*
4038 * shrink metadata reservation for delalloc
4039 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04004040static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4041 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004042{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004043 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004044 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004045 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004046 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004047 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004048 long time_left;
Miao Xied3ee29e2013-11-04 23:13:20 +08004049 unsigned long nr_pages;
4050 int loops;
Miao Xieb0244192013-11-04 23:13:25 +08004051 int items;
Miao Xie08e007d2012-10-16 11:33:38 +00004052 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004053
Miao Xiec61a16a2013-11-04 23:13:23 +08004054 /* Calc the number of the pages we need flush for space reservation */
Miao Xieb0244192013-11-04 23:13:25 +08004055 items = calc_reclaim_items_nr(root, to_reclaim);
4056 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004057
Josef Bacik663350a2011-11-03 22:54:25 -04004058 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004059 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004060 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004061
Miao Xie963d6782013-01-29 10:10:51 +00004062 delalloc_bytes = percpu_counter_sum_positive(
4063 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004064 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004065 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004066 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004067 if (wait_ordered)
Miao Xieb0244192013-11-04 23:13:25 +08004068 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004069 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004070 }
4071
Miao Xied3ee29e2013-11-04 23:13:20 +08004072 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004073 while (delalloc_bytes && loops < 3) {
4074 max_reclaim = min(delalloc_bytes, to_reclaim);
4075 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00004076 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04004077 /*
4078 * We need to wait for the async pages to actually start before
4079 * we do anything.
4080 */
Miao Xie9f3a0742013-11-04 23:13:24 +08004081 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
4082 if (!max_reclaim)
4083 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004084
Miao Xie9f3a0742013-11-04 23:13:24 +08004085 if (max_reclaim <= nr_pages)
4086 max_reclaim = 0;
4087 else
4088 max_reclaim -= nr_pages;
4089
4090 wait_event(root->fs_info->async_submit_wait,
4091 atomic_read(&root->fs_info->async_delalloc_pages) <=
4092 (int)max_reclaim);
4093skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004094 if (!trans)
4095 flush = BTRFS_RESERVE_FLUSH_ALL;
4096 else
4097 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004098 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004099 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004100 spin_unlock(&space_info->lock);
4101 break;
4102 }
Josef Bacik0019f102010-10-15 15:18:40 -04004103 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004104
Chris Mason36e39c42011-03-12 07:08:42 -05004105 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004106 if (wait_ordered && !trans) {
Miao Xieb0244192013-11-04 23:13:25 +08004107 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf104d042011-10-14 13:56:58 -04004108 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004109 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004110 if (time_left)
4111 break;
4112 }
Miao Xie963d6782013-01-29 10:10:51 +00004113 delalloc_bytes = percpu_counter_sum_positive(
4114 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004115 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004116}
4117
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004118/**
Josef Bacik663350a2011-11-03 22:54:25 -04004119 * maybe_commit_transaction - possibly commit the transaction if its ok to
4120 * @root - the root we're allocating for
4121 * @bytes - the number of bytes we want to reserve
4122 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004123 *
Josef Bacik663350a2011-11-03 22:54:25 -04004124 * This will check to make sure that committing the transaction will actually
4125 * get us somewhere and then commit the transaction if it does. Otherwise it
4126 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004127 */
Josef Bacik663350a2011-11-03 22:54:25 -04004128static int may_commit_transaction(struct btrfs_root *root,
4129 struct btrfs_space_info *space_info,
4130 u64 bytes, int force)
4131{
4132 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4133 struct btrfs_trans_handle *trans;
4134
4135 trans = (struct btrfs_trans_handle *)current->journal_info;
4136 if (trans)
4137 return -EAGAIN;
4138
4139 if (force)
4140 goto commit;
4141
4142 /* See if there is enough pinned space to make this reservation */
4143 spin_lock(&space_info->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004144 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4145 bytes) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004146 spin_unlock(&space_info->lock);
4147 goto commit;
4148 }
4149 spin_unlock(&space_info->lock);
4150
4151 /*
4152 * See if there is some space in the delayed insertion reservation for
4153 * this reservation.
4154 */
4155 if (space_info != delayed_rsv->space_info)
4156 return -ENOSPC;
4157
Liu Bod9b02182012-02-16 18:34:39 +08004158 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004159 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004160 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4161 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004162 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004163 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004164 return -ENOSPC;
4165 }
4166 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004167 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004168
4169commit:
4170 trans = btrfs_join_transaction(root);
4171 if (IS_ERR(trans))
4172 return -ENOSPC;
4173
4174 return btrfs_commit_transaction(trans, root);
4175}
4176
Josef Bacik96c3f432012-06-21 14:05:49 -04004177enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004178 FLUSH_DELAYED_ITEMS_NR = 1,
4179 FLUSH_DELAYED_ITEMS = 2,
4180 FLUSH_DELALLOC = 3,
4181 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004182 ALLOC_CHUNK = 5,
4183 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004184};
4185
4186static int flush_space(struct btrfs_root *root,
4187 struct btrfs_space_info *space_info, u64 num_bytes,
4188 u64 orig_bytes, int state)
4189{
4190 struct btrfs_trans_handle *trans;
4191 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004192 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004193
4194 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004195 case FLUSH_DELAYED_ITEMS_NR:
4196 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004197 if (state == FLUSH_DELAYED_ITEMS_NR)
4198 nr = calc_reclaim_items_nr(root, num_bytes) * 2;
4199 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004200 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004201
Josef Bacik96c3f432012-06-21 14:05:49 -04004202 trans = btrfs_join_transaction(root);
4203 if (IS_ERR(trans)) {
4204 ret = PTR_ERR(trans);
4205 break;
4206 }
4207 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4208 btrfs_end_transaction(trans, root);
4209 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004210 case FLUSH_DELALLOC:
4211 case FLUSH_DELALLOC_WAIT:
4212 shrink_delalloc(root, num_bytes, orig_bytes,
4213 state == FLUSH_DELALLOC_WAIT);
4214 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004215 case ALLOC_CHUNK:
4216 trans = btrfs_join_transaction(root);
4217 if (IS_ERR(trans)) {
4218 ret = PTR_ERR(trans);
4219 break;
4220 }
4221 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004222 btrfs_get_alloc_profile(root, 0),
4223 CHUNK_ALLOC_NO_FORCE);
4224 btrfs_end_transaction(trans, root);
4225 if (ret == -ENOSPC)
4226 ret = 0;
4227 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004228 case COMMIT_TRANS:
4229 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4230 break;
4231 default:
4232 ret = -ENOSPC;
4233 break;
4234 }
4235
4236 return ret;
4237}
Josef Bacik663350a2011-11-03 22:54:25 -04004238/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004239 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4240 * @root - the root we're allocating for
4241 * @block_rsv - the block_rsv we're allocating for
4242 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004243 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004244 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004245 * This will reserve orgi_bytes number of bytes from the space info associated
4246 * with the block_rsv. If there is not enough space it will make an attempt to
4247 * flush out space to make room. It will do this by flushing delalloc if
4248 * possible or committing the transaction. If flush is 0 then no attempts to
4249 * regain reservations will be made and this will fail if there is not enough
4250 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004251 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004252static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004253 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004254 u64 orig_bytes,
4255 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004256{
4257 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004258 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004259 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004260 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004261 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004262 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004263
4264again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004265 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004266 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004267 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004268 * We only want to wait if somebody other than us is flushing and we
4269 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004270 */
Miao Xie08e007d2012-10-16 11:33:38 +00004271 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4272 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004273 spin_unlock(&space_info->lock);
4274 /*
4275 * If we have a trans handle we can't wait because the flusher
4276 * may have to commit the transaction, which would mean we would
4277 * deadlock since we are waiting for the flusher to finish, but
4278 * hold the current transaction open.
4279 */
Josef Bacik663350a2011-11-03 22:54:25 -04004280 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004281 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004282 ret = wait_event_killable(space_info->wait, !space_info->flush);
4283 /* Must have been killed, return */
4284 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004285 return -EINTR;
4286
4287 spin_lock(&space_info->lock);
4288 }
4289
4290 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004291 used = space_info->bytes_used + space_info->bytes_reserved +
4292 space_info->bytes_pinned + space_info->bytes_readonly +
4293 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004294
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004295 /*
4296 * The idea here is that we've not already over-reserved the block group
4297 * then we can go ahead and save our reservation first and then start
4298 * flushing if we need to. Otherwise if we've already overcommitted
4299 * lets start flushing stuff first and then come back and try to make
4300 * our reservation.
4301 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004302 if (used <= space_info->total_bytes) {
4303 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004304 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004305 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004306 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004307 ret = 0;
4308 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004309 /*
4310 * Ok set num_bytes to orig_bytes since we aren't
4311 * overocmmitted, this way we only try and reclaim what
4312 * we need.
4313 */
4314 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004315 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004316 } else {
4317 /*
4318 * Ok we're over committed, set num_bytes to the overcommitted
4319 * amount plus the amount of bytes that we need for this
4320 * reservation.
4321 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004322 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004323 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004324 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004325
Josef Bacik44734ed2012-09-28 16:04:19 -04004326 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4327 space_info->bytes_may_use += orig_bytes;
4328 trace_btrfs_space_reservation(root->fs_info, "space_info",
4329 space_info->flags, orig_bytes,
4330 1);
4331 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004332 }
4333
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004334 /*
4335 * Couldn't make our reservation, save our place so while we're trying
4336 * to reclaim space we can actually use it instead of somebody else
4337 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004338 *
4339 * We make the other tasks wait for the flush only when we can flush
4340 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004341 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004342 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004343 flushing = true;
4344 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004345 }
4346
Yan, Zhengf0486c62010-05-16 10:46:25 -04004347 spin_unlock(&space_info->lock);
4348
Miao Xie08e007d2012-10-16 11:33:38 +00004349 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004350 goto out;
4351
Josef Bacik96c3f432012-06-21 14:05:49 -04004352 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4353 flush_state);
4354 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004355
4356 /*
4357 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4358 * would happen. So skip delalloc flush.
4359 */
4360 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4361 (flush_state == FLUSH_DELALLOC ||
4362 flush_state == FLUSH_DELALLOC_WAIT))
4363 flush_state = ALLOC_CHUNK;
4364
Josef Bacik96c3f432012-06-21 14:05:49 -04004365 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004366 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004367 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4368 flush_state < COMMIT_TRANS)
4369 goto again;
4370 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4371 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004372 goto again;
4373
4374out:
Josef Bacik5d803662013-02-07 16:06:02 -05004375 if (ret == -ENOSPC &&
4376 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4377 struct btrfs_block_rsv *global_rsv =
4378 &root->fs_info->global_block_rsv;
4379
4380 if (block_rsv != global_rsv &&
4381 !block_rsv_use_bytes(global_rsv, orig_bytes))
4382 ret = 0;
4383 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004384 if (ret == -ENOSPC)
4385 trace_btrfs_space_reservation(root->fs_info,
4386 "space_info:enospc",
4387 space_info->flags, orig_bytes, 1);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004388 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004389 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004390 space_info->flush = 0;
4391 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004392 spin_unlock(&space_info->lock);
4393 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004394 return ret;
4395}
4396
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004397static struct btrfs_block_rsv *get_block_rsv(
4398 const struct btrfs_trans_handle *trans,
4399 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004400{
Josef Bacik4c13d752011-08-30 11:31:29 -04004401 struct btrfs_block_rsv *block_rsv = NULL;
4402
Josef Bacik0e721102012-06-26 16:13:18 -04004403 if (root->ref_cows)
4404 block_rsv = trans->block_rsv;
4405
4406 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004407 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004408
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004409 if (root == root->fs_info->uuid_root)
4410 block_rsv = trans->block_rsv;
4411
Josef Bacik4c13d752011-08-30 11:31:29 -04004412 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004413 block_rsv = root->block_rsv;
4414
4415 if (!block_rsv)
4416 block_rsv = &root->fs_info->empty_block_rsv;
4417
4418 return block_rsv;
4419}
4420
4421static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4422 u64 num_bytes)
4423{
4424 int ret = -ENOSPC;
4425 spin_lock(&block_rsv->lock);
4426 if (block_rsv->reserved >= num_bytes) {
4427 block_rsv->reserved -= num_bytes;
4428 if (block_rsv->reserved < block_rsv->size)
4429 block_rsv->full = 0;
4430 ret = 0;
4431 }
4432 spin_unlock(&block_rsv->lock);
4433 return ret;
4434}
4435
4436static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4437 u64 num_bytes, int update_size)
4438{
4439 spin_lock(&block_rsv->lock);
4440 block_rsv->reserved += num_bytes;
4441 if (update_size)
4442 block_rsv->size += num_bytes;
4443 else if (block_rsv->reserved >= block_rsv->size)
4444 block_rsv->full = 1;
4445 spin_unlock(&block_rsv->lock);
4446}
4447
Josef Bacikd52be812013-05-29 14:54:47 -04004448int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4449 struct btrfs_block_rsv *dest, u64 num_bytes,
4450 int min_factor)
4451{
4452 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4453 u64 min_bytes;
4454
4455 if (global_rsv->space_info != dest->space_info)
4456 return -ENOSPC;
4457
4458 spin_lock(&global_rsv->lock);
4459 min_bytes = div_factor(global_rsv->size, min_factor);
4460 if (global_rsv->reserved < min_bytes + num_bytes) {
4461 spin_unlock(&global_rsv->lock);
4462 return -ENOSPC;
4463 }
4464 global_rsv->reserved -= num_bytes;
4465 if (global_rsv->reserved < global_rsv->size)
4466 global_rsv->full = 0;
4467 spin_unlock(&global_rsv->lock);
4468
4469 block_rsv_add_bytes(dest, num_bytes, 1);
4470 return 0;
4471}
4472
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004473static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4474 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004475 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004476{
4477 struct btrfs_space_info *space_info = block_rsv->space_info;
4478
4479 spin_lock(&block_rsv->lock);
4480 if (num_bytes == (u64)-1)
4481 num_bytes = block_rsv->size;
4482 block_rsv->size -= num_bytes;
4483 if (block_rsv->reserved >= block_rsv->size) {
4484 num_bytes = block_rsv->reserved - block_rsv->size;
4485 block_rsv->reserved = block_rsv->size;
4486 block_rsv->full = 1;
4487 } else {
4488 num_bytes = 0;
4489 }
4490 spin_unlock(&block_rsv->lock);
4491
4492 if (num_bytes > 0) {
4493 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004494 spin_lock(&dest->lock);
4495 if (!dest->full) {
4496 u64 bytes_to_add;
4497
4498 bytes_to_add = dest->size - dest->reserved;
4499 bytes_to_add = min(num_bytes, bytes_to_add);
4500 dest->reserved += bytes_to_add;
4501 if (dest->reserved >= dest->size)
4502 dest->full = 1;
4503 num_bytes -= bytes_to_add;
4504 }
4505 spin_unlock(&dest->lock);
4506 }
4507 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004508 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004509 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004510 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004511 space_info->flags, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004512 spin_unlock(&space_info->lock);
4513 }
4514 }
4515}
4516
4517static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4518 struct btrfs_block_rsv *dst, u64 num_bytes)
4519{
4520 int ret;
4521
4522 ret = block_rsv_use_bytes(src, num_bytes);
4523 if (ret)
4524 return ret;
4525
4526 block_rsv_add_bytes(dst, num_bytes, 1);
4527 return 0;
4528}
4529
Miao Xie66d8f3d2012-09-06 04:02:28 -06004530void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004531{
4532 memset(rsv, 0, sizeof(*rsv));
4533 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004534 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004535}
4536
Miao Xie66d8f3d2012-09-06 04:02:28 -06004537struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4538 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004539{
4540 struct btrfs_block_rsv *block_rsv;
4541 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004542
4543 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4544 if (!block_rsv)
4545 return NULL;
4546
Miao Xie66d8f3d2012-09-06 04:02:28 -06004547 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004548 block_rsv->space_info = __find_space_info(fs_info,
4549 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004550 return block_rsv;
4551}
4552
4553void btrfs_free_block_rsv(struct btrfs_root *root,
4554 struct btrfs_block_rsv *rsv)
4555{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004556 if (!rsv)
4557 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004558 btrfs_block_rsv_release(root, rsv, (u64)-1);
4559 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004560}
4561
Miao Xie08e007d2012-10-16 11:33:38 +00004562int btrfs_block_rsv_add(struct btrfs_root *root,
4563 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4564 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004565{
4566 int ret;
4567
4568 if (num_bytes == 0)
4569 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004570
Miao Xie61b520a2011-11-10 20:45:05 -05004571 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004572 if (!ret) {
4573 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4574 return 0;
4575 }
4576
Yan, Zhengf0486c62010-05-16 10:46:25 -04004577 return ret;
4578}
4579
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004580int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004581 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004582{
4583 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004584 int ret = -ENOSPC;
4585
4586 if (!block_rsv)
4587 return 0;
4588
4589 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004590 num_bytes = div_factor(block_rsv->size, min_factor);
4591 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004592 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004593 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004594
Josef Bacik36ba0222011-10-18 12:15:48 -04004595 return ret;
4596}
4597
Miao Xie08e007d2012-10-16 11:33:38 +00004598int btrfs_block_rsv_refill(struct btrfs_root *root,
4599 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4600 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004601{
4602 u64 num_bytes = 0;
4603 int ret = -ENOSPC;
4604
4605 if (!block_rsv)
4606 return 0;
4607
4608 spin_lock(&block_rsv->lock);
4609 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004610 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004611 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004612 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004613 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004614 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004615
Yan, Zhengf0486c62010-05-16 10:46:25 -04004616 if (!ret)
4617 return 0;
4618
Miao Xieaa38a712011-11-18 17:43:00 +08004619 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004620 if (!ret) {
4621 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004622 return 0;
4623 }
4624
Josef Bacik13553e52011-08-08 13:33:21 -04004625 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004626}
4627
4628int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4629 struct btrfs_block_rsv *dst_rsv,
4630 u64 num_bytes)
4631{
4632 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4633}
4634
4635void btrfs_block_rsv_release(struct btrfs_root *root,
4636 struct btrfs_block_rsv *block_rsv,
4637 u64 num_bytes)
4638{
4639 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4640 if (global_rsv->full || global_rsv == block_rsv ||
4641 block_rsv->space_info != global_rsv->space_info)
4642 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004643 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4644 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004645}
4646
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004647/*
4648 * helper to calculate size of global block reservation.
4649 * the desired value is sum of space used by extent tree,
4650 * checksum tree and root tree
4651 */
4652static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4653{
4654 struct btrfs_space_info *sinfo;
4655 u64 num_bytes;
4656 u64 meta_used;
4657 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004658 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004659
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004660 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4661 spin_lock(&sinfo->lock);
4662 data_used = sinfo->bytes_used;
4663 spin_unlock(&sinfo->lock);
4664
4665 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4666 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004667 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4668 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004669 meta_used = sinfo->bytes_used;
4670 spin_unlock(&sinfo->lock);
4671
4672 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4673 csum_size * 2;
4674 num_bytes += div64_u64(data_used + meta_used, 50);
4675
4676 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004677 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004678
4679 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4680}
4681
4682static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4683{
4684 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4685 struct btrfs_space_info *sinfo = block_rsv->space_info;
4686 u64 num_bytes;
4687
4688 num_bytes = calc_global_metadata_size(fs_info);
4689
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004690 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004691 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004692
Josef Bacikfdf30d12013-03-26 15:31:45 -04004693 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004694
4695 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004696 sinfo->bytes_reserved + sinfo->bytes_readonly +
4697 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004698
4699 if (sinfo->total_bytes > num_bytes) {
4700 num_bytes = sinfo->total_bytes - num_bytes;
4701 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004702 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004703 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004704 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004705 }
4706
4707 if (block_rsv->reserved >= block_rsv->size) {
4708 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004709 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004710 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004711 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004712 block_rsv->reserved = block_rsv->size;
4713 block_rsv->full = 1;
4714 }
David Sterba182608c2011-05-05 13:13:16 +02004715
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004716 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004717 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004718}
4719
Yan, Zhengf0486c62010-05-16 10:46:25 -04004720static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4721{
4722 struct btrfs_space_info *space_info;
4723
4724 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4725 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004726
4727 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004728 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004729 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004730 fs_info->trans_block_rsv.space_info = space_info;
4731 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004732 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004733
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004734 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4735 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4736 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4737 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00004738 if (fs_info->quota_root)
4739 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004740 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004741
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004742 update_global_block_rsv(fs_info);
4743}
4744
4745static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4746{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004747 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4748 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004749 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4750 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4751 WARN_ON(fs_info->trans_block_rsv.size > 0);
4752 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4753 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4754 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004755 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4756 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004757}
4758
Yan, Zhenga22285a2010-05-16 10:48:46 -04004759void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4760 struct btrfs_root *root)
4761{
Josef Bacik0e721102012-06-26 16:13:18 -04004762 if (!trans->block_rsv)
4763 return;
4764
Yan, Zhenga22285a2010-05-16 10:48:46 -04004765 if (!trans->bytes_reserved)
4766 return;
4767
Chris Masone77266e2012-02-24 10:39:05 -05004768 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004769 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004770 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004771 trans->bytes_reserved = 0;
4772}
4773
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004774/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004775int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4776 struct inode *inode)
4777{
4778 struct btrfs_root *root = BTRFS_I(inode)->root;
4779 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4780 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4781
4782 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004783 * We need to hold space in order to delete our orphan item once we've
4784 * added it, so this takes the reservation so we can release it later
4785 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004786 */
Chris Masonff5714c2011-05-28 07:00:39 -04004787 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004788 trace_btrfs_space_reservation(root->fs_info, "orphan",
4789 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004790 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4791}
4792
4793void btrfs_orphan_release_metadata(struct inode *inode)
4794{
4795 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004796 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004797 trace_btrfs_space_reservation(root->fs_info, "orphan",
4798 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004799 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4800}
4801
Miao Xied5c12072013-02-28 10:04:33 +00004802/*
4803 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4804 * root: the root of the parent directory
4805 * rsv: block reservation
4806 * items: the number of items that we need do reservation
4807 * qgroup_reserved: used to return the reserved size in qgroup
4808 *
4809 * This function is used to reserve the space for snapshot/subvolume
4810 * creation and deletion. Those operations are different with the
4811 * common file/directory operations, they change two fs/file trees
4812 * and root tree, the number of items that the qgroup reserves is
4813 * different with the free space reservation. So we can not use
4814 * the space reseravtion mechanism in start_transaction().
4815 */
4816int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4817 struct btrfs_block_rsv *rsv,
4818 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004819 u64 *qgroup_reserved,
4820 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004821{
Miao Xied5c12072013-02-28 10:04:33 +00004822 u64 num_bytes;
4823 int ret;
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004824 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00004825
4826 if (root->fs_info->quota_enabled) {
4827 /* One for parent inode, two for dir entries */
4828 num_bytes = 3 * root->leafsize;
4829 ret = btrfs_qgroup_reserve(root, num_bytes);
4830 if (ret)
4831 return ret;
4832 } else {
4833 num_bytes = 0;
4834 }
4835
4836 *qgroup_reserved = num_bytes;
4837
4838 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4839 rsv->space_info = __find_space_info(root->fs_info,
4840 BTRFS_BLOCK_GROUP_METADATA);
4841 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4842 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004843
4844 if (ret == -ENOSPC && use_global_rsv)
4845 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
4846
Miao Xied5c12072013-02-28 10:04:33 +00004847 if (ret) {
4848 if (*qgroup_reserved)
4849 btrfs_qgroup_free(root, *qgroup_reserved);
4850 }
4851
4852 return ret;
4853}
4854
4855void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4856 struct btrfs_block_rsv *rsv,
4857 u64 qgroup_reserved)
4858{
4859 btrfs_block_rsv_release(root, rsv, (u64)-1);
4860 if (qgroup_reserved)
4861 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004862}
4863
Josef Bacik7709cde2011-08-04 10:25:02 -04004864/**
4865 * drop_outstanding_extent - drop an outstanding extent
4866 * @inode: the inode we're dropping the extent for
4867 *
4868 * This is called when we are freeing up an outstanding extent, either called
4869 * after an error or after an extent is written. This will return the number of
4870 * reserved extents that need to be freed. This must be called with
4871 * BTRFS_I(inode)->lock held.
4872 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004873static unsigned drop_outstanding_extent(struct inode *inode)
4874{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004875 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004876 unsigned dropped_extents = 0;
4877
Josef Bacik9e0baf62011-07-15 15:16:44 +00004878 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4879 BTRFS_I(inode)->outstanding_extents--;
4880
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004881 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004882 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4883 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004884 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004885
Josef Bacik9e0baf62011-07-15 15:16:44 +00004886 /*
4887 * If we have more or the same amount of outsanding extents than we have
4888 * reserved then we need to leave the reserved extents count alone.
4889 */
4890 if (BTRFS_I(inode)->outstanding_extents >=
4891 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004892 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004893
4894 dropped_extents = BTRFS_I(inode)->reserved_extents -
4895 BTRFS_I(inode)->outstanding_extents;
4896 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004897 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004898}
4899
Josef Bacik7709cde2011-08-04 10:25:02 -04004900/**
4901 * calc_csum_metadata_size - return the amount of metada space that must be
4902 * reserved/free'd for the given bytes.
4903 * @inode: the inode we're manipulating
4904 * @num_bytes: the number of bytes in question
4905 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4906 *
4907 * This adjusts the number of csum_bytes in the inode and then returns the
4908 * correct amount of metadata that must either be reserved or freed. We
4909 * calculate how many checksums we can fit into one leaf and then divide the
4910 * number of bytes that will need to be checksumed by this value to figure out
4911 * how many checksums will be required. If we are adding bytes then the number
4912 * may go up and we will return the number of additional bytes that must be
4913 * reserved. If it is going down we will return the number of bytes that must
4914 * be freed.
4915 *
4916 * This must be called with BTRFS_I(inode)->lock held.
4917 */
4918static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4919 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004920{
Josef Bacik7709cde2011-08-04 10:25:02 -04004921 struct btrfs_root *root = BTRFS_I(inode)->root;
4922 u64 csum_size;
4923 int num_csums_per_leaf;
4924 int num_csums;
4925 int old_csums;
4926
4927 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4928 BTRFS_I(inode)->csum_bytes == 0)
4929 return 0;
4930
4931 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4932 if (reserve)
4933 BTRFS_I(inode)->csum_bytes += num_bytes;
4934 else
4935 BTRFS_I(inode)->csum_bytes -= num_bytes;
4936 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4937 num_csums_per_leaf = (int)div64_u64(csum_size,
4938 sizeof(struct btrfs_csum_item) +
4939 sizeof(struct btrfs_disk_key));
4940 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4941 num_csums = num_csums + num_csums_per_leaf - 1;
4942 num_csums = num_csums / num_csums_per_leaf;
4943
4944 old_csums = old_csums + num_csums_per_leaf - 1;
4945 old_csums = old_csums / num_csums_per_leaf;
4946
4947 /* No change, no need to reserve more */
4948 if (old_csums == num_csums)
4949 return 0;
4950
4951 if (reserve)
4952 return btrfs_calc_trans_metadata_size(root,
4953 num_csums - old_csums);
4954
4955 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004956}
4957
4958int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4959{
4960 struct btrfs_root *root = BTRFS_I(inode)->root;
4961 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004962 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004963 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004964 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004965 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004966 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004967 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004968 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004969 u64 to_free = 0;
4970 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004971
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004972 /* If we are a free space inode we need to not flush since we will be in
4973 * the middle of a transaction commit. We also don't need the delalloc
4974 * mutex since we won't race with anybody. We need this mostly to make
4975 * lockdep shut its filthy mouth.
4976 */
4977 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004978 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004979 delalloc_lock = false;
4980 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004981
Miao Xie08e007d2012-10-16 11:33:38 +00004982 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4983 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004984 schedule_timeout(1);
4985
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004986 if (delalloc_lock)
4987 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4988
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004989 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004990
Josef Bacik9e0baf62011-07-15 15:16:44 +00004991 spin_lock(&BTRFS_I(inode)->lock);
4992 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004993
Josef Bacik9e0baf62011-07-15 15:16:44 +00004994 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004995 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004996 nr_extents = BTRFS_I(inode)->outstanding_extents -
4997 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004998
4999 /*
5000 * Add an item to reserve for updating the inode when we complete the
5001 * delalloc io.
5002 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04005003 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5004 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005005 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05005006 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005007 }
5008
5009 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04005010 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05005011 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005012 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05005013
Wang Shilong88e081bf2013-03-01 11:36:01 +00005014 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02005015 ret = btrfs_qgroup_reserve(root, num_bytes +
5016 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005017 if (ret)
5018 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00005019 }
Arne Jansenc5567232011-09-14 15:44:05 +02005020
Wang Shilong88e081bf2013-03-01 11:36:01 +00005021 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
5022 if (unlikely(ret)) {
5023 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00005024 btrfs_qgroup_free(root, num_bytes +
5025 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005026 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005027 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005028
Josef Bacik660d3f62011-12-09 11:18:51 -05005029 spin_lock(&BTRFS_I(inode)->lock);
5030 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04005031 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5032 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05005033 nr_extents--;
5034 }
5035 BTRFS_I(inode)->reserved_extents += nr_extents;
5036 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005037
5038 if (delalloc_lock)
5039 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05005040
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005041 if (to_reserve)
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05305042 trace_btrfs_space_reservation(root->fs_info, "delalloc",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005043 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005044 block_rsv_add_bytes(block_rsv, to_reserve, 1);
5045
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005046 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005047
5048out_fail:
5049 spin_lock(&BTRFS_I(inode)->lock);
5050 dropped = drop_outstanding_extent(inode);
5051 /*
5052 * If the inodes csum_bytes is the same as the original
5053 * csum_bytes then we know we haven't raced with any free()ers
5054 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00005055 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04005056 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00005057 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04005058 } else {
5059 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5060 u64 bytes;
5061
5062 /*
5063 * This is tricky, but first we need to figure out how much we
5064 * free'd from any free-ers that occured during this
5065 * reservation, so we reset ->csum_bytes to the csum_bytes
5066 * before we dropped our lock, and then call the free for the
5067 * number of bytes that were freed while we were trying our
5068 * reservation.
5069 */
5070 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5071 BTRFS_I(inode)->csum_bytes = csum_bytes;
5072 to_free = calc_csum_metadata_size(inode, bytes, 0);
5073
5074
5075 /*
5076 * Now we need to see how much we would have freed had we not
5077 * been making this reservation and our ->csum_bytes were not
5078 * artificially inflated.
5079 */
5080 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5081 bytes = csum_bytes - orig_csum_bytes;
5082 bytes = calc_csum_metadata_size(inode, bytes, 0);
5083
5084 /*
5085 * Now reset ->csum_bytes to what it should be. If bytes is
5086 * more than to_free then we would have free'd more space had we
5087 * not had an artificially high ->csum_bytes, so we need to free
5088 * the remainder. If bytes is the same or less then we don't
5089 * need to do anything, the other free-ers did the correct
5090 * thing.
5091 */
5092 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5093 if (bytes > to_free)
5094 to_free = bytes - to_free;
5095 else
5096 to_free = 0;
5097 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005098 spin_unlock(&BTRFS_I(inode)->lock);
5099 if (dropped)
5100 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5101
5102 if (to_free) {
5103 btrfs_block_rsv_release(root, block_rsv, to_free);
5104 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5105 btrfs_ino(inode), to_free, 0);
5106 }
5107 if (delalloc_lock)
5108 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5109 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005110}
5111
Josef Bacik7709cde2011-08-04 10:25:02 -04005112/**
5113 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5114 * @inode: the inode to release the reservation for
5115 * @num_bytes: the number of bytes we're releasing
5116 *
5117 * This will release the metadata reservation for an inode. This can be called
5118 * once we complete IO for a given set of bytes to release their metadata
5119 * reservations.
5120 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005121void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5122{
5123 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005124 u64 to_free = 0;
5125 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005126
5127 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005128 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005129 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005130
Miao Xie09348562013-02-07 10:12:07 +00005131 if (num_bytes)
5132 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005133 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005134 if (dropped > 0)
5135 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005136
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005137 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5138 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005139 if (root->fs_info->quota_enabled) {
5140 btrfs_qgroup_free(root, num_bytes +
5141 dropped * root->leafsize);
5142 }
5143
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005144 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5145 to_free);
5146}
5147
Josef Bacik7709cde2011-08-04 10:25:02 -04005148/**
5149 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5150 * @inode: inode we're writing to
5151 * @num_bytes: the number of bytes we want to allocate
5152 *
5153 * This will do the following things
5154 *
5155 * o reserve space in the data space info for num_bytes
5156 * o reserve space in the metadata space info based on number of outstanding
5157 * extents and how much csums will be needed
5158 * o add to the inodes ->delalloc_bytes
5159 * o add it to the fs_info's delalloc inodes list.
5160 *
5161 * This will return 0 for success and -ENOSPC if there is no space left.
5162 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005163int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5164{
5165 int ret;
5166
5167 ret = btrfs_check_data_free_space(inode, num_bytes);
5168 if (ret)
5169 return ret;
5170
5171 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5172 if (ret) {
5173 btrfs_free_reserved_data_space(inode, num_bytes);
5174 return ret;
5175 }
5176
5177 return 0;
5178}
5179
Josef Bacik7709cde2011-08-04 10:25:02 -04005180/**
5181 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5182 * @inode: inode we're releasing space for
5183 * @num_bytes: the number of bytes we want to free up
5184 *
5185 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5186 * called in the case that we don't need the metadata AND data reservations
5187 * anymore. So if there is an error or we insert an inline extent.
5188 *
5189 * This function will release the metadata space that was not used and will
5190 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5191 * list if there are no delalloc bytes left.
5192 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005193void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5194{
5195 btrfs_delalloc_release_metadata(inode, num_bytes);
5196 btrfs_free_reserved_data_space(inode, num_bytes);
5197}
5198
Liu Boc53d6132012-12-27 09:01:19 +00005199static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005200 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005201{
Josef Bacik0af3d002010-06-21 14:48:16 -04005202 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005203 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005204 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005205 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005206 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005207 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005208
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005209 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005210 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005211 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005212 if (alloc)
5213 old_val += num_bytes;
5214 else
5215 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005216 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005217 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005218
Chris Masond3977122009-01-05 21:25:51 -05005219 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005220 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005221 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005222 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005223 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5224 BTRFS_BLOCK_GROUP_RAID1 |
5225 BTRFS_BLOCK_GROUP_RAID10))
5226 factor = 2;
5227 else
5228 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005229 /*
5230 * If this block group has free space cache written out, we
5231 * need to make sure to load it if we are removing space. This
5232 * is because we need the unpinning stage to actually add the
5233 * space back to the block group, otherwise we will leak space.
5234 */
5235 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005236 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005237
Chris Masondb945352007-10-15 16:15:53 -04005238 byte_in_group = bytenr - cache->key.objectid;
5239 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005240
Josef Bacik25179202008-10-29 14:49:05 -04005241 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005242 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005243
Josef Bacik73bc1872011-10-03 14:07:49 -04005244 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005245 cache->disk_cache_state < BTRFS_DC_CLEAR)
5246 cache->disk_cache_state = BTRFS_DC_CLEAR;
5247
Josef Bacik0f9dd462008-09-23 13:14:11 -04005248 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005249 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005250 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005251 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005252 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005253 btrfs_set_block_group_used(&cache->item, old_val);
5254 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005255 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005256 cache->space_info->bytes_used += num_bytes;
5257 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005258 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005259 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005260 } else {
Chris Masondb945352007-10-15 16:15:53 -04005261 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005262 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005263 cache->pinned += num_bytes;
5264 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005265 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005266 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005267 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005268 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005269
Yan, Zhengf0486c62010-05-16 10:46:25 -04005270 set_extent_dirty(info->pinned_extents,
5271 bytenr, bytenr + num_bytes - 1,
5272 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005273 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005274 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005275 total -= num_bytes;
5276 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005277 }
5278 return 0;
5279}
Chris Mason6324fbf2008-03-24 15:01:59 -04005280
Chris Masona061fc82008-05-07 11:43:44 -04005281static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5282{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005283 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005284 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005285
Liu Boa1897fd2012-12-27 09:01:23 +00005286 spin_lock(&root->fs_info->block_group_cache_lock);
5287 bytenr = root->fs_info->first_logical_byte;
5288 spin_unlock(&root->fs_info->block_group_cache_lock);
5289
5290 if (bytenr < (u64)-1)
5291 return bytenr;
5292
Josef Bacik0f9dd462008-09-23 13:14:11 -04005293 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5294 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005295 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005296
Yan Zhengd2fb3432008-12-11 16:30:39 -05005297 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005298 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005299
5300 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005301}
5302
Yan, Zhengf0486c62010-05-16 10:46:25 -04005303static int pin_down_extent(struct btrfs_root *root,
5304 struct btrfs_block_group_cache *cache,
5305 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005306{
Yan Zheng11833d62009-09-11 16:11:19 -04005307 spin_lock(&cache->space_info->lock);
5308 spin_lock(&cache->lock);
5309 cache->pinned += num_bytes;
5310 cache->space_info->bytes_pinned += num_bytes;
5311 if (reserved) {
5312 cache->reserved -= num_bytes;
5313 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005314 }
Yan Zheng11833d62009-09-11 16:11:19 -04005315 spin_unlock(&cache->lock);
5316 spin_unlock(&cache->space_info->lock);
5317
Yan, Zhengf0486c62010-05-16 10:46:25 -04005318 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5319 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Josef Bacik0be5dc62013-10-07 15:18:52 -04005320 if (reserved)
5321 trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
Yan324ae4d2007-11-16 14:57:08 -05005322 return 0;
5323}
Chris Mason9078a3e2007-04-26 16:46:15 -04005324
Yan, Zhengf0486c62010-05-16 10:46:25 -04005325/*
5326 * this function must be called within transaction
5327 */
5328int btrfs_pin_extent(struct btrfs_root *root,
5329 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005330{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005331 struct btrfs_block_group_cache *cache;
5332
5333 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005334 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005335
5336 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5337
5338 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005339 return 0;
5340}
Zheng Yane8569812008-09-26 10:05:48 -04005341
Yan, Zhengf0486c62010-05-16 10:46:25 -04005342/*
Chris Masone688b722011-10-31 20:52:39 -04005343 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005344 */
Liu Bodcfac412012-12-27 09:01:20 +00005345int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b722011-10-31 20:52:39 -04005346 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005347{
Chris Masone688b722011-10-31 20:52:39 -04005348 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005349 int ret;
Chris Masone688b722011-10-31 20:52:39 -04005350
5351 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005352 if (!cache)
5353 return -EINVAL;
Chris Masone688b722011-10-31 20:52:39 -04005354
5355 /*
5356 * pull in the free space cache (if any) so that our pin
5357 * removes the free space from the cache. We have load_only set
5358 * to one because the slow code to read in the free extents does check
5359 * the pinned extents.
5360 */
Liu Bof6373bf2012-12-27 09:01:18 +00005361 cache_block_group(cache, 1);
Chris Masone688b722011-10-31 20:52:39 -04005362
5363 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5364
5365 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005366 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b722011-10-31 20:52:39 -04005367 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005368 return ret;
Chris Masone688b722011-10-31 20:52:39 -04005369}
5370
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005371static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5372{
5373 int ret;
5374 struct btrfs_block_group_cache *block_group;
5375 struct btrfs_caching_control *caching_ctl;
5376
5377 block_group = btrfs_lookup_block_group(root->fs_info, start);
5378 if (!block_group)
5379 return -EINVAL;
5380
5381 cache_block_group(block_group, 0);
5382 caching_ctl = get_caching_control(block_group);
5383
5384 if (!caching_ctl) {
5385 /* Logic error */
5386 BUG_ON(!block_group_cache_done(block_group));
5387 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5388 } else {
5389 mutex_lock(&caching_ctl->mutex);
5390
5391 if (start >= caching_ctl->progress) {
5392 ret = add_excluded_extent(root, start, num_bytes);
5393 } else if (start + num_bytes <= caching_ctl->progress) {
5394 ret = btrfs_remove_free_space(block_group,
5395 start, num_bytes);
5396 } else {
5397 num_bytes = caching_ctl->progress - start;
5398 ret = btrfs_remove_free_space(block_group,
5399 start, num_bytes);
5400 if (ret)
5401 goto out_lock;
5402
5403 num_bytes = (start + num_bytes) -
5404 caching_ctl->progress;
5405 start = caching_ctl->progress;
5406 ret = add_excluded_extent(root, start, num_bytes);
5407 }
5408out_lock:
5409 mutex_unlock(&caching_ctl->mutex);
5410 put_caching_control(caching_ctl);
5411 }
5412 btrfs_put_block_group(block_group);
5413 return ret;
5414}
5415
5416int btrfs_exclude_logged_extents(struct btrfs_root *log,
5417 struct extent_buffer *eb)
5418{
5419 struct btrfs_file_extent_item *item;
5420 struct btrfs_key key;
5421 int found_type;
5422 int i;
5423
5424 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5425 return 0;
5426
5427 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5428 btrfs_item_key_to_cpu(eb, &key, i);
5429 if (key.type != BTRFS_EXTENT_DATA_KEY)
5430 continue;
5431 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5432 found_type = btrfs_file_extent_type(eb, item);
5433 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5434 continue;
5435 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5436 continue;
5437 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5438 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5439 __exclude_logged_extent(log, key.objectid, key.offset);
5440 }
5441
5442 return 0;
5443}
5444
Josef Bacikfb25e912011-07-26 17:00:46 -04005445/**
5446 * btrfs_update_reserved_bytes - update the block_group and space info counters
5447 * @cache: The cache we are manipulating
5448 * @num_bytes: The number of bytes in question
5449 * @reserve: One of the reservation enums
5450 *
5451 * This is called by the allocator when it reserves space, or by somebody who is
5452 * freeing space that was never actually used on disk. For example if you
5453 * reserve some space for a new leaf in transaction A and before transaction A
5454 * commits you free that leaf, you call this with reserve set to 0 in order to
5455 * clear the reservation.
5456 *
5457 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5458 * ENOSPC accounting. For data we handle the reservation through clearing the
5459 * delalloc bits in the io_tree. We have to do this since we could end up
5460 * allocating less disk space for the amount of data we have reserved in the
5461 * case of compression.
5462 *
5463 * If this is a reservation and the block group has become read only we cannot
5464 * make the reservation and return -EAGAIN, otherwise this function always
5465 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005466 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005467static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5468 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005469{
Josef Bacikfb25e912011-07-26 17:00:46 -04005470 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005471 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005472
Josef Bacikfb25e912011-07-26 17:00:46 -04005473 spin_lock(&space_info->lock);
5474 spin_lock(&cache->lock);
5475 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005476 if (cache->ro) {
5477 ret = -EAGAIN;
5478 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005479 cache->reserved += num_bytes;
5480 space_info->bytes_reserved += num_bytes;
5481 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005482 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005483 "space_info", space_info->flags,
5484 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005485 space_info->bytes_may_use -= num_bytes;
5486 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005487 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005488 } else {
5489 if (cache->ro)
5490 space_info->bytes_readonly += num_bytes;
5491 cache->reserved -= num_bytes;
5492 space_info->bytes_reserved -= num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005493 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005494 spin_unlock(&cache->lock);
5495 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005496 return ret;
5497}
5498
Jeff Mahoney143bede2012-03-01 14:56:26 +01005499void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005500 struct btrfs_root *root)
5501{
5502 struct btrfs_fs_info *fs_info = root->fs_info;
5503 struct btrfs_caching_control *next;
5504 struct btrfs_caching_control *caching_ctl;
5505 struct btrfs_block_group_cache *cache;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005506 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04005507
5508 down_write(&fs_info->extent_commit_sem);
5509
5510 list_for_each_entry_safe(caching_ctl, next,
5511 &fs_info->caching_block_groups, list) {
5512 cache = caching_ctl->block_group;
5513 if (block_group_cache_done(cache)) {
5514 cache->last_byte_to_unpin = (u64)-1;
5515 list_del_init(&caching_ctl->list);
5516 put_caching_control(caching_ctl);
5517 } else {
5518 cache->last_byte_to_unpin = caching_ctl->progress;
5519 }
5520 }
5521
5522 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5523 fs_info->pinned_extents = &fs_info->freed_extents[1];
5524 else
5525 fs_info->pinned_extents = &fs_info->freed_extents[0];
5526
5527 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005528
Josef Bacikb150a4f2013-06-19 15:00:04 -04005529 list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
5530 percpu_counter_set(&space_info->total_bytes_pinned, 0);
5531
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005532 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005533}
5534
5535static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5536{
5537 struct btrfs_fs_info *fs_info = root->fs_info;
5538 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005539 struct btrfs_space_info *space_info;
5540 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005541 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005542 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005543
5544 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005545 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005546 if (!cache ||
5547 start >= cache->key.objectid + cache->key.offset) {
5548 if (cache)
5549 btrfs_put_block_group(cache);
5550 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005551 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005552 }
5553
5554 len = cache->key.objectid + cache->key.offset - start;
5555 len = min(len, end + 1 - start);
5556
5557 if (start < cache->last_byte_to_unpin) {
5558 len = min(len, cache->last_byte_to_unpin - start);
5559 btrfs_add_free_space(cache, start, len);
5560 }
Josef Bacik25179202008-10-29 14:49:05 -04005561
Yan, Zhengf0486c62010-05-16 10:46:25 -04005562 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005563 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005564
Josef Bacik7b398f82012-10-22 15:52:28 -04005565 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005566 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005567 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005568 space_info->bytes_pinned -= len;
5569 if (cache->ro) {
5570 space_info->bytes_readonly += len;
5571 readonly = true;
5572 }
Josef Bacik25179202008-10-29 14:49:05 -04005573 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005574 if (!readonly && global_rsv->space_info == space_info) {
5575 spin_lock(&global_rsv->lock);
5576 if (!global_rsv->full) {
5577 len = min(len, global_rsv->size -
5578 global_rsv->reserved);
5579 global_rsv->reserved += len;
5580 space_info->bytes_may_use += len;
5581 if (global_rsv->reserved >= global_rsv->size)
5582 global_rsv->full = 1;
5583 }
5584 spin_unlock(&global_rsv->lock);
5585 }
5586 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005587 }
5588
5589 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005590 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005591 return 0;
5592}
5593
5594int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005595 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005596{
Yan Zheng11833d62009-09-11 16:11:19 -04005597 struct btrfs_fs_info *fs_info = root->fs_info;
5598 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005599 u64 start;
5600 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005601 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005602
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005603 if (trans->aborted)
5604 return 0;
5605
Yan Zheng11833d62009-09-11 16:11:19 -04005606 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5607 unpin = &fs_info->freed_extents[1];
5608 else
5609 unpin = &fs_info->freed_extents[0];
5610
Chris Masond3977122009-01-05 21:25:51 -05005611 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005612 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005613 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005614 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005615 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005616
Li Dongyang5378e602011-03-24 10:24:27 +00005617 if (btrfs_test_opt(root, DISCARD))
5618 ret = btrfs_discard_extent(root, start,
5619 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005620
Chris Mason1a5bc162007-10-15 16:15:26 -04005621 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005622 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005623 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005624 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005625
Chris Masone20d96d2007-03-22 12:13:20 -04005626 return 0;
5627}
5628
Josef Bacikb150a4f2013-06-19 15:00:04 -04005629static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5630 u64 owner, u64 root_objectid)
5631{
5632 struct btrfs_space_info *space_info;
5633 u64 flags;
5634
5635 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5636 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5637 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5638 else
5639 flags = BTRFS_BLOCK_GROUP_METADATA;
5640 } else {
5641 flags = BTRFS_BLOCK_GROUP_DATA;
5642 }
5643
5644 space_info = __find_space_info(fs_info, flags);
5645 BUG_ON(!space_info); /* Logic bug */
5646 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5647}
5648
5649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005650static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5651 struct btrfs_root *root,
5652 u64 bytenr, u64 num_bytes, u64 parent,
5653 u64 root_objectid, u64 owner_objectid,
5654 u64 owner_offset, int refs_to_drop,
5655 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005656{
Chris Masone2fa7222007-03-12 16:22:34 -04005657 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005658 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005659 struct btrfs_fs_info *info = root->fs_info;
5660 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005661 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005662 struct btrfs_extent_item *ei;
5663 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005664 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005665 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005666 int extent_slot = 0;
5667 int found_extent = 0;
5668 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005669 u32 item_size;
5670 u64 refs;
Josef Bacik3173a182013-03-07 14:22:04 -05005671 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5672 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005673
Chris Mason5caf2a02007-04-02 11:20:42 -04005674 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005675 if (!path)
5676 return -ENOMEM;
5677
Chris Mason3c12ac72008-04-21 12:01:38 -04005678 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005679 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680
5681 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5682 BUG_ON(!is_data && refs_to_drop != 1);
5683
Josef Bacik3173a182013-03-07 14:22:04 -05005684 if (is_data)
5685 skinny_metadata = 0;
5686
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005687 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5688 bytenr, num_bytes, parent,
5689 root_objectid, owner_objectid,
5690 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005691 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005692 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005693 while (extent_slot >= 0) {
5694 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005695 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005696 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005697 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005698 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5699 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005700 found_extent = 1;
5701 break;
5702 }
Josef Bacik3173a182013-03-07 14:22:04 -05005703 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5704 key.offset == owner_objectid) {
5705 found_extent = 1;
5706 break;
5707 }
Chris Mason952fcca2008-02-18 16:33:44 -05005708 if (path->slots[0] - extent_slot > 5)
5709 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005710 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005711 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005712#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5713 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5714 if (found_extent && item_size < sizeof(*ei))
5715 found_extent = 0;
5716#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005717 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005718 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005719 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005720 NULL, refs_to_drop,
5721 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005722 if (ret) {
5723 btrfs_abort_transaction(trans, extent_root, ret);
5724 goto out;
5725 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005726 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005727 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005728
5729 key.objectid = bytenr;
5730 key.type = BTRFS_EXTENT_ITEM_KEY;
5731 key.offset = num_bytes;
5732
Josef Bacik3173a182013-03-07 14:22:04 -05005733 if (!is_data && skinny_metadata) {
5734 key.type = BTRFS_METADATA_ITEM_KEY;
5735 key.offset = owner_objectid;
5736 }
5737
Zheng Yan31840ae2008-09-23 13:14:14 -04005738 ret = btrfs_search_slot(trans, extent_root,
5739 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005740 if (ret > 0 && skinny_metadata && path->slots[0]) {
5741 /*
5742 * Couldn't find our skinny metadata item,
5743 * see if we have ye olde extent item.
5744 */
5745 path->slots[0]--;
5746 btrfs_item_key_to_cpu(path->nodes[0], &key,
5747 path->slots[0]);
5748 if (key.objectid == bytenr &&
5749 key.type == BTRFS_EXTENT_ITEM_KEY &&
5750 key.offset == num_bytes)
5751 ret = 0;
5752 }
5753
5754 if (ret > 0 && skinny_metadata) {
5755 skinny_metadata = false;
5756 key.type = BTRFS_EXTENT_ITEM_KEY;
5757 key.offset = num_bytes;
5758 btrfs_release_path(path);
5759 ret = btrfs_search_slot(trans, extent_root,
5760 &key, path, -1, 1);
5761 }
5762
Josef Bacikf3465ca2008-11-12 14:19:50 -05005763 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005764 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005765 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005766 if (ret > 0)
5767 btrfs_print_leaf(extent_root,
5768 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005769 }
David Sterba005d6422012-09-18 07:52:32 -06005770 if (ret < 0) {
5771 btrfs_abort_transaction(trans, extent_root, ret);
5772 goto out;
5773 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005774 extent_slot = path->slots[0];
5775 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305776 } else if (WARN_ON(ret == -ENOENT)) {
Chris Mason7bb86312007-12-11 09:25:06 -05005777 btrfs_print_leaf(extent_root, path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005778 btrfs_err(info,
5779 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005780 bytenr, parent, root_objectid, owner_objectid,
5781 owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005782 } else {
David Sterba005d6422012-09-18 07:52:32 -06005783 btrfs_abort_transaction(trans, extent_root, ret);
5784 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005785 }
Chris Mason5f39d392007-10-15 16:14:19 -04005786
5787 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005788 item_size = btrfs_item_size_nr(leaf, extent_slot);
5789#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5790 if (item_size < sizeof(*ei)) {
5791 BUG_ON(found_extent || extent_slot != path->slots[0]);
5792 ret = convert_extent_item_v0(trans, extent_root, path,
5793 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005794 if (ret < 0) {
5795 btrfs_abort_transaction(trans, extent_root, ret);
5796 goto out;
5797 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005798
David Sterbab3b4aa72011-04-21 01:20:15 +02005799 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005800 path->leave_spinning = 1;
5801
5802 key.objectid = bytenr;
5803 key.type = BTRFS_EXTENT_ITEM_KEY;
5804 key.offset = num_bytes;
5805
5806 ret = btrfs_search_slot(trans, extent_root, &key, path,
5807 -1, 1);
5808 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005809 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005810 ret, bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005811 btrfs_print_leaf(extent_root, path->nodes[0]);
5812 }
David Sterba005d6422012-09-18 07:52:32 -06005813 if (ret < 0) {
5814 btrfs_abort_transaction(trans, extent_root, ret);
5815 goto out;
5816 }
5817
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005818 extent_slot = path->slots[0];
5819 leaf = path->nodes[0];
5820 item_size = btrfs_item_size_nr(leaf, extent_slot);
5821 }
5822#endif
5823 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005824 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005825 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005826 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5827 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005828 struct btrfs_tree_block_info *bi;
5829 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5830 bi = (struct btrfs_tree_block_info *)(ei + 1);
5831 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005832 }
5833
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005834 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04005835 if (refs < refs_to_drop) {
5836 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
5837 "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
5838 ret = -EINVAL;
5839 btrfs_abort_transaction(trans, extent_root, ret);
5840 goto out;
5841 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005842 refs -= refs_to_drop;
5843
5844 if (refs > 0) {
5845 if (extent_op)
5846 __run_delayed_extent_op(extent_op, leaf, ei);
5847 /*
5848 * In the case of inline back ref, reference count will
5849 * be updated by remove_extent_backref
5850 */
5851 if (iref) {
5852 BUG_ON(!found_extent);
5853 } else {
5854 btrfs_set_extent_refs(leaf, ei, refs);
5855 btrfs_mark_buffer_dirty(leaf);
5856 }
5857 if (found_extent) {
5858 ret = remove_extent_backref(trans, extent_root, path,
5859 iref, refs_to_drop,
5860 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005861 if (ret) {
5862 btrfs_abort_transaction(trans, extent_root, ret);
5863 goto out;
5864 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005865 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04005866 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
5867 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005868 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005869 if (found_extent) {
5870 BUG_ON(is_data && refs_to_drop !=
5871 extent_data_ref_count(root, path, iref));
5872 if (iref) {
5873 BUG_ON(path->slots[0] != extent_slot);
5874 } else {
5875 BUG_ON(path->slots[0] != extent_slot + 1);
5876 path->slots[0] = extent_slot;
5877 num_to_del = 2;
5878 }
Chris Mason78fae272007-03-25 11:35:08 -04005879 }
Chris Masonb9473432009-03-13 11:00:37 -04005880
Chris Mason952fcca2008-02-18 16:33:44 -05005881 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5882 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005883 if (ret) {
5884 btrfs_abort_transaction(trans, extent_root, ret);
5885 goto out;
5886 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005887 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005888
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005889 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005890 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005891 if (ret) {
5892 btrfs_abort_transaction(trans, extent_root, ret);
5893 goto out;
5894 }
Chris Mason459931e2008-12-10 09:10:46 -05005895 }
5896
Liu Boc53d6132012-12-27 09:01:19 +00005897 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005898 if (ret) {
5899 btrfs_abort_transaction(trans, extent_root, ret);
5900 goto out;
5901 }
Chris Masona28ec192007-03-06 20:08:01 -05005902 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005903out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005904 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005905 return ret;
5906}
5907
5908/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005909 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005910 * delayed ref for that extent as well. This searches the delayed ref tree for
5911 * a given extent, and if there are no other delayed refs to be processed, it
5912 * removes it from the tree.
5913 */
5914static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5915 struct btrfs_root *root, u64 bytenr)
5916{
5917 struct btrfs_delayed_ref_head *head;
5918 struct btrfs_delayed_ref_root *delayed_refs;
5919 struct btrfs_delayed_ref_node *ref;
5920 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005921 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005922
5923 delayed_refs = &trans->transaction->delayed_refs;
5924 spin_lock(&delayed_refs->lock);
5925 head = btrfs_find_delayed_ref_head(trans, bytenr);
5926 if (!head)
5927 goto out;
5928
5929 node = rb_prev(&head->node.rb_node);
5930 if (!node)
5931 goto out;
5932
5933 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5934
5935 /* there are still entries for this ref, we can't drop it */
5936 if (ref->bytenr == bytenr)
5937 goto out;
5938
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005939 if (head->extent_op) {
5940 if (!head->must_insert_reserved)
5941 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005942 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005943 head->extent_op = NULL;
5944 }
5945
Chris Mason1887be62009-03-13 10:11:24 -04005946 /*
5947 * waiting for the lock here would deadlock. If someone else has it
5948 * locked they are already in the process of dropping it anyway
5949 */
5950 if (!mutex_trylock(&head->mutex))
5951 goto out;
5952
5953 /*
5954 * at this point we have a head with no other entries. Go
5955 * ahead and process it.
5956 */
5957 head->node.in_tree = 0;
5958 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005959
Chris Mason1887be62009-03-13 10:11:24 -04005960 delayed_refs->num_entries--;
5961
5962 /*
5963 * we don't take a ref on the node because we're removing it from the
5964 * tree, so we just steal the ref the tree was holding.
5965 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005966 delayed_refs->num_heads--;
5967 if (list_empty(&head->cluster))
5968 delayed_refs->num_heads_ready--;
5969
5970 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005971 spin_unlock(&delayed_refs->lock);
5972
Yan, Zhengf0486c62010-05-16 10:46:25 -04005973 BUG_ON(head->extent_op);
5974 if (head->must_insert_reserved)
5975 ret = 1;
5976
5977 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005978 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005979 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005980out:
5981 spin_unlock(&delayed_refs->lock);
5982 return 0;
5983}
5984
Yan, Zhengf0486c62010-05-16 10:46:25 -04005985void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5986 struct btrfs_root *root,
5987 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005988 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005989{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005990 struct btrfs_block_group_cache *cache = NULL;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005991 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005992 int ret;
5993
5994 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005995 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5996 buf->start, buf->len,
5997 parent, root->root_key.objectid,
5998 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005999 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006000 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006001 }
6002
6003 if (!last_ref)
6004 return;
6005
Yan, Zhengf0486c62010-05-16 10:46:25 -04006006 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006007
6008 if (btrfs_header_generation(buf) == trans->transid) {
6009 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6010 ret = check_ref_cleanup(trans, root, buf->start);
6011 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04006012 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006013 }
6014
6015 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
6016 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04006017 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006018 }
6019
6020 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6021
6022 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04006023 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Josef Bacik0be5dc62013-10-07 15:18:52 -04006024 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
Josef Bacikb150a4f2013-06-19 15:00:04 -04006025 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006026 }
6027out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04006028 if (pin)
6029 add_pinned_bytes(root->fs_info, buf->len,
6030 btrfs_header_level(buf),
6031 root->root_key.objectid);
6032
Josef Bacika826d6d2011-03-16 13:42:43 -04006033 /*
6034 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6035 * anymore.
6036 */
6037 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006038 btrfs_put_block_group(cache);
6039}
6040
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006041/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006042int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6043 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
6044 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04006045{
6046 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006047 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04006048
Josef Bacikb150a4f2013-06-19 15:00:04 -04006049 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
6050
Chris Mason56bec292009-03-13 10:10:06 -04006051 /*
6052 * tree log blocks never actually go into the extent allocation
6053 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04006054 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006055 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6056 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04006057 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04006058 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04006059 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006060 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006061 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6062 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006063 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006064 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006065 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006066 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6067 num_bytes,
6068 parent, root_objectid, owner,
6069 offset, BTRFS_DROP_DELAYED_REF,
6070 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04006071 }
Chris Mason925baed2008-06-25 16:01:30 -04006072 return ret;
6073}
6074
David Woodhouse53b381b2013-01-29 18:40:14 -05006075static u64 stripe_align(struct btrfs_root *root,
6076 struct btrfs_block_group_cache *cache,
6077 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05006078{
Qu Wenruofda28322013-02-26 08:10:22 +00006079 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05006080 return ret;
6081}
6082
Chris Masonfec577f2007-02-26 10:40:21 -05006083/*
Josef Bacik817d52f2009-07-13 21:29:25 -04006084 * when we wait for progress in the block group caching, its because
6085 * our allocation attempt failed at least once. So, we must sleep
6086 * and let some progress happen before we try again.
6087 *
6088 * This function will sleep at least once waiting for new free space to
6089 * show up, and then it will check the block group free space numbers
6090 * for our min num_bytes. Another option is to have it go ahead
6091 * and look in the rbtree for a free extent of a given size, but this
6092 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04006093 *
6094 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6095 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04006096 */
Josef Bacik36cce922013-08-05 11:15:21 -04006097static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04006098wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6099 u64 num_bytes)
6100{
Yan Zheng11833d62009-09-11 16:11:19 -04006101 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006102
Yan Zheng11833d62009-09-11 16:11:19 -04006103 caching_ctl = get_caching_control(cache);
6104 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006105 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04006106
Yan Zheng11833d62009-09-11 16:11:19 -04006107 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08006108 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006109
6110 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04006111}
6112
6113static noinline int
6114wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6115{
6116 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04006117 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006118
6119 caching_ctl = get_caching_control(cache);
6120 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006121 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006122
6123 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04006124 if (cache->cached == BTRFS_CACHE_ERROR)
6125 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04006126 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04006127 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04006128}
6129
Liu Bo31e50222012-11-21 14:18:10 +00006130int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04006131{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006132 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006133 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006134 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006135 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006136 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006137 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006138 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006139 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006140 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006141 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006142 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006143 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006144
Chris Masone942f882013-02-20 14:06:05 -05006145 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04006146}
6147
Ilya Dryomov7738a532012-03-27 17:09:17 +03006148static int get_block_group_index(struct btrfs_block_group_cache *cache)
6149{
Liu Bo31e50222012-11-21 14:18:10 +00006150 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006151}
6152
Josef Bacik817d52f2009-07-13 21:29:25 -04006153enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006154 LOOP_CACHING_NOWAIT = 0,
6155 LOOP_CACHING_WAIT = 1,
6156 LOOP_ALLOC_CHUNK = 2,
6157 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006158};
6159
6160/*
Chris Masonfec577f2007-02-26 10:40:21 -05006161 * walks the btree of allocated extents and find a hole of a given size.
6162 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08006163 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04006164 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08006165 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05006166 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08006167 *
6168 * If there is no suitable free space, we will record the max size of
6169 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05006170 */
Josef Bacik00361582013-08-14 14:02:47 -04006171static noinline int find_free_extent(struct btrfs_root *orig_root,
Chris Mason98ed5172008-01-03 10:01:48 -05006172 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05006173 u64 hint_byte, struct btrfs_key *ins,
David Sterbab6919a52013-04-29 13:39:40 +00006174 u64 flags)
Chris Masonfec577f2007-02-26 10:40:21 -05006175{
Josef Bacik80eb2342008-10-29 14:49:05 -04006176 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05006177 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006178 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04006179 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006180 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05006181 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08006182 u64 max_extent_size = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04006183 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04006184 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006185 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00006186 int index = __get_raid_index(flags);
6187 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04006188 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04006189 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04006190 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006191 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04006192 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006193 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006194
Chris Masondb945352007-10-15 16:15:53 -04006195 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04006196 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04006197 ins->objectid = 0;
6198 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006199
David Sterbab6919a52013-04-29 13:39:40 +00006200 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006201
David Sterbab6919a52013-04-29 13:39:40 +00006202 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006203 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006204 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006205 return -ENOSPC;
6206 }
Josef Bacik2552d172009-04-03 10:14:19 -04006207
Josef Bacik67377732010-09-16 16:19:09 -04006208 /*
6209 * If the space info is for both data and metadata it means we have a
6210 * small filesystem and we can't use the clustering stuff.
6211 */
6212 if (btrfs_mixed_space_info(space_info))
6213 use_cluster = false;
6214
David Sterbab6919a52013-04-29 13:39:40 +00006215 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006216 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05006217 if (!btrfs_test_opt(root, SSD))
6218 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04006219 }
6220
David Sterbab6919a52013-04-29 13:39:40 +00006221 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
Josef Bacik67377732010-09-16 16:19:09 -04006222 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006223 last_ptr = &root->fs_info->data_alloc_cluster;
6224 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006225
Chris Mason239b14b2008-03-24 15:02:07 -04006226 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006227 spin_lock(&last_ptr->lock);
6228 if (last_ptr->block_group)
6229 hint_byte = last_ptr->window_start;
6230 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04006231 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006232
Chris Masona061fc82008-05-07 11:43:44 -04006233 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04006234 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04006235
Josef Bacik817d52f2009-07-13 21:29:25 -04006236 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04006237 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006238
Josef Bacik2552d172009-04-03 10:14:19 -04006239 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04006240 block_group = btrfs_lookup_block_group(root->fs_info,
6241 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006242 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04006243 /*
6244 * we don't want to use the block group if it doesn't match our
6245 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05006246 *
6247 * However if we are re-searching with an ideal block group
6248 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006249 */
David Sterbab6919a52013-04-29 13:39:40 +00006250 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006251 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006252 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006253 if (list_empty(&block_group->list) ||
6254 block_group->ro) {
6255 /*
6256 * someone is removing this block group,
6257 * we can't jump into the have_block_group
6258 * target because our list pointers are not
6259 * valid
6260 */
6261 btrfs_put_block_group(block_group);
6262 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006263 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006264 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006265 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006266 }
Josef Bacik2552d172009-04-03 10:14:19 -04006267 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006268 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006269 }
Chris Mason42e70e72008-11-07 18:17:11 -05006270 }
Josef Bacik2552d172009-04-03 10:14:19 -04006271search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006272 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006273 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006274 list_for_each_entry(block_group, &space_info->block_groups[index],
6275 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04006276 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006277 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05006278
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006279 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00006280 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006281 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006282
Chris Mason83a50de2010-12-13 15:06:46 -05006283 /*
6284 * this can happen if we end up cycling through all the
6285 * raid types, but we want to make sure we only allocate
6286 * for the proper type.
6287 */
David Sterbab6919a52013-04-29 13:39:40 +00006288 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05006289 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6290 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006291 BTRFS_BLOCK_GROUP_RAID5 |
6292 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006293 BTRFS_BLOCK_GROUP_RAID10;
6294
6295 /*
6296 * if they asked for extra copies and this block group
6297 * doesn't provide them, bail. This does allow us to
6298 * fill raid0 from raid1.
6299 */
David Sterbab6919a52013-04-29 13:39:40 +00006300 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05006301 goto loop;
6302 }
6303
Josef Bacik2552d172009-04-03 10:14:19 -04006304have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006305 cached = block_group_cache_done(block_group);
6306 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05006307 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00006308 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006309 BUG_ON(ret < 0);
6310 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006311 }
6312
Josef Bacik36cce922013-08-05 11:15:21 -04006313 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
6314 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05006315 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006316 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006317
Josef Bacik0a243252009-09-11 16:11:20 -04006318 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006319 * Ok we want to try and use the cluster allocator, so
6320 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006321 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006322 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05006323 unsigned long aligned_cluster;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006324 /*
6325 * the refill lock keeps out other
6326 * people trying to start a new cluster
6327 */
6328 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006329 used_block_group = last_ptr->block_group;
6330 if (used_block_group != block_group &&
6331 (!used_block_group ||
6332 used_block_group->ro ||
David Sterbab6919a52013-04-29 13:39:40 +00006333 !block_group_bits(used_block_group, flags))) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006334 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006335 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006336 }
Chris Mason44fb5512009-06-04 15:34:51 -04006337
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006338 if (used_block_group != block_group)
6339 btrfs_get_block_group(used_block_group);
6340
6341 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08006342 last_ptr,
6343 num_bytes,
6344 used_block_group->key.objectid,
6345 &max_extent_size);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006346 if (offset) {
6347 /* we have a block, we're done */
6348 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006349 trace_btrfs_reserve_extent_cluster(root,
6350 block_group, search_start, num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006351 goto checks;
6352 }
6353
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006354 WARN_ON(last_ptr->block_group != used_block_group);
6355 if (used_block_group != block_group) {
6356 btrfs_put_block_group(used_block_group);
6357 used_block_group = block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006358 }
Chris Mason44fb5512009-06-04 15:34:51 -04006359refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006360 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006361 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6362 * set up a new clusters, so lets just skip it
6363 * and let the allocator find whatever block
6364 * it can find. If we reach this point, we
6365 * will have tried the cluster allocator
6366 * plenty of times and not have found
6367 * anything, so we are likely way too
6368 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006369 * anything.
6370 *
6371 * However, if the cluster is taken from the
6372 * current block group, release the cluster
6373 * first, so that we stand a better chance of
6374 * succeeding in the unclustered
6375 * allocation. */
6376 if (loop >= LOOP_NO_EMPTY_SIZE &&
6377 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006378 spin_unlock(&last_ptr->refill_lock);
6379 goto unclustered_alloc;
6380 }
6381
Chris Masonfa9c0d72009-04-03 09:47:43 -04006382 /*
6383 * this cluster didn't work out, free it and
6384 * start over
6385 */
6386 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6387
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006388 if (loop >= LOOP_NO_EMPTY_SIZE) {
6389 spin_unlock(&last_ptr->refill_lock);
6390 goto unclustered_alloc;
6391 }
6392
Chris Mason8de972b2013-01-04 15:39:43 -05006393 aligned_cluster = max_t(unsigned long,
6394 empty_cluster + empty_size,
6395 block_group->full_stripe_len);
6396
Chris Masonfa9c0d72009-04-03 09:47:43 -04006397 /* allocate a cluster in this block group */
Josef Bacik00361582013-08-14 14:02:47 -04006398 ret = btrfs_find_space_cluster(root, block_group,
6399 last_ptr, search_start,
6400 num_bytes,
6401 aligned_cluster);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006402 if (ret == 0) {
6403 /*
6404 * now pull our allocation out of this
6405 * cluster
6406 */
6407 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08006408 last_ptr,
6409 num_bytes,
6410 search_start,
6411 &max_extent_size);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006412 if (offset) {
6413 /* we found one, proceed */
6414 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006415 trace_btrfs_reserve_extent_cluster(root,
6416 block_group, search_start,
6417 num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006418 goto checks;
6419 }
Josef Bacik0a243252009-09-11 16:11:20 -04006420 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6421 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006422 spin_unlock(&last_ptr->refill_lock);
6423
Josef Bacik0a243252009-09-11 16:11:20 -04006424 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006425 wait_block_group_cache_progress(block_group,
6426 num_bytes + empty_cluster + empty_size);
6427 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006428 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006429
Chris Masonfa9c0d72009-04-03 09:47:43 -04006430 /*
6431 * at this point we either didn't find a cluster
6432 * or we weren't able to allocate a block from our
6433 * cluster. Free the cluster we've been trying
6434 * to use, and go to the next block group
6435 */
Josef Bacik0a243252009-09-11 16:11:20 -04006436 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006437 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006438 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006439 }
6440
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006441unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006442 spin_lock(&block_group->free_space_ctl->tree_lock);
6443 if (cached &&
6444 block_group->free_space_ctl->free_space <
6445 num_bytes + empty_cluster + empty_size) {
Miao Xiea4820392013-09-09 13:19:42 +08006446 if (block_group->free_space_ctl->free_space >
6447 max_extent_size)
6448 max_extent_size =
6449 block_group->free_space_ctl->free_space;
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006450 spin_unlock(&block_group->free_space_ctl->tree_lock);
6451 goto loop;
6452 }
6453 spin_unlock(&block_group->free_space_ctl->tree_lock);
6454
Josef Bacik6226cb02009-04-03 10:14:18 -04006455 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08006456 num_bytes, empty_size,
6457 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006458 /*
6459 * If we didn't find a chunk, and we haven't failed on this
6460 * block group before, and this block group is in the middle of
6461 * caching and we are ok with waiting, then go ahead and wait
6462 * for progress to be made, and set failed_alloc to true.
6463 *
6464 * If failed_alloc is true then we've already waited on this
6465 * block group once and should move on to the next block group.
6466 */
6467 if (!offset && !failed_alloc && !cached &&
6468 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006469 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006470 num_bytes + empty_size);
6471 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006472 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006473 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006474 if (!cached)
6475 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006476 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006477 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006478checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006479 search_start = stripe_align(root, used_block_group,
6480 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006481
Josef Bacik2552d172009-04-03 10:14:19 -04006482 /* move on to the next group */
6483 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006484 used_block_group->key.objectid + used_block_group->key.offset) {
6485 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006486 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04006487 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006488
Josef Bacik6226cb02009-04-03 10:14:18 -04006489 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006490 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04006491 search_start - offset);
6492 BUG_ON(offset > search_start);
6493
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006494 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006495 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006496 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006497 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006498 goto loop;
6499 }
Yan Zheng11833d62009-09-11 16:11:19 -04006500
Josef Bacik2552d172009-04-03 10:14:19 -04006501 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006502 ins->objectid = search_start;
6503 ins->offset = num_bytes;
6504
Josef Bacik3f7de032011-11-10 08:29:20 -05006505 trace_btrfs_reserve_extent(orig_root, block_group,
6506 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006507 if (used_block_group != block_group)
6508 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006509 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006510 break;
6511loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006512 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006513 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006514 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006515 if (used_block_group != block_group)
6516 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006517 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006518 }
6519 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006520
Miao Xie60d2adb2011-09-09 17:34:35 +08006521 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6522 goto search;
6523
Yan, Zhengb742bb82010-05-16 10:46:24 -04006524 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6525 goto search;
6526
Josef Bacik285ff5a2012-01-13 15:27:45 -05006527 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006528 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6529 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006530 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6531 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6532 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6533 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04006534 */
Josef Bacik723bda22011-05-27 16:11:38 -04006535 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006536 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006537 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006538 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04006539 struct btrfs_trans_handle *trans;
6540
6541 trans = btrfs_join_transaction(root);
6542 if (IS_ERR(trans)) {
6543 ret = PTR_ERR(trans);
6544 goto out;
6545 }
6546
David Sterbab6919a52013-04-29 13:39:40 +00006547 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04006548 CHUNK_ALLOC_FORCE);
6549 /*
6550 * Do not bail out on ENOSPC since we
6551 * can do more things.
6552 */
Josef Bacik00361582013-08-14 14:02:47 -04006553 if (ret < 0 && ret != -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04006554 btrfs_abort_transaction(trans,
6555 root, ret);
Josef Bacik00361582013-08-14 14:02:47 -04006556 else
6557 ret = 0;
6558 btrfs_end_transaction(trans, root);
6559 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04006560 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006561 }
6562
6563 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006564 empty_size = 0;
6565 empty_cluster = 0;
6566 }
Chris Mason42e70e72008-11-07 18:17:11 -05006567
Josef Bacik723bda22011-05-27 16:11:38 -04006568 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006569 } else if (!ins->objectid) {
6570 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006571 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006572 ret = 0;
6573 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006574out:
Miao Xiea4820392013-09-09 13:19:42 +08006575 if (ret == -ENOSPC)
6576 ins->offset = max_extent_size;
Chris Mason0f70abe2007-02-28 16:46:22 -05006577 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006578}
Chris Masonec44a352008-04-28 15:29:52 -04006579
Josef Bacik9ed74f22009-09-11 16:12:44 -04006580static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6581 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006582{
6583 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006584 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006585
Josef Bacik9ed74f22009-09-11 16:12:44 -04006586 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006587 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006588 info->flags,
6589 info->total_bytes - info->bytes_used - info->bytes_pinned -
6590 info->bytes_reserved - info->bytes_readonly,
Chris Masond3977122009-01-05 21:25:51 -05006591 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006592 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6593 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006594 info->total_bytes, info->bytes_used, info->bytes_pinned,
6595 info->bytes_reserved, info->bytes_may_use,
6596 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006597 spin_unlock(&info->lock);
6598
6599 if (!dump_block_groups)
6600 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006601
Josef Bacik80eb2342008-10-29 14:49:05 -04006602 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006603again:
6604 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006605 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006606 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006607 cache->key.objectid, cache->key.offset,
6608 btrfs_block_group_used(&cache->item), cache->pinned,
6609 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006610 btrfs_dump_free_space(cache, bytes);
6611 spin_unlock(&cache->lock);
6612 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006613 if (++index < BTRFS_NR_RAID_TYPES)
6614 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006615 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006616}
Zheng Yane8569812008-09-26 10:05:48 -04006617
Josef Bacik00361582013-08-14 14:02:47 -04006618int btrfs_reserve_extent(struct btrfs_root *root,
Yan Zheng11833d62009-09-11 16:11:19 -04006619 u64 num_bytes, u64 min_alloc_size,
6620 u64 empty_size, u64 hint_byte,
David Sterbab6919a52013-04-29 13:39:40 +00006621 struct btrfs_key *ins, int is_data)
Chris Masonfec577f2007-02-26 10:40:21 -05006622{
Miao Xie9e622d62012-01-26 15:01:12 -05006623 bool final_tried = false;
David Sterbab6919a52013-04-29 13:39:40 +00006624 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05006625 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006626
David Sterbab6919a52013-04-29 13:39:40 +00006627 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04006628again:
Chris Masondb945352007-10-15 16:15:53 -04006629 WARN_ON(num_bytes < root->sectorsize);
Josef Bacik00361582013-08-14 14:02:47 -04006630 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
6631 flags);
Chris Mason3b951512008-04-17 11:29:12 -04006632
Miao Xie9e622d62012-01-26 15:01:12 -05006633 if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08006634 if (!final_tried && ins->offset) {
6635 num_bytes = min(num_bytes >> 1, ins->offset);
Zach Brown24542bf2012-11-16 00:04:43 +00006636 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006637 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006638 if (num_bytes == min_alloc_size)
6639 final_tried = true;
6640 goto again;
6641 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6642 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006643
David Sterbab6919a52013-04-29 13:39:40 +00006644 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006645 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006646 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006647 if (sinfo)
6648 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006649 }
Chris Mason925baed2008-06-25 16:01:30 -04006650 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006651
6652 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006653}
6654
Chris Masone688b722011-10-31 20:52:39 -04006655static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6656 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006657{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006658 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006659 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006660
Josef Bacik0f9dd462008-09-23 13:14:11 -04006661 cache = btrfs_lookup_block_group(root->fs_info, start);
6662 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006663 btrfs_err(root->fs_info, "Unable to find block group for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006664 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006665 return -ENOSPC;
6666 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006667
Li Dongyang5378e602011-03-24 10:24:27 +00006668 if (btrfs_test_opt(root, DISCARD))
6669 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006670
Chris Masone688b722011-10-31 20:52:39 -04006671 if (pin)
6672 pin_down_extent(root, cache, start, len, 1);
6673 else {
6674 btrfs_add_free_space(cache, start, len);
6675 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6676 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006677 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006678
liubo1abe9b82011-03-24 11:18:59 +00006679 trace_btrfs_reserved_extent_free(root, start, len);
6680
Chris Masone6dcd2d2008-07-17 12:53:50 -04006681 return ret;
6682}
6683
Chris Masone688b722011-10-31 20:52:39 -04006684int btrfs_free_reserved_extent(struct btrfs_root *root,
6685 u64 start, u64 len)
6686{
6687 return __btrfs_free_reserved_extent(root, start, len, 0);
6688}
6689
6690int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6691 u64 start, u64 len)
6692{
6693 return __btrfs_free_reserved_extent(root, start, len, 1);
6694}
6695
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006696static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6697 struct btrfs_root *root,
6698 u64 parent, u64 root_objectid,
6699 u64 flags, u64 owner, u64 offset,
6700 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006701{
6702 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006703 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006704 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006705 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006706 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006707 struct extent_buffer *leaf;
6708 int type;
6709 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006710
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006711 if (parent > 0)
6712 type = BTRFS_SHARED_DATA_REF_KEY;
6713 else
6714 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006715
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006716 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006717
6718 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006719 if (!path)
6720 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006721
Chris Masonb9473432009-03-13 11:00:37 -04006722 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006723 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6724 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006725 if (ret) {
6726 btrfs_free_path(path);
6727 return ret;
6728 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006729
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006730 leaf = path->nodes[0];
6731 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006732 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006733 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6734 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6735 btrfs_set_extent_flags(leaf, extent_item,
6736 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006737
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006738 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6739 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6740 if (parent > 0) {
6741 struct btrfs_shared_data_ref *ref;
6742 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6743 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6744 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6745 } else {
6746 struct btrfs_extent_data_ref *ref;
6747 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6748 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6749 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6750 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6751 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6752 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006753
6754 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006755 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006756
Liu Boc53d6132012-12-27 09:01:19 +00006757 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006758 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006759 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006760 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006761 BUG();
6762 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04006763 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006764 return ret;
6765}
6766
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006767static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6768 struct btrfs_root *root,
6769 u64 parent, u64 root_objectid,
6770 u64 flags, struct btrfs_disk_key *key,
6771 int level, struct btrfs_key *ins)
6772{
6773 int ret;
6774 struct btrfs_fs_info *fs_info = root->fs_info;
6775 struct btrfs_extent_item *extent_item;
6776 struct btrfs_tree_block_info *block_info;
6777 struct btrfs_extent_inline_ref *iref;
6778 struct btrfs_path *path;
6779 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006780 u32 size = sizeof(*extent_item) + sizeof(*iref);
6781 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6782 SKINNY_METADATA);
6783
6784 if (!skinny_metadata)
6785 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006786
6787 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04006788 if (!path) {
6789 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
6790 root->leafsize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07006791 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04006792 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006793
6794 path->leave_spinning = 1;
6795 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6796 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006797 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04006798 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
6799 root->leafsize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006800 btrfs_free_path(path);
6801 return ret;
6802 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006803
6804 leaf = path->nodes[0];
6805 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6806 struct btrfs_extent_item);
6807 btrfs_set_extent_refs(leaf, extent_item, 1);
6808 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6809 btrfs_set_extent_flags(leaf, extent_item,
6810 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006811
Josef Bacik3173a182013-03-07 14:22:04 -05006812 if (skinny_metadata) {
6813 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6814 } else {
6815 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6816 btrfs_set_tree_block_key(leaf, block_info, key);
6817 btrfs_set_tree_block_level(leaf, block_info, level);
6818 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6819 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006820
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006821 if (parent > 0) {
6822 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6823 btrfs_set_extent_inline_ref_type(leaf, iref,
6824 BTRFS_SHARED_BLOCK_REF_KEY);
6825 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6826 } else {
6827 btrfs_set_extent_inline_ref_type(leaf, iref,
6828 BTRFS_TREE_BLOCK_REF_KEY);
6829 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6830 }
6831
6832 btrfs_mark_buffer_dirty(leaf);
6833 btrfs_free_path(path);
6834
Josef Bacik3173a182013-03-07 14:22:04 -05006835 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006836 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006837 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006838 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006839 BUG();
6840 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04006841
6842 trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->leafsize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006843 return ret;
6844}
6845
6846int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6847 struct btrfs_root *root,
6848 u64 root_objectid, u64 owner,
6849 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006850{
6851 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006852
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006853 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006854
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006855 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6856 ins->offset, 0,
6857 root_objectid, owner, offset,
6858 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006859 return ret;
6860}
Chris Masone02119d2008-09-05 16:13:11 -04006861
6862/*
6863 * this is used by the tree logging recovery code. It records that
6864 * an extent has been allocated and makes sure to clear the free
6865 * space cache bits as well
6866 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006867int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6868 struct btrfs_root *root,
6869 u64 root_objectid, u64 owner, u64 offset,
6870 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006871{
6872 int ret;
6873 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006874
6875 /*
6876 * Mixed block groups will exclude before processing the log so we only
6877 * need to do the exlude dance if this fs isn't mixed.
6878 */
6879 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
6880 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
6881 if (ret)
6882 return ret;
6883 }
Chris Masone02119d2008-09-05 16:13:11 -04006884
Chris Masone02119d2008-09-05 16:13:11 -04006885 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006886 if (!block_group)
6887 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04006888
Josef Bacikfb25e912011-07-26 17:00:46 -04006889 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6890 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006891 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006892 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6893 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006894 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006895 return ret;
6896}
6897
Eric Sandeen48a3b632013-04-25 20:41:01 +00006898static struct extent_buffer *
6899btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6900 u64 bytenr, u32 blocksize, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006901{
6902 struct extent_buffer *buf;
6903
6904 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6905 if (!buf)
6906 return ERR_PTR(-ENOMEM);
6907 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006908 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006909 btrfs_tree_lock(buf);
6910 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006911 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006912
6913 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006914 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006915
Chris Masond0c803c2008-09-11 16:17:57 -04006916 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006917 /*
6918 * we allow two log transactions at a time, use different
6919 * EXENT bit to differentiate dirty pages.
6920 */
6921 if (root->log_transid % 2 == 0)
6922 set_extent_dirty(&root->dirty_log_pages, buf->start,
6923 buf->start + buf->len - 1, GFP_NOFS);
6924 else
6925 set_extent_new(&root->dirty_log_pages, buf->start,
6926 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006927 } else {
6928 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6929 buf->start + buf->len - 1, GFP_NOFS);
6930 }
Chris Mason65b51a02008-08-01 15:11:20 -04006931 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006932 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006933 return buf;
6934}
6935
Yan, Zhengf0486c62010-05-16 10:46:25 -04006936static struct btrfs_block_rsv *
6937use_block_rsv(struct btrfs_trans_handle *trans,
6938 struct btrfs_root *root, u32 blocksize)
6939{
6940 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006941 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006942 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00006943 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006944
6945 block_rsv = get_block_rsv(trans, root);
6946
Miao Xieb586b322013-05-13 13:55:10 +00006947 if (unlikely(block_rsv->size == 0))
6948 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00006949again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04006950 ret = block_rsv_use_bytes(block_rsv, blocksize);
6951 if (!ret)
6952 return block_rsv;
6953
Miao Xieb586b322013-05-13 13:55:10 +00006954 if (block_rsv->failfast)
6955 return ERR_PTR(ret);
6956
Miao Xied88033d2013-05-13 13:55:12 +00006957 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
6958 global_updated = true;
6959 update_global_block_rsv(root->fs_info);
6960 goto again;
6961 }
6962
Miao Xieb586b322013-05-13 13:55:10 +00006963 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6964 static DEFINE_RATELIMIT_STATE(_rs,
6965 DEFAULT_RATELIMIT_INTERVAL * 10,
6966 /*DEFAULT_RATELIMIT_BURST*/ 1);
6967 if (__ratelimit(&_rs))
6968 WARN(1, KERN_DEBUG
6969 "btrfs: block rsv returned %d\n", ret);
6970 }
6971try_reserve:
6972 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6973 BTRFS_RESERVE_NO_FLUSH);
6974 if (!ret)
6975 return block_rsv;
6976 /*
6977 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00006978 * the global reserve if its space type is the same as the global
6979 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00006980 */
Miao Xie5881cfc2013-05-13 13:55:11 +00006981 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
6982 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00006983 ret = block_rsv_use_bytes(global_rsv, blocksize);
6984 if (!ret)
6985 return global_rsv;
6986 }
6987 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006988}
6989
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006990static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6991 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006992{
6993 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006994 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006995}
6996
Chris Masonfec577f2007-02-26 10:40:21 -05006997/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006998 * finds a free extent and does all the dirty work required for allocation
6999 * returns the key for the extent through ins, and a tree buffer for
7000 * the first block of the extent through buf.
7001 *
Chris Masonfec577f2007-02-26 10:40:21 -05007002 * returns the tree buffer or NULL.
7003 */
Chris Mason5f39d392007-10-15 16:14:19 -04007004struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007005 struct btrfs_root *root, u32 blocksize,
7006 u64 parent, u64 root_objectid,
7007 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02007008 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05007009{
Chris Masone2fa7222007-03-12 16:22:34 -04007010 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007011 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04007012 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007013 u64 flags = 0;
7014 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05007015 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7016 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007017
7018 block_rsv = use_block_rsv(trans, root, blocksize);
7019 if (IS_ERR(block_rsv))
7020 return ERR_CAST(block_rsv);
7021
Josef Bacik00361582013-08-14 14:02:47 -04007022 ret = btrfs_reserve_extent(root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007023 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05007024 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007025 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007026 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05007027 }
Chris Mason55c69072008-01-09 15:55:33 -05007028
Chris Mason4008c042009-02-12 14:09:45 -05007029 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
7030 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007031 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007032
7033 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
7034 if (parent == 0)
7035 parent = ins.objectid;
7036 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7037 } else
7038 BUG_ON(parent > 0);
7039
7040 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
7041 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00007042 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007043 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007044 if (key)
7045 memcpy(&extent_op->key, key, sizeof(extent_op->key));
7046 else
7047 memset(&extent_op->key, 0, sizeof(extent_op->key));
7048 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05007049 if (skinny_metadata)
7050 extent_op->update_key = 0;
7051 else
7052 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007053 extent_op->update_flags = 1;
7054 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04007055 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007056
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007057 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
7058 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007059 ins.offset, parent, root_objectid,
7060 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02007061 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007062 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007063 }
Chris Masonfec577f2007-02-26 10:40:21 -05007064 return buf;
7065}
Chris Masona28ec192007-03-06 20:08:01 -05007066
Yan Zheng2c47e6052009-06-27 21:07:35 -04007067struct walk_control {
7068 u64 refs[BTRFS_MAX_LEVEL];
7069 u64 flags[BTRFS_MAX_LEVEL];
7070 struct btrfs_key update_progress;
7071 int stage;
7072 int level;
7073 int shared_level;
7074 int update_ref;
7075 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007076 int reada_slot;
7077 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007078 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007079};
7080
7081#define DROP_REFERENCE 1
7082#define UPDATE_BACKREF 2
7083
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007084static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
7085 struct btrfs_root *root,
7086 struct walk_control *wc,
7087 struct btrfs_path *path)
7088{
7089 u64 bytenr;
7090 u64 generation;
7091 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007092 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007093 u32 nritems;
7094 u32 blocksize;
7095 struct btrfs_key key;
7096 struct extent_buffer *eb;
7097 int ret;
7098 int slot;
7099 int nread = 0;
7100
7101 if (path->slots[wc->level] < wc->reada_slot) {
7102 wc->reada_count = wc->reada_count * 2 / 3;
7103 wc->reada_count = max(wc->reada_count, 2);
7104 } else {
7105 wc->reada_count = wc->reada_count * 3 / 2;
7106 wc->reada_count = min_t(int, wc->reada_count,
7107 BTRFS_NODEPTRS_PER_BLOCK(root));
7108 }
7109
7110 eb = path->nodes[wc->level];
7111 nritems = btrfs_header_nritems(eb);
7112 blocksize = btrfs_level_size(root, wc->level - 1);
7113
7114 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7115 if (nread >= wc->reada_count)
7116 break;
7117
7118 cond_resched();
7119 bytenr = btrfs_node_blockptr(eb, slot);
7120 generation = btrfs_node_ptr_generation(eb, slot);
7121
7122 if (slot == path->slots[wc->level])
7123 goto reada;
7124
7125 if (wc->stage == UPDATE_BACKREF &&
7126 generation <= root->root_key.offset)
7127 continue;
7128
Yan, Zheng94fcca92009-10-09 09:25:16 -04007129 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05007130 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7131 wc->level - 1, 1, &refs,
7132 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007133 /* We don't care about errors in readahead. */
7134 if (ret < 0)
7135 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007136 BUG_ON(refs == 0);
7137
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007138 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007139 if (refs == 1)
7140 goto reada;
7141
Yan, Zheng94fcca92009-10-09 09:25:16 -04007142 if (wc->level == 1 &&
7143 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7144 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007145 if (!wc->update_ref ||
7146 generation <= root->root_key.offset)
7147 continue;
7148 btrfs_node_key_to_cpu(eb, &key, slot);
7149 ret = btrfs_comp_cpu_keys(&key,
7150 &wc->update_progress);
7151 if (ret < 0)
7152 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007153 } else {
7154 if (wc->level == 1 &&
7155 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7156 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007157 }
7158reada:
7159 ret = readahead_tree_block(root, bytenr, blocksize,
7160 generation);
7161 if (ret)
7162 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007163 nread++;
7164 }
7165 wc->reada_slot = slot;
7166}
7167
Chris Mason9aca1d52007-03-13 11:09:37 -04007168/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007169 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007170 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04007171 * when wc->stage == UPDATE_BACKREF, this function updates
7172 * back refs for pointers in the block.
7173 *
7174 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04007175 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007176static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7177 struct btrfs_root *root,
7178 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007179 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04007180{
7181 int level = wc->level;
7182 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04007183 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7184 int ret;
7185
7186 if (wc->stage == UPDATE_BACKREF &&
7187 btrfs_header_owner(eb) != root->root_key.objectid)
7188 return 1;
7189
7190 /*
7191 * when reference count of tree block is 1, it won't increase
7192 * again. once full backref flag is set, we never clear it.
7193 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04007194 if (lookup_info &&
7195 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7196 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007197 BUG_ON(!path->locks[level]);
7198 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007199 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007200 &wc->refs[level],
7201 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007202 BUG_ON(ret == -ENOMEM);
7203 if (ret)
7204 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007205 BUG_ON(wc->refs[level] == 0);
7206 }
7207
Yan Zheng2c47e6052009-06-27 21:07:35 -04007208 if (wc->stage == DROP_REFERENCE) {
7209 if (wc->refs[level] > 1)
7210 return 1;
7211
7212 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04007213 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007214 path->locks[level] = 0;
7215 }
7216 return 0;
7217 }
7218
7219 /* wc->stage == UPDATE_BACKREF */
7220 if (!(wc->flags[level] & flag)) {
7221 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007222 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007223 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007224 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007225 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007226 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04007227 eb->len, flag,
7228 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007229 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007230 wc->flags[level] |= flag;
7231 }
7232
7233 /*
7234 * the block is shared by multiple trees, so it's not good to
7235 * keep the tree lock
7236 */
7237 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04007238 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007239 path->locks[level] = 0;
7240 }
7241 return 0;
7242}
7243
7244/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007245 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007246 *
7247 * when wc->stage == DROP_REFERENCE, this function checks
7248 * reference count of the block pointed to. if the block
7249 * is shared and we need update back refs for the subtree
7250 * rooted at the block, this function changes wc->stage to
7251 * UPDATE_BACKREF. if the block is shared and there is no
7252 * need to update back, this function drops the reference
7253 * to the block.
7254 *
7255 * NOTE: return value 1 means we should stop walking down.
7256 */
7257static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7258 struct btrfs_root *root,
7259 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007260 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007261{
7262 u64 bytenr;
7263 u64 generation;
7264 u64 parent;
7265 u32 blocksize;
7266 struct btrfs_key key;
7267 struct extent_buffer *next;
7268 int level = wc->level;
7269 int reada = 0;
7270 int ret = 0;
7271
7272 generation = btrfs_node_ptr_generation(path->nodes[level],
7273 path->slots[level]);
7274 /*
7275 * if the lower level block was created before the snapshot
7276 * was created, we know there is no need to update back refs
7277 * for the subtree
7278 */
7279 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007280 generation <= root->root_key.offset) {
7281 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007282 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007283 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007284
7285 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7286 blocksize = btrfs_level_size(root, level - 1);
7287
7288 next = btrfs_find_tree_block(root, bytenr, blocksize);
7289 if (!next) {
7290 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00007291 if (!next)
7292 return -ENOMEM;
Josef Bacikb2aaaa32013-07-05 17:05:38 -04007293 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
7294 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007295 reada = 1;
7296 }
7297 btrfs_tree_lock(next);
7298 btrfs_set_lock_blocking(next);
7299
Josef Bacik3173a182013-03-07 14:22:04 -05007300 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007301 &wc->refs[level - 1],
7302 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007303 if (ret < 0) {
7304 btrfs_tree_unlock(next);
7305 return ret;
7306 }
7307
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007308 if (unlikely(wc->refs[level - 1] == 0)) {
7309 btrfs_err(root->fs_info, "Missing references.");
7310 BUG();
7311 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007312 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007313
Yan, Zheng94fcca92009-10-09 09:25:16 -04007314 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007315 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007316 if (level == 1 &&
7317 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7318 goto skip;
7319
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007320 if (!wc->update_ref ||
7321 generation <= root->root_key.offset)
7322 goto skip;
7323
7324 btrfs_node_key_to_cpu(path->nodes[level], &key,
7325 path->slots[level]);
7326 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7327 if (ret < 0)
7328 goto skip;
7329
7330 wc->stage = UPDATE_BACKREF;
7331 wc->shared_level = level - 1;
7332 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007333 } else {
7334 if (level == 1 &&
7335 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7336 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007337 }
7338
Chris Masonb9fab912012-05-06 07:23:47 -04007339 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007340 btrfs_tree_unlock(next);
7341 free_extent_buffer(next);
7342 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007343 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007344 }
7345
7346 if (!next) {
7347 if (reada && level == 1)
7348 reada_walk_down(trans, root, wc, path);
7349 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007350 if (!next || !extent_buffer_uptodate(next)) {
7351 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007352 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007353 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007354 btrfs_tree_lock(next);
7355 btrfs_set_lock_blocking(next);
7356 }
7357
7358 level--;
7359 BUG_ON(level != btrfs_header_level(next));
7360 path->nodes[level] = next;
7361 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007362 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007363 wc->level = level;
7364 if (wc->level == 1)
7365 wc->reada_slot = 0;
7366 return 0;
7367skip:
7368 wc->refs[level - 1] = 0;
7369 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007370 if (wc->stage == DROP_REFERENCE) {
7371 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7372 parent = path->nodes[level]->start;
7373 } else {
7374 BUG_ON(root->root_key.objectid !=
7375 btrfs_header_owner(path->nodes[level]));
7376 parent = 0;
7377 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007378
Yan, Zheng94fcca92009-10-09 09:25:16 -04007379 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007380 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007381 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007382 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007383 btrfs_tree_unlock(next);
7384 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007385 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007386 return 1;
7387}
7388
7389/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007390 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007391 *
7392 * when wc->stage == DROP_REFERENCE, this function drops
7393 * reference count on the block.
7394 *
7395 * when wc->stage == UPDATE_BACKREF, this function changes
7396 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7397 * to UPDATE_BACKREF previously while processing the block.
7398 *
7399 * NOTE: return value 1 means we should stop walking up.
7400 */
7401static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7402 struct btrfs_root *root,
7403 struct btrfs_path *path,
7404 struct walk_control *wc)
7405{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007406 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007407 int level = wc->level;
7408 struct extent_buffer *eb = path->nodes[level];
7409 u64 parent = 0;
7410
7411 if (wc->stage == UPDATE_BACKREF) {
7412 BUG_ON(wc->shared_level < level);
7413 if (level < wc->shared_level)
7414 goto out;
7415
Yan Zheng2c47e6052009-06-27 21:07:35 -04007416 ret = find_next_key(path, level + 1, &wc->update_progress);
7417 if (ret > 0)
7418 wc->update_ref = 0;
7419
7420 wc->stage = DROP_REFERENCE;
7421 wc->shared_level = -1;
7422 path->slots[level] = 0;
7423
7424 /*
7425 * check reference count again if the block isn't locked.
7426 * we should start walking down the tree again if reference
7427 * count is one.
7428 */
7429 if (!path->locks[level]) {
7430 BUG_ON(level == 0);
7431 btrfs_tree_lock(eb);
7432 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007433 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007434
7435 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007436 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007437 &wc->refs[level],
7438 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007439 if (ret < 0) {
7440 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007441 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007442 return ret;
7443 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007444 BUG_ON(wc->refs[level] == 0);
7445 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007446 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007447 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007448 return 1;
7449 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007450 }
7451 }
7452
7453 /* wc->stage == DROP_REFERENCE */
7454 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7455
7456 if (wc->refs[level] == 1) {
7457 if (level == 0) {
7458 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007459 ret = btrfs_dec_ref(trans, root, eb, 1,
7460 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007461 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007462 ret = btrfs_dec_ref(trans, root, eb, 0,
7463 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007464 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007465 }
7466 /* make block locked assertion in clean_tree_block happy */
7467 if (!path->locks[level] &&
7468 btrfs_header_generation(eb) == trans->transid) {
7469 btrfs_tree_lock(eb);
7470 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007471 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007472 }
7473 clean_tree_block(trans, root, eb);
7474 }
7475
7476 if (eb == root->node) {
7477 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7478 parent = eb->start;
7479 else
7480 BUG_ON(root->root_key.objectid !=
7481 btrfs_header_owner(eb));
7482 } else {
7483 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7484 parent = path->nodes[level + 1]->start;
7485 else
7486 BUG_ON(root->root_key.objectid !=
7487 btrfs_header_owner(path->nodes[level + 1]));
7488 }
7489
Jan Schmidt5581a512012-05-16 17:04:52 +02007490 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007491out:
7492 wc->refs[level] = 0;
7493 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007494 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007495}
7496
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007497static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7498 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007499 struct btrfs_path *path,
7500 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007501{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007502 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007503 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007504 int ret;
7505
Yan Zheng2c47e6052009-06-27 21:07:35 -04007506 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007507 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007508 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007509 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007510
Yan Zheng2c47e6052009-06-27 21:07:35 -04007511 if (level == 0)
7512 break;
7513
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007514 if (path->slots[level] >=
7515 btrfs_header_nritems(path->nodes[level]))
7516 break;
7517
Yan, Zheng94fcca92009-10-09 09:25:16 -04007518 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007519 if (ret > 0) {
7520 path->slots[level]++;
7521 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00007522 } else if (ret < 0)
7523 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007524 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007525 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007526 return 0;
7527}
7528
Chris Masond3977122009-01-05 21:25:51 -05007529static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007530 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007531 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007532 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007533{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007534 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007535 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007536
Yan Zheng2c47e6052009-06-27 21:07:35 -04007537 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7538 while (level < max_level && path->nodes[level]) {
7539 wc->level = level;
7540 if (path->slots[level] + 1 <
7541 btrfs_header_nritems(path->nodes[level])) {
7542 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007543 return 0;
7544 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007545 ret = walk_up_proc(trans, root, path, wc);
7546 if (ret > 0)
7547 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007548
Yan Zheng2c47e6052009-06-27 21:07:35 -04007549 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007550 btrfs_tree_unlock_rw(path->nodes[level],
7551 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007552 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007553 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007554 free_extent_buffer(path->nodes[level]);
7555 path->nodes[level] = NULL;
7556 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007557 }
7558 }
7559 return 1;
7560}
7561
Chris Mason9aca1d52007-03-13 11:09:37 -04007562/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007563 * drop a subvolume tree.
7564 *
7565 * this function traverses the tree freeing any blocks that only
7566 * referenced by the tree.
7567 *
7568 * when a shared tree block is found. this function decreases its
7569 * reference count by one. if update_ref is true, this function
7570 * also make sure backrefs for the shared block and all lower level
7571 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007572 *
7573 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007574 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007575int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007576 struct btrfs_block_rsv *block_rsv, int update_ref,
7577 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007578{
Chris Mason5caf2a02007-04-02 11:20:42 -04007579 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007580 struct btrfs_trans_handle *trans;
7581 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007582 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007583 struct walk_control *wc;
7584 struct btrfs_key key;
7585 int err = 0;
7586 int ret;
7587 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04007588 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05007589
Chris Mason5caf2a02007-04-02 11:20:42 -04007590 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007591 if (!path) {
7592 err = -ENOMEM;
7593 goto out;
7594 }
Chris Mason20524f02007-03-10 06:35:47 -05007595
Yan Zheng2c47e6052009-06-27 21:07:35 -04007596 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007597 if (!wc) {
7598 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007599 err = -ENOMEM;
7600 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007601 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007602
Yan, Zhenga22285a2010-05-16 10:48:46 -04007603 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007604 if (IS_ERR(trans)) {
7605 err = PTR_ERR(trans);
7606 goto out_free;
7607 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007608
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007609 if (block_rsv)
7610 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007611
Chris Mason9f3a7422007-08-07 15:52:19 -04007612 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007613 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007614 path->nodes[level] = btrfs_lock_root_node(root);
7615 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007616 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007617 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007618 memset(&wc->update_progress, 0,
7619 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007620 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007621 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007622 memcpy(&wc->update_progress, &key,
7623 sizeof(wc->update_progress));
7624
Chris Mason6702ed42007-08-07 16:15:09 -04007625 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007626 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007627 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007628 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7629 path->lowest_level = 0;
7630 if (ret < 0) {
7631 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007632 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007633 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007634 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007635
Chris Mason7d9eb122008-07-08 14:19:17 -04007636 /*
7637 * unlock our path, this is safe because only this
7638 * function is allowed to delete this snapshot
7639 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007640 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007641
Yan Zheng2c47e6052009-06-27 21:07:35 -04007642 level = btrfs_header_level(root->node);
7643 while (1) {
7644 btrfs_tree_lock(path->nodes[level]);
7645 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007646 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007647
7648 ret = btrfs_lookup_extent_info(trans, root,
7649 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007650 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007651 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007652 if (ret < 0) {
7653 err = ret;
7654 goto out_end_trans;
7655 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007656 BUG_ON(wc->refs[level] == 0);
7657
7658 if (level == root_item->drop_level)
7659 break;
7660
7661 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007662 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007663 WARN_ON(wc->refs[level] != 1);
7664 level--;
7665 }
7666 }
7667
7668 wc->level = level;
7669 wc->shared_level = -1;
7670 wc->stage = DROP_REFERENCE;
7671 wc->update_ref = update_ref;
7672 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007673 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007674 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007675
7676 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007677
Yan Zheng2c47e6052009-06-27 21:07:35 -04007678 ret = walk_down_tree(trans, root, path, wc);
7679 if (ret < 0) {
7680 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007681 break;
7682 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007683
7684 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7685 if (ret < 0) {
7686 err = ret;
7687 break;
7688 }
7689
7690 if (ret > 0) {
7691 BUG_ON(wc->stage != DROP_REFERENCE);
7692 break;
7693 }
7694
7695 if (wc->stage == DROP_REFERENCE) {
7696 level = wc->level;
7697 btrfs_node_key(path->nodes[level],
7698 &root_item->drop_progress,
7699 path->slots[level]);
7700 root_item->drop_level = level;
7701 }
7702
7703 BUG_ON(wc->level == 0);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007704 if (btrfs_should_end_transaction(trans, tree_root) ||
7705 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007706 ret = btrfs_update_root(trans, tree_root,
7707 &root->root_key,
7708 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007709 if (ret) {
7710 btrfs_abort_transaction(trans, tree_root, ret);
7711 err = ret;
7712 goto out_end_trans;
7713 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007714
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007715 btrfs_end_transaction_throttle(trans, tree_root);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007716 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
7717 pr_debug("btrfs: drop snapshot early exit\n");
7718 err = -EAGAIN;
7719 goto out_free;
7720 }
7721
Yan, Zhenga22285a2010-05-16 10:48:46 -04007722 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007723 if (IS_ERR(trans)) {
7724 err = PTR_ERR(trans);
7725 goto out_free;
7726 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007727 if (block_rsv)
7728 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007729 }
Chris Mason20524f02007-03-10 06:35:47 -05007730 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007731 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007732 if (err)
7733 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007734
7735 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007736 if (ret) {
7737 btrfs_abort_transaction(trans, tree_root, ret);
7738 goto out_end_trans;
7739 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007740
Yan, Zheng76dda932009-09-21 16:00:26 -04007741 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007742 ret = btrfs_find_root(tree_root, &root->root_key, path,
7743 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007744 if (ret < 0) {
7745 btrfs_abort_transaction(trans, tree_root, ret);
7746 err = ret;
7747 goto out_end_trans;
7748 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007749 /* if we fail to delete the orphan item this time
7750 * around, it'll get picked up the next time.
7751 *
7752 * The most common failure here is just -ENOENT.
7753 */
7754 btrfs_del_orphan_item(trans, tree_root,
7755 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007756 }
7757 }
7758
7759 if (root->in_radix) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007760 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007761 } else {
7762 free_extent_buffer(root->node);
7763 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00007764 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007765 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04007766 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007767out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007768 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007769out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007770 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007771 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007772out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04007773 /*
7774 * So if we need to stop dropping the snapshot for whatever reason we
7775 * need to make sure to add it back to the dead root list so that we
7776 * keep trying to do the work later. This also cleans up roots if we
7777 * don't have it in the radix (like when we recover after a power fail
7778 * or unmount) so we don't leak memory.
7779 */
Josef Bacikb37b39c2013-07-23 16:57:15 -04007780 if (!for_reloc && root_dropped == false)
Josef Bacikd29a9f62013-07-17 19:30:20 -04007781 btrfs_add_dead_root(root);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007782 if (err)
7783 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007784 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007785}
Chris Mason9078a3e2007-04-26 16:46:15 -04007786
Yan Zheng2c47e6052009-06-27 21:07:35 -04007787/*
7788 * drop subtree rooted at tree block 'node'.
7789 *
7790 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007791 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007792 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007793int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7794 struct btrfs_root *root,
7795 struct extent_buffer *node,
7796 struct extent_buffer *parent)
7797{
7798 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007799 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007800 int level;
7801 int parent_level;
7802 int ret = 0;
7803 int wret;
7804
Yan Zheng2c47e6052009-06-27 21:07:35 -04007805 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7806
Yan Zhengf82d02d2008-10-29 14:49:05 -04007807 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007808 if (!path)
7809 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007810
Yan Zheng2c47e6052009-06-27 21:07:35 -04007811 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007812 if (!wc) {
7813 btrfs_free_path(path);
7814 return -ENOMEM;
7815 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007816
Chris Masonb9447ef2009-03-09 11:45:38 -04007817 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007818 parent_level = btrfs_header_level(parent);
7819 extent_buffer_get(parent);
7820 path->nodes[parent_level] = parent;
7821 path->slots[parent_level] = btrfs_header_nritems(parent);
7822
Chris Masonb9447ef2009-03-09 11:45:38 -04007823 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007824 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007825 path->nodes[level] = node;
7826 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007827 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007828
7829 wc->refs[parent_level] = 1;
7830 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7831 wc->level = level;
7832 wc->shared_level = -1;
7833 wc->stage = DROP_REFERENCE;
7834 wc->update_ref = 0;
7835 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007836 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007837 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007838
7839 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007840 wret = walk_down_tree(trans, root, path, wc);
7841 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007842 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007843 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007844 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007845
Yan Zheng2c47e6052009-06-27 21:07:35 -04007846 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007847 if (wret < 0)
7848 ret = wret;
7849 if (wret != 0)
7850 break;
7851 }
7852
Yan Zheng2c47e6052009-06-27 21:07:35 -04007853 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007854 btrfs_free_path(path);
7855 return ret;
7856}
7857
Chris Masonec44a352008-04-28 15:29:52 -04007858static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7859{
7860 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007861 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007862
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007863 /*
7864 * if restripe for this chunk_type is on pick target profile and
7865 * return, otherwise do the usual balance
7866 */
7867 stripped = get_restripe_target(root->fs_info, flags);
7868 if (stripped)
7869 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007870
Chris Masoncd02dca2010-12-13 14:56:23 -05007871 /*
7872 * we add in the count of missing devices because we want
7873 * to make sure that any RAID levels on a degraded FS
7874 * continue to be honored.
7875 */
7876 num_devices = root->fs_info->fs_devices->rw_devices +
7877 root->fs_info->fs_devices->missing_devices;
7878
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007879 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007880 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007881 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7882
Chris Masonec44a352008-04-28 15:29:52 -04007883 if (num_devices == 1) {
7884 stripped |= BTRFS_BLOCK_GROUP_DUP;
7885 stripped = flags & ~stripped;
7886
7887 /* turn raid0 into single device chunks */
7888 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7889 return stripped;
7890
7891 /* turn mirroring into duplication */
7892 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7893 BTRFS_BLOCK_GROUP_RAID10))
7894 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007895 } else {
7896 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007897 if (flags & stripped)
7898 return flags;
7899
7900 stripped |= BTRFS_BLOCK_GROUP_DUP;
7901 stripped = flags & ~stripped;
7902
7903 /* switch duplicated blocks with raid1 */
7904 if (flags & BTRFS_BLOCK_GROUP_DUP)
7905 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7906
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007907 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007908 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007909
Chris Masonec44a352008-04-28 15:29:52 -04007910 return flags;
7911}
7912
Miao Xie199c36e2011-07-15 10:34:36 +00007913static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007914{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007915 struct btrfs_space_info *sinfo = cache->space_info;
7916 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007917 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007918 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007919
Chris Masonc286ac42008-07-22 23:06:41 -04007920
Miao Xie199c36e2011-07-15 10:34:36 +00007921 /*
7922 * We need some metadata space and system metadata space for
7923 * allocating chunks in some corner cases until we force to set
7924 * it to be readonly.
7925 */
7926 if ((sinfo->flags &
7927 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7928 !force)
7929 min_allocable_bytes = 1 * 1024 * 1024;
7930 else
7931 min_allocable_bytes = 0;
7932
Yan, Zhengf0486c62010-05-16 10:46:25 -04007933 spin_lock(&sinfo->lock);
7934 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007935
7936 if (cache->ro) {
7937 ret = 0;
7938 goto out;
7939 }
7940
Yan, Zhengf0486c62010-05-16 10:46:25 -04007941 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7942 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007943
Yan, Zhengf0486c62010-05-16 10:46:25 -04007944 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007945 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7946 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007947 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007948 cache->ro = 1;
7949 ret = 0;
7950 }
WuBo61cfea92011-07-26 03:30:11 +00007951out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007952 spin_unlock(&cache->lock);
7953 spin_unlock(&sinfo->lock);
7954 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007955}
7956
Yan, Zhengf0486c62010-05-16 10:46:25 -04007957int btrfs_set_block_group_ro(struct btrfs_root *root,
7958 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007959
7960{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007961 struct btrfs_trans_handle *trans;
7962 u64 alloc_flags;
7963 int ret;
7964
7965 BUG_ON(cache->ro);
7966
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007967 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007968 if (IS_ERR(trans))
7969 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007970
7971 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007972 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007973 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007974 CHUNK_ALLOC_FORCE);
7975 if (ret < 0)
7976 goto out;
7977 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007978
Miao Xie199c36e2011-07-15 10:34:36 +00007979 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007980 if (!ret)
7981 goto out;
7982 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007983 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007984 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007985 if (ret < 0)
7986 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007987 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007988out:
7989 btrfs_end_transaction(trans, root);
7990 return ret;
7991}
7992
Chris Masonc87f08c2011-02-16 13:57:04 -05007993int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7994 struct btrfs_root *root, u64 type)
7995{
7996 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007997 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007998 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007999}
8000
Miao Xie6d07bce2011-01-05 10:07:31 +00008001/*
8002 * helper to account the unused space of all the readonly block group in the
8003 * list. takes mirrors into account.
8004 */
8005static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8006{
8007 struct btrfs_block_group_cache *block_group;
8008 u64 free_bytes = 0;
8009 int factor;
8010
8011 list_for_each_entry(block_group, groups_list, list) {
8012 spin_lock(&block_group->lock);
8013
8014 if (!block_group->ro) {
8015 spin_unlock(&block_group->lock);
8016 continue;
8017 }
8018
8019 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8020 BTRFS_BLOCK_GROUP_RAID10 |
8021 BTRFS_BLOCK_GROUP_DUP))
8022 factor = 2;
8023 else
8024 factor = 1;
8025
8026 free_bytes += (block_group->key.offset -
8027 btrfs_block_group_used(&block_group->item)) *
8028 factor;
8029
8030 spin_unlock(&block_group->lock);
8031 }
8032
8033 return free_bytes;
8034}
8035
8036/*
8037 * helper to account the unused space of all the readonly block group in the
8038 * space_info. takes mirrors into account.
8039 */
8040u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8041{
8042 int i;
8043 u64 free_bytes = 0;
8044
8045 spin_lock(&sinfo->lock);
8046
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308047 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Miao Xie6d07bce2011-01-05 10:07:31 +00008048 if (!list_empty(&sinfo->block_groups[i]))
8049 free_bytes += __btrfs_get_ro_block_group_free_space(
8050 &sinfo->block_groups[i]);
8051
8052 spin_unlock(&sinfo->lock);
8053
8054 return free_bytes;
8055}
8056
Jeff Mahoney143bede2012-03-01 14:56:26 +01008057void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04008058 struct btrfs_block_group_cache *cache)
8059{
8060 struct btrfs_space_info *sinfo = cache->space_info;
8061 u64 num_bytes;
8062
8063 BUG_ON(!cache->ro);
8064
8065 spin_lock(&sinfo->lock);
8066 spin_lock(&cache->lock);
8067 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8068 cache->bytes_super - btrfs_block_group_used(&cache->item);
8069 sinfo->bytes_readonly -= num_bytes;
8070 cache->ro = 0;
8071 spin_unlock(&cache->lock);
8072 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008073}
8074
Josef Bacikba1bf482009-09-11 16:11:19 -04008075/*
8076 * checks to see if its even possible to relocate this block group.
8077 *
8078 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8079 * ok to go ahead and try.
8080 */
8081int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008082{
Zheng Yan1a40e232008-09-26 10:09:34 -04008083 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008084 struct btrfs_space_info *space_info;
8085 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8086 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04008087 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00008088 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04008089 u64 dev_min = 1;
8090 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008091 u64 target;
liubocdcb7252011-08-03 10:15:25 +00008092 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04008093 int full = 0;
8094 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008095
Josef Bacikba1bf482009-09-11 16:11:19 -04008096 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008097
Josef Bacikba1bf482009-09-11 16:11:19 -04008098 /* odd, couldn't find the block group, leave it alone */
8099 if (!block_group)
8100 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008101
liubocdcb7252011-08-03 10:15:25 +00008102 min_free = btrfs_block_group_used(&block_group->item);
8103
Josef Bacikba1bf482009-09-11 16:11:19 -04008104 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00008105 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04008106 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008107
Josef Bacikba1bf482009-09-11 16:11:19 -04008108 space_info = block_group->space_info;
8109 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008110
Josef Bacikba1bf482009-09-11 16:11:19 -04008111 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008112
Josef Bacikba1bf482009-09-11 16:11:19 -04008113 /*
8114 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008115 * relocate it unless we're able to allocate a new chunk below.
8116 *
8117 * Otherwise, we need to make sure we have room in the space to handle
8118 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008119 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008120 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00008121 (space_info->bytes_used + space_info->bytes_reserved +
8122 space_info->bytes_pinned + space_info->bytes_readonly +
8123 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008124 spin_unlock(&space_info->lock);
8125 goto out;
8126 }
8127 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008128
Josef Bacikba1bf482009-09-11 16:11:19 -04008129 /*
8130 * ok we don't have enough space, but maybe we have free space on our
8131 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008132 * alloc devices and guess if we have enough space. if this block
8133 * group is going to be restriped, run checks against the target
8134 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04008135 */
8136 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05008137
liubocdcb7252011-08-03 10:15:25 +00008138 /*
8139 * index:
8140 * 0: raid10
8141 * 1: raid1
8142 * 2: dup
8143 * 3: raid0
8144 * 4: single
8145 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008146 target = get_restripe_target(root->fs_info, block_group->flags);
8147 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00008148 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008149 } else {
8150 /*
8151 * this is just a balance, so if we were marked as full
8152 * we know there is no space for a new chunk
8153 */
8154 if (full)
8155 goto out;
8156
8157 index = get_block_group_index(block_group);
8158 }
8159
Miao Xiee6ec7162013-01-17 05:38:51 +00008160 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00008161 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04008162 /* Divide by 2 */
8163 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008164 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00008165 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00008166 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04008167 /* Multiply by 2 */
8168 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008169 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00008170 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04008171 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00008172 }
8173
Josef Bacik6df9a952013-06-27 13:22:46 -04008174 /* We need to do this so that we can look at pending chunks */
8175 trans = btrfs_join_transaction(root);
8176 if (IS_ERR(trans)) {
8177 ret = PTR_ERR(trans);
8178 goto out;
8179 }
8180
Josef Bacikba1bf482009-09-11 16:11:19 -04008181 mutex_lock(&root->fs_info->chunk_mutex);
8182 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00008183 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008184
Josef Bacikba1bf482009-09-11 16:11:19 -04008185 /*
8186 * check to make sure we can actually find a chunk with enough
8187 * space to fit our block group in.
8188 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01008189 if (device->total_bytes > device->bytes_used + min_free &&
8190 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04008191 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008192 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008193 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00008194 dev_nr++;
8195
8196 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05008197 break;
liubocdcb7252011-08-03 10:15:25 +00008198
Josef Bacikba1bf482009-09-11 16:11:19 -04008199 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008200 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008201 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008202 mutex_unlock(&root->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04008203 btrfs_end_transaction(trans, root);
Chris Masonedbd8d42007-12-21 16:27:24 -05008204out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008205 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008206 return ret;
8207}
8208
Christoph Hellwigb2950862008-12-02 09:54:17 -05008209static int find_first_block_group(struct btrfs_root *root,
8210 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008211{
Chris Mason925baed2008-06-25 16:01:30 -04008212 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008213 struct btrfs_key found_key;
8214 struct extent_buffer *leaf;
8215 int slot;
8216
8217 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8218 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008219 goto out;
8220
Chris Masond3977122009-01-05 21:25:51 -05008221 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008222 slot = path->slots[0];
8223 leaf = path->nodes[0];
8224 if (slot >= btrfs_header_nritems(leaf)) {
8225 ret = btrfs_next_leaf(root, path);
8226 if (ret == 0)
8227 continue;
8228 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008229 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008230 break;
8231 }
8232 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8233
8234 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008235 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8236 ret = 0;
8237 goto out;
8238 }
Chris Mason0b86a832008-03-24 15:01:56 -04008239 path->slots[0]++;
8240 }
Chris Mason925baed2008-06-25 16:01:30 -04008241out:
Chris Mason0b86a832008-03-24 15:01:56 -04008242 return ret;
8243}
8244
Josef Bacik0af3d002010-06-21 14:48:16 -04008245void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8246{
8247 struct btrfs_block_group_cache *block_group;
8248 u64 last = 0;
8249
8250 while (1) {
8251 struct inode *inode;
8252
8253 block_group = btrfs_lookup_first_block_group(info, last);
8254 while (block_group) {
8255 spin_lock(&block_group->lock);
8256 if (block_group->iref)
8257 break;
8258 spin_unlock(&block_group->lock);
8259 block_group = next_block_group(info->tree_root,
8260 block_group);
8261 }
8262 if (!block_group) {
8263 if (last == 0)
8264 break;
8265 last = 0;
8266 continue;
8267 }
8268
8269 inode = block_group->inode;
8270 block_group->iref = 0;
8271 block_group->inode = NULL;
8272 spin_unlock(&block_group->lock);
8273 iput(inode);
8274 last = block_group->key.objectid + block_group->key.offset;
8275 btrfs_put_block_group(block_group);
8276 }
8277}
8278
Zheng Yan1a40e232008-09-26 10:09:34 -04008279int btrfs_free_block_groups(struct btrfs_fs_info *info)
8280{
8281 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008282 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008283 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008284 struct rb_node *n;
8285
Yan Zheng11833d62009-09-11 16:11:19 -04008286 down_write(&info->extent_commit_sem);
8287 while (!list_empty(&info->caching_block_groups)) {
8288 caching_ctl = list_entry(info->caching_block_groups.next,
8289 struct btrfs_caching_control, list);
8290 list_del(&caching_ctl->list);
8291 put_caching_control(caching_ctl);
8292 }
8293 up_write(&info->extent_commit_sem);
8294
Zheng Yan1a40e232008-09-26 10:09:34 -04008295 spin_lock(&info->block_group_cache_lock);
8296 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8297 block_group = rb_entry(n, struct btrfs_block_group_cache,
8298 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008299 rb_erase(&block_group->cache_node,
8300 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008301 spin_unlock(&info->block_group_cache_lock);
8302
Josef Bacik80eb2342008-10-29 14:49:05 -04008303 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008304 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008305 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008306
Josef Bacik817d52f2009-07-13 21:29:25 -04008307 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008308 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008309
Josef Bacik3c148742011-02-02 15:53:47 +00008310 /*
8311 * We haven't cached this block group, which means we could
8312 * possibly have excluded extents on this block group.
8313 */
Josef Bacik36cce922013-08-05 11:15:21 -04008314 if (block_group->cached == BTRFS_CACHE_NO ||
8315 block_group->cached == BTRFS_CACHE_ERROR)
Josef Bacik3c148742011-02-02 15:53:47 +00008316 free_excluded_extents(info->extent_root, block_group);
8317
Josef Bacik817d52f2009-07-13 21:29:25 -04008318 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008319 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008320
8321 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008322 }
8323 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008324
8325 /* now that all the block groups are freed, go through and
8326 * free all the space_info structs. This is only called during
8327 * the final stages of unmount, and so we know nobody is
8328 * using them. We call synchronize_rcu() once before we start,
8329 * just to be on the safe side.
8330 */
8331 synchronize_rcu();
8332
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008333 release_global_block_rsv(info);
8334
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308335 while (!list_empty(&info->space_info)) {
Chris Mason4184ea72009-03-10 12:39:20 -04008336 space_info = list_entry(info->space_info.next,
8337 struct btrfs_space_info,
8338 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008339 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308340 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00008341 space_info->bytes_reserved > 0 ||
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308342 space_info->bytes_may_use > 0)) {
David Sterbab069e0c2013-02-08 21:28:17 +00008343 dump_space_info(space_info, 0, 0);
8344 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008345 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04008346 percpu_counter_destroy(&space_info->total_bytes_pinned);
Chris Mason4184ea72009-03-10 12:39:20 -04008347 list_del(&space_info->list);
8348 kfree(space_info);
8349 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008350 return 0;
8351}
8352
Yan, Zhengb742bb82010-05-16 10:46:24 -04008353static void __link_block_group(struct btrfs_space_info *space_info,
8354 struct btrfs_block_group_cache *cache)
8355{
8356 int index = get_block_group_index(cache);
8357
8358 down_write(&space_info->groups_sem);
8359 list_add_tail(&cache->list, &space_info->block_groups[index]);
8360 up_write(&space_info->groups_sem);
8361}
8362
Chris Mason9078a3e2007-04-26 16:46:15 -04008363int btrfs_read_block_groups(struct btrfs_root *root)
8364{
8365 struct btrfs_path *path;
8366 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008367 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008368 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008369 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008370 struct btrfs_key key;
8371 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008372 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008373 int need_clear = 0;
8374 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008375
Chris Masonbe744172007-05-06 10:15:01 -04008376 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008377 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008378 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008379 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008380 path = btrfs_alloc_path();
8381 if (!path)
8382 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008383 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008384
David Sterba6c417612011-04-13 15:41:04 +02008385 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008386 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008387 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008388 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008389 if (btrfs_test_opt(root, CLEAR_CACHE))
8390 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008391
Chris Masond3977122009-01-05 21:25:51 -05008392 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008393 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008394 if (ret > 0)
8395 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008396 if (ret != 0)
8397 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008398 leaf = path->nodes[0];
8399 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008400 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008401 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008402 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008403 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008404 }
Li Zefan34d52cb2011-03-29 13:46:06 +08008405 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8406 GFP_NOFS);
8407 if (!cache->free_space_ctl) {
8408 kfree(cache);
8409 ret = -ENOMEM;
8410 goto error;
8411 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008412
Yan Zhengd2fb3432008-12-11 16:30:39 -05008413 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008414 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008415 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008416 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008417 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008418
Liu Bocf7c1ef2012-07-06 03:31:34 -06008419 if (need_clear) {
8420 /*
8421 * When we mount with old space cache, we need to
8422 * set BTRFS_DC_CLEAR and set dirty flag.
8423 *
8424 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8425 * truncate the old free space cache inode and
8426 * setup a new one.
8427 * b) Setting 'dirty flag' makes sure that we flush
8428 * the new space cache info onto disk.
8429 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008430 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008431 if (btrfs_test_opt(root, SPACE_CACHE))
8432 cache->dirty = 1;
8433 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008434
Chris Mason5f39d392007-10-15 16:14:19 -04008435 read_extent_buffer(leaf, &cache->item,
8436 btrfs_item_ptr_offset(leaf, path->slots[0]),
8437 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008438 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008439
Chris Mason9078a3e2007-04-26 16:46:15 -04008440 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008441 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008442 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008443 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05008444 cache->full_stripe_len = btrfs_full_stripe_len(root,
8445 &root->fs_info->mapping_tree,
8446 found_key.objectid);
Li Zefan34d52cb2011-03-29 13:46:06 +08008447 btrfs_init_free_space_ctl(cache);
8448
Josef Bacik817d52f2009-07-13 21:29:25 -04008449 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008450 * We need to exclude the super stripes now so that the space
8451 * info has super bytes accounted for, otherwise we'll think
8452 * we have more space than we actually do.
8453 */
Josef Bacik835d9742013-03-19 12:13:25 -04008454 ret = exclude_super_stripes(root, cache);
8455 if (ret) {
8456 /*
8457 * We may have excluded something, so call this just in
8458 * case.
8459 */
8460 free_excluded_extents(root, cache);
8461 kfree(cache->free_space_ctl);
8462 kfree(cache);
8463 goto error;
8464 }
Josef Bacik3c148742011-02-02 15:53:47 +00008465
8466 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008467 * check for two cases, either we are full, and therefore
8468 * don't need to bother with the caching work since we won't
8469 * find any space, or we are empty, and we can just add all
8470 * the space in and be done with it. This saves us _alot_ of
8471 * time, particularly in the full case.
8472 */
8473 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008474 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008475 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008476 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008477 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008478 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008479 cache->cached = BTRFS_CACHE_FINISHED;
8480 add_new_free_space(cache, root->fs_info,
8481 found_key.objectid,
8482 found_key.objectid +
8483 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008484 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008485 }
Chris Mason96b51792007-10-15 16:15:19 -04008486
Josef Bacik8c579fe2013-04-02 12:40:42 -04008487 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8488 if (ret) {
8489 btrfs_remove_free_space_cache(cache);
8490 btrfs_put_block_group(cache);
8491 goto error;
8492 }
8493
Chris Mason6324fbf2008-03-24 15:01:59 -04008494 ret = update_space_info(info, cache->flags, found_key.offset,
8495 btrfs_block_group_used(&cache->item),
8496 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008497 if (ret) {
8498 btrfs_remove_free_space_cache(cache);
8499 spin_lock(&info->block_group_cache_lock);
8500 rb_erase(&cache->cache_node,
8501 &info->block_group_cache_tree);
8502 spin_unlock(&info->block_group_cache_lock);
8503 btrfs_put_block_group(cache);
8504 goto error;
8505 }
8506
Chris Mason6324fbf2008-03-24 15:01:59 -04008507 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008508 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008509 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008510 spin_unlock(&cache->space_info->lock);
8511
Yan, Zhengb742bb82010-05-16 10:46:24 -04008512 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008513
Chris Mason75ccf472008-09-30 19:24:06 -04008514 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008515 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008516 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008517 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008518
8519 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8520 if (!(get_alloc_profile(root, space_info->flags) &
8521 (BTRFS_BLOCK_GROUP_RAID10 |
8522 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008523 BTRFS_BLOCK_GROUP_RAID5 |
8524 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008525 BTRFS_BLOCK_GROUP_DUP)))
8526 continue;
8527 /*
8528 * avoid allocating from un-mirrored block group if there are
8529 * mirrored block groups.
8530 */
chandan1095cc02013-07-16 12:28:56 +05308531 list_for_each_entry(cache,
8532 &space_info->block_groups[BTRFS_RAID_RAID0],
8533 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008534 set_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05308535 list_for_each_entry(cache,
8536 &space_info->block_groups[BTRFS_RAID_SINGLE],
8537 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008538 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008539 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008540
8541 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008542 ret = 0;
8543error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008544 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008545 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008546}
Chris Mason6324fbf2008-03-24 15:01:59 -04008547
Josef Bacikea658ba2012-09-11 16:57:25 -04008548void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8549 struct btrfs_root *root)
8550{
8551 struct btrfs_block_group_cache *block_group, *tmp;
8552 struct btrfs_root *extent_root = root->fs_info->extent_root;
8553 struct btrfs_block_group_item item;
8554 struct btrfs_key key;
8555 int ret = 0;
8556
8557 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8558 new_bg_list) {
8559 list_del_init(&block_group->new_bg_list);
8560
8561 if (ret)
8562 continue;
8563
8564 spin_lock(&block_group->lock);
8565 memcpy(&item, &block_group->item, sizeof(item));
8566 memcpy(&key, &block_group->key, sizeof(key));
8567 spin_unlock(&block_group->lock);
8568
8569 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8570 sizeof(item));
8571 if (ret)
8572 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacik6df9a952013-06-27 13:22:46 -04008573 ret = btrfs_finish_chunk_alloc(trans, extent_root,
8574 key.objectid, key.offset);
8575 if (ret)
8576 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacikea658ba2012-09-11 16:57:25 -04008577 }
8578}
8579
Chris Mason6324fbf2008-03-24 15:01:59 -04008580int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8581 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008582 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008583 u64 size)
8584{
8585 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008586 struct btrfs_root *extent_root;
8587 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008588
8589 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008590
Chris Mason12fcfd22009-03-24 10:24:20 -04008591 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008592
Chris Mason8f18cf12008-04-25 16:53:30 -04008593 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008594 if (!cache)
8595 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008596 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8597 GFP_NOFS);
8598 if (!cache->free_space_ctl) {
8599 kfree(cache);
8600 return -ENOMEM;
8601 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008602
Chris Masone17cade2008-04-15 15:41:47 -04008603 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008604 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008605 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008606 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008607 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008608 cache->full_stripe_len = btrfs_full_stripe_len(root,
8609 &root->fs_info->mapping_tree,
8610 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008611
Yan Zhengd2fb3432008-12-11 16:30:39 -05008612 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008613 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008614 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008615 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008616 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008617
Li Zefan34d52cb2011-03-29 13:46:06 +08008618 btrfs_init_free_space_ctl(cache);
8619
Chris Mason6324fbf2008-03-24 15:01:59 -04008620 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008621 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8622 cache->flags = type;
8623 btrfs_set_block_group_flags(&cache->item, type);
8624
Yan Zheng11833d62009-09-11 16:11:19 -04008625 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008626 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008627 ret = exclude_super_stripes(root, cache);
8628 if (ret) {
8629 /*
8630 * We may have excluded something, so call this just in
8631 * case.
8632 */
8633 free_excluded_extents(root, cache);
8634 kfree(cache->free_space_ctl);
8635 kfree(cache);
8636 return ret;
8637 }
Josef Bacik96303082009-07-13 21:29:25 -04008638
Josef Bacik817d52f2009-07-13 21:29:25 -04008639 add_new_free_space(cache, root->fs_info, chunk_offset,
8640 chunk_offset + size);
8641
Yan Zheng11833d62009-09-11 16:11:19 -04008642 free_excluded_extents(root, cache);
8643
Josef Bacik8c579fe2013-04-02 12:40:42 -04008644 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8645 if (ret) {
8646 btrfs_remove_free_space_cache(cache);
8647 btrfs_put_block_group(cache);
8648 return ret;
8649 }
8650
Chris Mason6324fbf2008-03-24 15:01:59 -04008651 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8652 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008653 if (ret) {
8654 btrfs_remove_free_space_cache(cache);
8655 spin_lock(&root->fs_info->block_group_cache_lock);
8656 rb_erase(&cache->cache_node,
8657 &root->fs_info->block_group_cache_tree);
8658 spin_unlock(&root->fs_info->block_group_cache_lock);
8659 btrfs_put_block_group(cache);
8660 return ret;
8661 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008662 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008663
8664 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008665 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008666 spin_unlock(&cache->space_info->lock);
8667
Yan, Zhengb742bb82010-05-16 10:46:24 -04008668 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008669
Josef Bacikea658ba2012-09-11 16:57:25 -04008670 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008671
Chris Masond18a2c42008-04-04 15:40:00 -04008672 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008673
Chris Mason6324fbf2008-03-24 15:01:59 -04008674 return 0;
8675}
Zheng Yan1a40e232008-09-26 10:09:34 -04008676
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008677static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8678{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008679 u64 extra_flags = chunk_to_extended(flags) &
8680 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008681
Miao Xiede98ced2013-01-29 10:13:12 +00008682 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008683 if (flags & BTRFS_BLOCK_GROUP_DATA)
8684 fs_info->avail_data_alloc_bits &= ~extra_flags;
8685 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8686 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8687 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8688 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008689 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008690}
8691
Zheng Yan1a40e232008-09-26 10:09:34 -04008692int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8693 struct btrfs_root *root, u64 group_start)
8694{
8695 struct btrfs_path *path;
8696 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008697 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008698 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008699 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008700 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008701 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008702 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008703 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008704
Zheng Yan1a40e232008-09-26 10:09:34 -04008705 root = root->fs_info->extent_root;
8706
8707 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8708 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008709 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008710
liubo9f7c43c2011-03-07 02:13:33 +00008711 /*
8712 * Free the reserved super bytes from this block group before
8713 * remove it.
8714 */
8715 free_excluded_extents(root, block_group);
8716
Zheng Yan1a40e232008-09-26 10:09:34 -04008717 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008718 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008719 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8720 BTRFS_BLOCK_GROUP_RAID1 |
8721 BTRFS_BLOCK_GROUP_RAID10))
8722 factor = 2;
8723 else
8724 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008725
Chris Mason44fb5512009-06-04 15:34:51 -04008726 /* make sure this block group isn't part of an allocation cluster */
8727 cluster = &root->fs_info->data_alloc_cluster;
8728 spin_lock(&cluster->refill_lock);
8729 btrfs_return_cluster_to_free_space(block_group, cluster);
8730 spin_unlock(&cluster->refill_lock);
8731
8732 /*
8733 * make sure this block group isn't part of a metadata
8734 * allocation cluster
8735 */
8736 cluster = &root->fs_info->meta_alloc_cluster;
8737 spin_lock(&cluster->refill_lock);
8738 btrfs_return_cluster_to_free_space(block_group, cluster);
8739 spin_unlock(&cluster->refill_lock);
8740
Zheng Yan1a40e232008-09-26 10:09:34 -04008741 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008742 if (!path) {
8743 ret = -ENOMEM;
8744 goto out;
8745 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008746
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008747 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008748 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008749 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008750 if (ret) {
8751 btrfs_add_delayed_iput(inode);
8752 goto out;
8753 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008754 clear_nlink(inode);
8755 /* One for the block groups ref */
8756 spin_lock(&block_group->lock);
8757 if (block_group->iref) {
8758 block_group->iref = 0;
8759 block_group->inode = NULL;
8760 spin_unlock(&block_group->lock);
8761 iput(inode);
8762 } else {
8763 spin_unlock(&block_group->lock);
8764 }
8765 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008766 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008767 }
8768
8769 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8770 key.offset = block_group->key.objectid;
8771 key.type = 0;
8772
8773 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8774 if (ret < 0)
8775 goto out;
8776 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008777 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008778 if (ret == 0) {
8779 ret = btrfs_del_item(trans, tree_root, path);
8780 if (ret)
8781 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008782 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008783 }
8784
Yan Zheng3dfdb932009-01-21 10:49:16 -05008785 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008786 rb_erase(&block_group->cache_node,
8787 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008788
8789 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8790 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008791 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008792
Josef Bacik80eb2342008-10-29 14:49:05 -04008793 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008794 /*
8795 * we must use list_del_init so people can check to see if they
8796 * are still on the list after taking the semaphore
8797 */
8798 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008799 if (list_empty(&block_group->space_info->block_groups[index]))
8800 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008801 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008802
Josef Bacik817d52f2009-07-13 21:29:25 -04008803 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008804 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008805
8806 btrfs_remove_free_space_cache(block_group);
8807
Yan Zhengc146afa2008-11-12 14:34:12 -05008808 spin_lock(&block_group->space_info->lock);
8809 block_group->space_info->total_bytes -= block_group->key.offset;
8810 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008811 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008812 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008813
Josef Bacik0af3d002010-06-21 14:48:16 -04008814 memcpy(&key, &block_group->key, sizeof(key));
8815
Chris Mason283bb192009-07-24 16:30:55 -04008816 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008817
Chris Masonfa9c0d72009-04-03 09:47:43 -04008818 btrfs_put_block_group(block_group);
8819 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008820
8821 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8822 if (ret > 0)
8823 ret = -EIO;
8824 if (ret < 0)
8825 goto out;
8826
8827 ret = btrfs_del_item(trans, root, path);
8828out:
8829 btrfs_free_path(path);
8830 return ret;
8831}
liuboacce9522011-01-06 19:30:25 +08008832
liuboc59021f2011-03-07 02:13:14 +00008833int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8834{
8835 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008836 struct btrfs_super_block *disk_super;
8837 u64 features;
8838 u64 flags;
8839 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008840 int ret;
8841
David Sterba6c417612011-04-13 15:41:04 +02008842 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008843 if (!btrfs_super_root(disk_super))
8844 return 1;
liuboc59021f2011-03-07 02:13:14 +00008845
liubo1aba86d2011-04-08 08:44:37 +00008846 features = btrfs_super_incompat_flags(disk_super);
8847 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8848 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008849
liubo1aba86d2011-04-08 08:44:37 +00008850 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8851 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008852 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008853 goto out;
liuboc59021f2011-03-07 02:13:14 +00008854
liubo1aba86d2011-04-08 08:44:37 +00008855 if (mixed) {
8856 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8857 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8858 } else {
8859 flags = BTRFS_BLOCK_GROUP_METADATA;
8860 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8861 if (ret)
8862 goto out;
8863
8864 flags = BTRFS_BLOCK_GROUP_DATA;
8865 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8866 }
8867out:
liuboc59021f2011-03-07 02:13:14 +00008868 return ret;
8869}
8870
liuboacce9522011-01-06 19:30:25 +08008871int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8872{
8873 return unpin_extent_range(root, start, end);
8874}
8875
8876int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008877 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008878{
Li Dongyang5378e602011-03-24 10:24:27 +00008879 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008880}
Li Dongyangf7039b12011-03-24 10:24:28 +00008881
8882int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8883{
8884 struct btrfs_fs_info *fs_info = root->fs_info;
8885 struct btrfs_block_group_cache *cache = NULL;
8886 u64 group_trimmed;
8887 u64 start;
8888 u64 end;
8889 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008890 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008891 int ret = 0;
8892
Liu Bo2cac13e2012-02-09 18:17:41 +08008893 /*
8894 * try to trim all FS space, our block group may start from non-zero.
8895 */
8896 if (range->len == total_bytes)
8897 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8898 else
8899 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008900
8901 while (cache) {
8902 if (cache->key.objectid >= (range->start + range->len)) {
8903 btrfs_put_block_group(cache);
8904 break;
8905 }
8906
8907 start = max(range->start, cache->key.objectid);
8908 end = min(range->start + range->len,
8909 cache->key.objectid + cache->key.offset);
8910
8911 if (end - start >= range->minlen) {
8912 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008913 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04008914 if (ret) {
8915 btrfs_put_block_group(cache);
8916 break;
8917 }
8918 ret = wait_block_group_cache_done(cache);
8919 if (ret) {
8920 btrfs_put_block_group(cache);
8921 break;
8922 }
Li Dongyangf7039b12011-03-24 10:24:28 +00008923 }
8924 ret = btrfs_trim_block_group(cache,
8925 &group_trimmed,
8926 start,
8927 end,
8928 range->minlen);
8929
8930 trimmed += group_trimmed;
8931 if (ret) {
8932 btrfs_put_block_group(cache);
8933 break;
8934 }
8935 }
8936
8937 cache = next_block_group(fs_info->tree_root, cache);
8938 }
8939
8940 range->len = trimmed;
8941 return ret;
8942}