blob: bb5b3067ddc3b505a5cc6b3dcf8044d4adbeabd6 [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"
Miao Xie995946d2014-04-02 19:51:06 +080029#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050030#include "disk-io.h"
31#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050033#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040034#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040035#include "free-space-cache.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060036#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040037#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070038#include "qgroup.h"
Chris Masonfec577f2007-02-26 10:40:21 -050039
Arne Jansen709c0482011-09-12 12:22:57 +020040#undef SCRAMBLE_DELAYED_REFS
41
Miao Xie9e622d62012-01-26 15:01:12 -050042/*
43 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040044 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
45 * if we really need one.
46 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040047 * CHUNK_ALLOC_LIMITED means to only try and allocate one
48 * if we have very few chunks already allocated. This is
49 * used as part of the clustering code to help make sure
50 * we have a good pool of storage to cluster in, without
51 * filling the FS with empty chunks
52 *
Miao Xie9e622d62012-01-26 15:01:12 -050053 * CHUNK_ALLOC_FORCE means it must try to allocate one
54 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040055 */
56enum {
57 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050058 CHUNK_ALLOC_LIMITED = 1,
59 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040060};
61
Josef Bacikfb25e912011-07-26 17:00:46 -040062/*
63 * Control how reservations are dealt with.
64 *
65 * RESERVE_FREE - freeing a reservation.
66 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
67 * ENOSPC accounting
68 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
69 * bytes_may_use as the ENOSPC accounting is done elsewhere
70 */
71enum {
72 RESERVE_FREE = 0,
73 RESERVE_ALLOC = 1,
74 RESERVE_ALLOC_NO_ACCOUNT = 2,
75};
76
Liu Boc53d6132012-12-27 09:01:19 +000077static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040078 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040079static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 u64 bytenr, u64 num_bytes, u64 parent,
82 u64 root_objectid, u64 owner_objectid,
83 u64 owner_offset, int refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -070084 struct btrfs_delayed_extent_op *extra_op,
85 int no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040086static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
87 struct extent_buffer *leaf,
88 struct btrfs_extent_item *ei);
89static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
90 struct btrfs_root *root,
91 u64 parent, u64 root_objectid,
92 u64 flags, u64 owner, u64 offset,
93 struct btrfs_key *ins, int ref_mod);
94static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 u64 parent, u64 root_objectid,
97 u64 flags, struct btrfs_disk_key *key,
Josef Bacikfcebe452014-05-13 17:30:47 -070098 int level, struct btrfs_key *ins,
99 int no_quota);
Josef Bacik6a632092009-02-20 11:00:09 -0500100static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -0400101 struct btrfs_root *extent_root, u64 flags,
102 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400103static int find_next_key(struct btrfs_path *path, int level,
104 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400105static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
106 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400107static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
108 u64 num_bytes, int reserve);
Josef Bacik5d803662013-02-07 16:06:02 -0500109static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
110 u64 num_bytes);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000111int btrfs_pin_extent(struct btrfs_root *root,
112 u64 bytenr, u64 num_bytes, int reserved);
Josef Bacik6a632092009-02-20 11:00:09 -0500113
Josef Bacik817d52f2009-07-13 21:29:25 -0400114static noinline int
115block_group_cache_done(struct btrfs_block_group_cache *cache)
116{
117 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -0400118 return cache->cached == BTRFS_CACHE_FINISHED ||
119 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -0400120}
121
Josef Bacik0f9dd462008-09-23 13:14:11 -0400122static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
123{
124 return (cache->flags & bits) == bits;
125}
126
David Sterba62a45b62011-04-20 15:52:26 +0200127static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000128{
129 atomic_inc(&cache->count);
130}
131
132void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
133{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400134 if (atomic_dec_and_test(&cache->count)) {
135 WARN_ON(cache->pinned > 0);
136 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800137 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000138 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400139 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000140}
141
Josef Bacik0f9dd462008-09-23 13:14:11 -0400142/*
143 * this adds the block group to the fs_info rb tree for the block group
144 * cache
145 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500146static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400147 struct btrfs_block_group_cache *block_group)
148{
149 struct rb_node **p;
150 struct rb_node *parent = NULL;
151 struct btrfs_block_group_cache *cache;
152
153 spin_lock(&info->block_group_cache_lock);
154 p = &info->block_group_cache_tree.rb_node;
155
156 while (*p) {
157 parent = *p;
158 cache = rb_entry(parent, struct btrfs_block_group_cache,
159 cache_node);
160 if (block_group->key.objectid < cache->key.objectid) {
161 p = &(*p)->rb_left;
162 } else if (block_group->key.objectid > cache->key.objectid) {
163 p = &(*p)->rb_right;
164 } else {
165 spin_unlock(&info->block_group_cache_lock);
166 return -EEXIST;
167 }
168 }
169
170 rb_link_node(&block_group->cache_node, parent, p);
171 rb_insert_color(&block_group->cache_node,
172 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000173
174 if (info->first_logical_byte > block_group->key.objectid)
175 info->first_logical_byte = block_group->key.objectid;
176
Josef Bacik0f9dd462008-09-23 13:14:11 -0400177 spin_unlock(&info->block_group_cache_lock);
178
179 return 0;
180}
181
182/*
183 * This will return the block group at or after bytenr if contains is 0, else
184 * it will return the block group that contains the bytenr
185 */
186static struct btrfs_block_group_cache *
187block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
188 int contains)
189{
190 struct btrfs_block_group_cache *cache, *ret = NULL;
191 struct rb_node *n;
192 u64 end, start;
193
194 spin_lock(&info->block_group_cache_lock);
195 n = info->block_group_cache_tree.rb_node;
196
197 while (n) {
198 cache = rb_entry(n, struct btrfs_block_group_cache,
199 cache_node);
200 end = cache->key.objectid + cache->key.offset - 1;
201 start = cache->key.objectid;
202
203 if (bytenr < start) {
204 if (!contains && (!ret || start < ret->key.objectid))
205 ret = cache;
206 n = n->rb_left;
207 } else if (bytenr > start) {
208 if (contains && bytenr <= end) {
209 ret = cache;
210 break;
211 }
212 n = n->rb_right;
213 } else {
214 ret = cache;
215 break;
216 }
217 }
Liu Boa1897fd2012-12-27 09:01:23 +0000218 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000219 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000220 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
221 info->first_logical_byte = ret->key.objectid;
222 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400223 spin_unlock(&info->block_group_cache_lock);
224
225 return ret;
226}
227
Yan Zheng11833d62009-09-11 16:11:19 -0400228static int add_excluded_extent(struct btrfs_root *root,
229 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400230{
Yan Zheng11833d62009-09-11 16:11:19 -0400231 u64 end = start + num_bytes - 1;
232 set_extent_bits(&root->fs_info->freed_extents[0],
233 start, end, EXTENT_UPTODATE, GFP_NOFS);
234 set_extent_bits(&root->fs_info->freed_extents[1],
235 start, end, EXTENT_UPTODATE, GFP_NOFS);
236 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400237}
238
Yan Zheng11833d62009-09-11 16:11:19 -0400239static void free_excluded_extents(struct btrfs_root *root,
240 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400241{
Yan Zheng11833d62009-09-11 16:11:19 -0400242 u64 start, end;
243
244 start = cache->key.objectid;
245 end = start + cache->key.offset - 1;
246
247 clear_extent_bits(&root->fs_info->freed_extents[0],
248 start, end, EXTENT_UPTODATE, GFP_NOFS);
249 clear_extent_bits(&root->fs_info->freed_extents[1],
250 start, end, EXTENT_UPTODATE, GFP_NOFS);
251}
252
253static int exclude_super_stripes(struct btrfs_root *root,
254 struct btrfs_block_group_cache *cache)
255{
Josef Bacik817d52f2009-07-13 21:29:25 -0400256 u64 bytenr;
257 u64 *logical;
258 int stripe_len;
259 int i, nr, ret;
260
Yan, Zheng06b23312009-11-26 09:31:11 +0000261 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
262 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
263 cache->bytes_super += stripe_len;
264 ret = add_excluded_extent(root, cache->key.objectid,
265 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400266 if (ret)
267 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000268 }
269
Josef Bacik817d52f2009-07-13 21:29:25 -0400270 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
271 bytenr = btrfs_sb_offset(i);
272 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
273 cache->key.objectid, bytenr,
274 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400275 if (ret)
276 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400277
Josef Bacik817d52f2009-07-13 21:29:25 -0400278 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400279 u64 start, len;
280
281 if (logical[nr] > cache->key.objectid +
282 cache->key.offset)
283 continue;
284
285 if (logical[nr] + stripe_len <= cache->key.objectid)
286 continue;
287
288 start = logical[nr];
289 if (start < cache->key.objectid) {
290 start = cache->key.objectid;
291 len = (logical[nr] + stripe_len) - start;
292 } else {
293 len = min_t(u64, stripe_len,
294 cache->key.objectid +
295 cache->key.offset - start);
296 }
297
298 cache->bytes_super += len;
299 ret = add_excluded_extent(root, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400300 if (ret) {
301 kfree(logical);
302 return ret;
303 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400304 }
Yan Zheng11833d62009-09-11 16:11:19 -0400305
Josef Bacik817d52f2009-07-13 21:29:25 -0400306 kfree(logical);
307 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400308 return 0;
309}
310
Yan Zheng11833d62009-09-11 16:11:19 -0400311static struct btrfs_caching_control *
312get_caching_control(struct btrfs_block_group_cache *cache)
313{
314 struct btrfs_caching_control *ctl;
315
316 spin_lock(&cache->lock);
317 if (cache->cached != BTRFS_CACHE_STARTED) {
318 spin_unlock(&cache->lock);
319 return NULL;
320 }
321
Josef Bacikdde5abe2010-09-16 16:17:03 -0400322 /* We're loading it the fast way, so we don't have a caching_ctl. */
323 if (!cache->caching_ctl) {
324 spin_unlock(&cache->lock);
325 return NULL;
326 }
327
Yan Zheng11833d62009-09-11 16:11:19 -0400328 ctl = cache->caching_ctl;
329 atomic_inc(&ctl->count);
330 spin_unlock(&cache->lock);
331 return ctl;
332}
333
334static void put_caching_control(struct btrfs_caching_control *ctl)
335{
336 if (atomic_dec_and_test(&ctl->count))
337 kfree(ctl);
338}
339
Josef Bacik0f9dd462008-09-23 13:14:11 -0400340/*
341 * this is only called by cache_block_group, since we could have freed extents
342 * we need to check the pinned_extents for any extents that can't be used yet
343 * since their free space will be released as soon as the transaction commits.
344 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400345static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400346 struct btrfs_fs_info *info, u64 start, u64 end)
347{
Josef Bacik817d52f2009-07-13 21:29:25 -0400348 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400349 int ret;
350
351 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400352 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400353 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400354 EXTENT_DIRTY | EXTENT_UPTODATE,
355 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400356 if (ret)
357 break;
358
Yan, Zheng06b23312009-11-26 09:31:11 +0000359 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400360 start = extent_end + 1;
361 } else if (extent_start > start && extent_start < end) {
362 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400363 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500364 ret = btrfs_add_free_space(block_group, start,
365 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100366 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400367 start = extent_end + 1;
368 } else {
369 break;
370 }
371 }
372
373 if (start < end) {
374 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400375 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500376 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100377 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378 }
379
Josef Bacik817d52f2009-07-13 21:29:25 -0400380 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400381}
382
Qu Wenruod458b052014-02-28 10:46:19 +0800383static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400384{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400385 struct btrfs_block_group_cache *block_group;
386 struct btrfs_fs_info *fs_info;
387 struct btrfs_caching_control *caching_ctl;
388 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400389 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400390 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400391 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400392 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400393 u64 last = 0;
394 u32 nritems;
Josef Bacik36cce922013-08-05 11:15:21 -0400395 int ret = -ENOMEM;
Chris Masonf510cfe2007-10-15 16:14:48 -0400396
Josef Bacikbab39bf2011-06-30 14:42:28 -0400397 caching_ctl = container_of(work, struct btrfs_caching_control, work);
398 block_group = caching_ctl->block_group;
399 fs_info = block_group->fs_info;
400 extent_root = fs_info->extent_root;
401
Chris Masone37c9e62007-05-09 20:13:14 -0400402 path = btrfs_alloc_path();
403 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400404 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400405
Josef Bacik817d52f2009-07-13 21:29:25 -0400406 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400407
Chris Mason5cd57b22008-06-25 16:01:30 -0400408 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400409 * We don't want to deadlock with somebody trying to allocate a new
410 * extent for the extent root while also trying to search the extent
411 * root to add free space. So we skip locking and search the commit
412 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400413 */
414 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400415 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400416 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400417
Yan Zhenge4404d62008-12-12 10:03:26 -0500418 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400419 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400420 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400421again:
Yan Zheng11833d62009-09-11 16:11:19 -0400422 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400423 /* need to make sure the commit_root doesn't disappear */
Josef Bacik9e351cc2014-03-13 15:42:13 -0400424 down_read(&fs_info->commit_root_sem);
Chris Mason013f1b12009-07-31 14:57:55 -0400425
Liu Bo52ee28d2013-07-11 17:51:15 +0800426next:
Yan Zheng11833d62009-09-11 16:11:19 -0400427 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400428 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400429 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 leaf = path->nodes[0];
432 nritems = btrfs_header_nritems(leaf);
433
Chris Masond3977122009-01-05 21:25:51 -0500434 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200435 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400436 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400437 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400438 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400439
Yan Zheng11833d62009-09-11 16:11:19 -0400440 if (path->slots[0] < nritems) {
441 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
442 } else {
443 ret = find_next_key(path, 0, &key);
444 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400445 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400446
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400447 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400448 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacik589d8ad2011-05-11 17:30:53 -0400449 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400450 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400451 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400452 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400453 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400454 goto again;
455 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400456
457 ret = btrfs_next_leaf(extent_root, path);
458 if (ret < 0)
459 goto err;
460 if (ret)
461 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400462 leaf = path->nodes[0];
463 nritems = btrfs_header_nritems(leaf);
464 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400465 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400466
Liu Bo52ee28d2013-07-11 17:51:15 +0800467 if (key.objectid < last) {
468 key.objectid = last;
469 key.offset = 0;
470 key.type = BTRFS_EXTENT_ITEM_KEY;
471
472 caching_ctl->progress = last;
473 btrfs_release_path(path);
474 goto next;
475 }
476
Yan Zheng11833d62009-09-11 16:11:19 -0400477 if (key.objectid < block_group->key.objectid) {
478 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400479 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400480 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400481
Chris Masone37c9e62007-05-09 20:13:14 -0400482 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400483 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400484 break;
Yan7d7d6062007-09-14 16:15:28 -0400485
Josef Bacik3173a182013-03-07 14:22:04 -0500486 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
487 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400488 total_found += add_new_free_space(block_group,
489 fs_info, last,
490 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500491 if (key.type == BTRFS_METADATA_ITEM_KEY)
492 last = key.objectid +
493 fs_info->tree_root->leafsize;
494 else
495 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400496
Yan Zheng11833d62009-09-11 16:11:19 -0400497 if (total_found > (1024 * 1024 * 2)) {
498 total_found = 0;
499 wake_up(&caching_ctl->wait);
500 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400501 }
Chris Masone37c9e62007-05-09 20:13:14 -0400502 path->slots[0]++;
503 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400504 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400505
506 total_found += add_new_free_space(block_group, fs_info, last,
507 block_group->key.objectid +
508 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400509 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400510
511 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400512 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400513 block_group->cached = BTRFS_CACHE_FINISHED;
514 spin_unlock(&block_group->lock);
515
Chris Mason54aa1f42007-06-22 14:16:25 -0400516err:
Chris Masone37c9e62007-05-09 20:13:14 -0400517 btrfs_free_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400518 up_read(&fs_info->commit_root_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400519
Yan Zheng11833d62009-09-11 16:11:19 -0400520 free_excluded_extents(extent_root, block_group);
521
522 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400523out:
Josef Bacik36cce922013-08-05 11:15:21 -0400524 if (ret) {
525 spin_lock(&block_group->lock);
526 block_group->caching_ctl = NULL;
527 block_group->cached = BTRFS_CACHE_ERROR;
528 spin_unlock(&block_group->lock);
529 }
Yan Zheng11833d62009-09-11 16:11:19 -0400530 wake_up(&caching_ctl->wait);
531
532 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000533 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400534}
535
Josef Bacik9d66e232010-08-25 16:54:15 -0400536static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400537 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400538{
Josef Bacik291c7d22011-11-14 13:52:14 -0500539 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400540 struct btrfs_fs_info *fs_info = cache->fs_info;
541 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400542 int ret = 0;
543
Josef Bacik291c7d22011-11-14 13:52:14 -0500544 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100545 if (!caching_ctl)
546 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500547
548 INIT_LIST_HEAD(&caching_ctl->list);
549 mutex_init(&caching_ctl->mutex);
550 init_waitqueue_head(&caching_ctl->wait);
551 caching_ctl->block_group = cache;
552 caching_ctl->progress = cache->key.objectid;
553 atomic_set(&caching_ctl->count, 1);
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800554 btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500555
556 spin_lock(&cache->lock);
557 /*
558 * This should be a rare occasion, but this could happen I think in the
559 * case where one thread starts to load the space cache info, and then
560 * some other thread starts a transaction commit which tries to do an
561 * allocation while the other thread is still loading the space cache
562 * info. The previous loop should have kept us from choosing this block
563 * group, but if we've moved to the state where we will wait on caching
564 * block groups we need to first check if we're doing a fast load here,
565 * so we can wait for it to finish, otherwise we could end up allocating
566 * from a block group who's cache gets evicted for one reason or
567 * another.
568 */
569 while (cache->cached == BTRFS_CACHE_FAST) {
570 struct btrfs_caching_control *ctl;
571
572 ctl = cache->caching_ctl;
573 atomic_inc(&ctl->count);
574 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
575 spin_unlock(&cache->lock);
576
577 schedule();
578
579 finish_wait(&ctl->wait, &wait);
580 put_caching_control(ctl);
581 spin_lock(&cache->lock);
582 }
583
584 if (cache->cached != BTRFS_CACHE_NO) {
585 spin_unlock(&cache->lock);
586 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400587 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500588 }
589 WARN_ON(cache->caching_ctl);
590 cache->caching_ctl = caching_ctl;
591 cache->cached = BTRFS_CACHE_FAST;
592 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400593
Josef Bacikd53ba472012-04-12 16:03:57 -0400594 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400595 ret = load_free_space_cache(fs_info, cache);
596
597 spin_lock(&cache->lock);
598 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500599 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400600 cache->cached = BTRFS_CACHE_FINISHED;
601 cache->last_byte_to_unpin = (u64)-1;
602 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500603 if (load_cache_only) {
604 cache->caching_ctl = NULL;
605 cache->cached = BTRFS_CACHE_NO;
606 } else {
607 cache->cached = BTRFS_CACHE_STARTED;
608 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400609 }
610 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500611 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000612 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500613 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000614 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400615 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000616 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500617 } else {
618 /*
619 * We are not going to do the fast caching, set cached to the
620 * appropriate value and wakeup any waiters.
621 */
622 spin_lock(&cache->lock);
623 if (load_cache_only) {
624 cache->caching_ctl = NULL;
625 cache->cached = BTRFS_CACHE_NO;
626 } else {
627 cache->cached = BTRFS_CACHE_STARTED;
628 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400629 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500630 wake_up(&caching_ctl->wait);
631 }
632
633 if (load_cache_only) {
634 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400635 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400636 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400637
Josef Bacik9e351cc2014-03-13 15:42:13 -0400638 down_write(&fs_info->commit_root_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500639 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400640 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400641 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400642
Josef Bacik11dfe352009-11-13 20:12:59 +0000643 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400644
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800645 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400646
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400647 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400648}
649
Josef Bacik0f9dd462008-09-23 13:14:11 -0400650/*
651 * return the block group that starts at or after bytenr
652 */
Chris Masond3977122009-01-05 21:25:51 -0500653static struct btrfs_block_group_cache *
654btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400655{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400656 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400657
Josef Bacik0f9dd462008-09-23 13:14:11 -0400658 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400659
Josef Bacik0f9dd462008-09-23 13:14:11 -0400660 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400661}
662
Josef Bacik0f9dd462008-09-23 13:14:11 -0400663/*
Sankar P9f556842009-05-14 13:52:22 -0400664 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400665 */
Chris Masond3977122009-01-05 21:25:51 -0500666struct btrfs_block_group_cache *btrfs_lookup_block_group(
667 struct btrfs_fs_info *info,
668 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400669{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400670 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400671
Josef Bacik0f9dd462008-09-23 13:14:11 -0400672 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400673
Josef Bacik0f9dd462008-09-23 13:14:11 -0400674 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400675}
Chris Mason0b86a832008-03-24 15:01:56 -0400676
Josef Bacik0f9dd462008-09-23 13:14:11 -0400677static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
678 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400679{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400680 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400681 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400682
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200683 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400684
Chris Mason4184ea72009-03-10 12:39:20 -0400685 rcu_read_lock();
686 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400687 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400688 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400689 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400690 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400691 }
Chris Mason4184ea72009-03-10 12:39:20 -0400692 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400693 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400694}
695
Chris Mason4184ea72009-03-10 12:39:20 -0400696/*
697 * after adding space to the filesystem, we need to clear the full flags
698 * on all the space infos.
699 */
700void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
701{
702 struct list_head *head = &info->space_info;
703 struct btrfs_space_info *found;
704
705 rcu_read_lock();
706 list_for_each_entry_rcu(found, head, list)
707 found->full = 0;
708 rcu_read_unlock();
709}
710
Chris Masone02119d2008-09-05 16:13:11 -0400711/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400712int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400713{
714 int ret;
715 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400716 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400717
Zheng Yan31840ae2008-09-23 13:14:14 -0400718 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700719 if (!path)
720 return -ENOMEM;
721
Chris Masone02119d2008-09-05 16:13:11 -0400722 key.objectid = start;
723 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500724 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400725 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
726 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500727 if (ret > 0) {
728 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
729 if (key.objectid == start &&
730 key.type == BTRFS_METADATA_ITEM_KEY)
731 ret = 0;
732 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400733 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500734 return ret;
735}
736
Chris Masond8d5f3e2007-12-11 12:42:00 -0500737/*
Josef Bacik3173a182013-03-07 14:22:04 -0500738 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400739 *
740 * the head node for delayed ref is used to store the sum of all the
741 * reference count modifications queued up in the rbtree. the head
742 * node may also store the extent flags to set. This way you can check
743 * to see what the reference count and extent flags would be if all of
744 * the delayed refs are not processed.
745 */
746int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
747 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500748 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400749{
750 struct btrfs_delayed_ref_head *head;
751 struct btrfs_delayed_ref_root *delayed_refs;
752 struct btrfs_path *path;
753 struct btrfs_extent_item *ei;
754 struct extent_buffer *leaf;
755 struct btrfs_key key;
756 u32 item_size;
757 u64 num_refs;
758 u64 extent_flags;
759 int ret;
760
Josef Bacik3173a182013-03-07 14:22:04 -0500761 /*
762 * If we don't have skinny metadata, don't bother doing anything
763 * different
764 */
765 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
766 offset = root->leafsize;
767 metadata = 0;
768 }
769
Yan, Zhenga22285a2010-05-16 10:48:46 -0400770 path = btrfs_alloc_path();
771 if (!path)
772 return -ENOMEM;
773
Yan, Zhenga22285a2010-05-16 10:48:46 -0400774 if (!trans) {
775 path->skip_locking = 1;
776 path->search_commit_root = 1;
777 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000778
779search_again:
780 key.objectid = bytenr;
781 key.offset = offset;
782 if (metadata)
783 key.type = BTRFS_METADATA_ITEM_KEY;
784 else
785 key.type = BTRFS_EXTENT_ITEM_KEY;
786
Yan, Zhenga22285a2010-05-16 10:48:46 -0400787again:
788 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
789 &key, path, 0, 0);
790 if (ret < 0)
791 goto out_free;
792
Josef Bacik3173a182013-03-07 14:22:04 -0500793 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100794 if (path->slots[0]) {
795 path->slots[0]--;
796 btrfs_item_key_to_cpu(path->nodes[0], &key,
797 path->slots[0]);
798 if (key.objectid == bytenr &&
799 key.type == BTRFS_EXTENT_ITEM_KEY &&
800 key.offset == root->leafsize)
801 ret = 0;
802 }
803 if (ret) {
804 key.objectid = bytenr;
805 key.type = BTRFS_EXTENT_ITEM_KEY;
806 key.offset = root->leafsize;
807 btrfs_release_path(path);
808 goto again;
809 }
Josef Bacik3173a182013-03-07 14:22:04 -0500810 }
811
Yan, Zhenga22285a2010-05-16 10:48:46 -0400812 if (ret == 0) {
813 leaf = path->nodes[0];
814 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
815 if (item_size >= sizeof(*ei)) {
816 ei = btrfs_item_ptr(leaf, path->slots[0],
817 struct btrfs_extent_item);
818 num_refs = btrfs_extent_refs(leaf, ei);
819 extent_flags = btrfs_extent_flags(leaf, ei);
820 } else {
821#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
822 struct btrfs_extent_item_v0 *ei0;
823 BUG_ON(item_size != sizeof(*ei0));
824 ei0 = btrfs_item_ptr(leaf, path->slots[0],
825 struct btrfs_extent_item_v0);
826 num_refs = btrfs_extent_refs_v0(leaf, ei0);
827 /* FIXME: this isn't correct for data */
828 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
829#else
830 BUG();
831#endif
832 }
833 BUG_ON(num_refs == 0);
834 } else {
835 num_refs = 0;
836 extent_flags = 0;
837 ret = 0;
838 }
839
840 if (!trans)
841 goto out;
842
843 delayed_refs = &trans->transaction->delayed_refs;
844 spin_lock(&delayed_refs->lock);
845 head = btrfs_find_delayed_ref_head(trans, bytenr);
846 if (head) {
847 if (!mutex_trylock(&head->mutex)) {
848 atomic_inc(&head->node.refs);
849 spin_unlock(&delayed_refs->lock);
850
David Sterbab3b4aa72011-04-21 01:20:15 +0200851 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400852
David Sterba8cc33e52011-05-02 15:29:25 +0200853 /*
854 * Mutex was contended, block until it's released and try
855 * again
856 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400857 mutex_lock(&head->mutex);
858 mutex_unlock(&head->mutex);
859 btrfs_put_delayed_ref(&head->node);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000860 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400861 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500862 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400863 if (head->extent_op && head->extent_op->update_flags)
864 extent_flags |= head->extent_op->flags_to_set;
865 else
866 BUG_ON(num_refs == 0);
867
868 num_refs += head->node.ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500869 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400870 mutex_unlock(&head->mutex);
871 }
872 spin_unlock(&delayed_refs->lock);
873out:
874 WARN_ON(num_refs == 0);
875 if (refs)
876 *refs = num_refs;
877 if (flags)
878 *flags = extent_flags;
879out_free:
880 btrfs_free_path(path);
881 return ret;
882}
883
884/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500885 * Back reference rules. Back refs have three main goals:
886 *
887 * 1) differentiate between all holders of references to an extent so that
888 * when a reference is dropped we can make sure it was a valid reference
889 * before freeing the extent.
890 *
891 * 2) Provide enough information to quickly find the holders of an extent
892 * if we notice a given block is corrupted or bad.
893 *
894 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
895 * maintenance. This is actually the same as #2, but with a slightly
896 * different use case.
897 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400898 * There are two kinds of back refs. The implicit back refs is optimized
899 * for pointers in non-shared tree blocks. For a given pointer in a block,
900 * back refs of this kind provide information about the block's owner tree
901 * and the pointer's key. These information allow us to find the block by
902 * b-tree searching. The full back refs is for pointers in tree blocks not
903 * referenced by their owner trees. The location of tree block is recorded
904 * in the back refs. Actually the full back refs is generic, and can be
905 * used in all cases the implicit back refs is used. The major shortcoming
906 * of the full back refs is its overhead. Every time a tree block gets
907 * COWed, we have to update back refs entry for all pointers in it.
908 *
909 * For a newly allocated tree block, we use implicit back refs for
910 * pointers in it. This means most tree related operations only involve
911 * implicit back refs. For a tree block created in old transaction, the
912 * only way to drop a reference to it is COW it. So we can detect the
913 * event that tree block loses its owner tree's reference and do the
914 * back refs conversion.
915 *
916 * When a tree block is COW'd through a tree, there are four cases:
917 *
918 * The reference count of the block is one and the tree is the block's
919 * owner tree. Nothing to do in this case.
920 *
921 * The reference count of the block is one and the tree is not the
922 * block's owner tree. In this case, full back refs is used for pointers
923 * in the block. Remove these full back refs, add implicit back refs for
924 * every pointers in the new block.
925 *
926 * The reference count of the block is greater than one and the tree is
927 * the block's owner tree. In this case, implicit back refs is used for
928 * pointers in the block. Add full back refs for every pointers in the
929 * block, increase lower level extents' reference counts. The original
930 * implicit back refs are entailed to the new block.
931 *
932 * The reference count of the block is greater than one and the tree is
933 * not the block's owner tree. Add implicit back refs for every pointer in
934 * the new block, increase lower level extents' reference count.
935 *
936 * Back Reference Key composing:
937 *
938 * The key objectid corresponds to the first byte in the extent,
939 * The key type is used to differentiate between types of back refs.
940 * There are different meanings of the key offset for different types
941 * of back refs.
942 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500943 * File extents can be referenced by:
944 *
945 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400946 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500947 * - different offsets inside a file (bookend extents in file.c)
948 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500950 *
951 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500952 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953 * - original offset in the file
954 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400955 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 * The key offset for the implicit back refs is hash of the first
957 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500958 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500960 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500962 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 * The key offset for the implicit back refs is the first byte of
964 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500965 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400966 * When a file extent is allocated, The implicit back refs is used.
967 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500968 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 * (root_key.objectid, inode objectid, offset in file, 1)
970 *
971 * When a file extent is removed file truncation, we find the
972 * corresponding implicit back refs and check the following fields:
973 *
974 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500975 *
976 * Btree extents can be referenced by:
977 *
978 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500979 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400980 * Both the implicit back refs and the full back refs for tree blocks
981 * only consist of key. The key offset for the implicit back refs is
982 * objectid of block's owner tree. The key offset for the full back refs
983 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500984 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 * When implicit back refs is used, information about the lowest key and
986 * level of the tree block are required. These information are stored in
987 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500988 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400989
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
991static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
992 struct btrfs_root *root,
993 struct btrfs_path *path,
994 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500995{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400996 struct btrfs_extent_item *item;
997 struct btrfs_extent_item_v0 *ei0;
998 struct btrfs_extent_ref_v0 *ref0;
999 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001000 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001 struct btrfs_key key;
1002 struct btrfs_key found_key;
1003 u32 new_size = sizeof(*item);
1004 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001005 int ret;
1006
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001007 leaf = path->nodes[0];
1008 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001009
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1011 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_extent_item_v0);
1013 refs = btrfs_extent_refs_v0(leaf, ei0);
1014
1015 if (owner == (u64)-1) {
1016 while (1) {
1017 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1018 ret = btrfs_next_leaf(root, path);
1019 if (ret < 0)
1020 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001021 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001022 leaf = path->nodes[0];
1023 }
1024 btrfs_item_key_to_cpu(leaf, &found_key,
1025 path->slots[0]);
1026 BUG_ON(key.objectid != found_key.objectid);
1027 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1028 path->slots[0]++;
1029 continue;
1030 }
1031 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1032 struct btrfs_extent_ref_v0);
1033 owner = btrfs_ref_objectid_v0(leaf, ref0);
1034 break;
1035 }
1036 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001037 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001038
1039 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1040 new_size += sizeof(*bi);
1041
1042 new_size -= sizeof(*ei0);
1043 ret = btrfs_search_slot(trans, root, &key, path,
1044 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001045 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001046 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001047 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001049 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001050
1051 leaf = path->nodes[0];
1052 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1053 btrfs_set_extent_refs(leaf, item, refs);
1054 /* FIXME: get real generation */
1055 btrfs_set_extent_generation(leaf, item, 0);
1056 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1057 btrfs_set_extent_flags(leaf, item,
1058 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1059 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1060 bi = (struct btrfs_tree_block_info *)(item + 1);
1061 /* FIXME: get first key of the block */
1062 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1063 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1064 } else {
1065 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1066 }
1067 btrfs_mark_buffer_dirty(leaf);
1068 return 0;
1069}
1070#endif
1071
1072static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1073{
1074 u32 high_crc = ~(u32)0;
1075 u32 low_crc = ~(u32)0;
1076 __le64 lenum;
1077
1078 lenum = cpu_to_le64(root_objectid);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001079 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001080 lenum = cpu_to_le64(owner);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001081 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082 lenum = cpu_to_le64(offset);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001083 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001084
1085 return ((u64)high_crc << 31) ^ (u64)low_crc;
1086}
1087
1088static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1089 struct btrfs_extent_data_ref *ref)
1090{
1091 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1092 btrfs_extent_data_ref_objectid(leaf, ref),
1093 btrfs_extent_data_ref_offset(leaf, ref));
1094}
1095
1096static int match_extent_data_ref(struct extent_buffer *leaf,
1097 struct btrfs_extent_data_ref *ref,
1098 u64 root_objectid, u64 owner, u64 offset)
1099{
1100 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1101 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1102 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1103 return 0;
1104 return 1;
1105}
1106
1107static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1108 struct btrfs_root *root,
1109 struct btrfs_path *path,
1110 u64 bytenr, u64 parent,
1111 u64 root_objectid,
1112 u64 owner, u64 offset)
1113{
1114 struct btrfs_key key;
1115 struct btrfs_extent_data_ref *ref;
1116 struct extent_buffer *leaf;
1117 u32 nritems;
1118 int ret;
1119 int recow;
1120 int err = -ENOENT;
1121
1122 key.objectid = bytenr;
1123 if (parent) {
1124 key.type = BTRFS_SHARED_DATA_REF_KEY;
1125 key.offset = parent;
1126 } else {
1127 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1128 key.offset = hash_extent_data_ref(root_objectid,
1129 owner, offset);
1130 }
1131again:
1132 recow = 0;
1133 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1134 if (ret < 0) {
1135 err = ret;
1136 goto fail;
1137 }
1138
1139 if (parent) {
1140 if (!ret)
1141 return 0;
1142#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1143 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001144 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1146 if (ret < 0) {
1147 err = ret;
1148 goto fail;
1149 }
1150 if (!ret)
1151 return 0;
1152#endif
1153 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001154 }
1155
1156 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 nritems = btrfs_header_nritems(leaf);
1158 while (1) {
1159 if (path->slots[0] >= nritems) {
1160 ret = btrfs_next_leaf(root, path);
1161 if (ret < 0)
1162 err = ret;
1163 if (ret)
1164 goto fail;
1165
1166 leaf = path->nodes[0];
1167 nritems = btrfs_header_nritems(leaf);
1168 recow = 1;
1169 }
1170
1171 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1172 if (key.objectid != bytenr ||
1173 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1174 goto fail;
1175
1176 ref = btrfs_item_ptr(leaf, path->slots[0],
1177 struct btrfs_extent_data_ref);
1178
1179 if (match_extent_data_ref(leaf, ref, root_objectid,
1180 owner, offset)) {
1181 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001182 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 goto again;
1184 }
1185 err = 0;
1186 break;
1187 }
1188 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001189 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001190fail:
1191 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001192}
1193
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1195 struct btrfs_root *root,
1196 struct btrfs_path *path,
1197 u64 bytenr, u64 parent,
1198 u64 root_objectid, u64 owner,
1199 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001200{
1201 struct btrfs_key key;
1202 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001204 u32 num_refs;
1205 int ret;
1206
1207 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208 if (parent) {
1209 key.type = BTRFS_SHARED_DATA_REF_KEY;
1210 key.offset = parent;
1211 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001212 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001213 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1214 key.offset = hash_extent_data_ref(root_objectid,
1215 owner, offset);
1216 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001217 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218
1219 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1220 if (ret && ret != -EEXIST)
1221 goto fail;
1222
1223 leaf = path->nodes[0];
1224 if (parent) {
1225 struct btrfs_shared_data_ref *ref;
1226 ref = btrfs_item_ptr(leaf, path->slots[0],
1227 struct btrfs_shared_data_ref);
1228 if (ret == 0) {
1229 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1230 } else {
1231 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1232 num_refs += refs_to_add;
1233 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1234 }
1235 } else {
1236 struct btrfs_extent_data_ref *ref;
1237 while (ret == -EEXIST) {
1238 ref = btrfs_item_ptr(leaf, path->slots[0],
1239 struct btrfs_extent_data_ref);
1240 if (match_extent_data_ref(leaf, ref, root_objectid,
1241 owner, offset))
1242 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001243 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001244 key.offset++;
1245 ret = btrfs_insert_empty_item(trans, root, path, &key,
1246 size);
1247 if (ret && ret != -EEXIST)
1248 goto fail;
1249
1250 leaf = path->nodes[0];
1251 }
1252 ref = btrfs_item_ptr(leaf, path->slots[0],
1253 struct btrfs_extent_data_ref);
1254 if (ret == 0) {
1255 btrfs_set_extent_data_ref_root(leaf, ref,
1256 root_objectid);
1257 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1258 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1259 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1260 } else {
1261 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1262 num_refs += refs_to_add;
1263 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1264 }
1265 }
1266 btrfs_mark_buffer_dirty(leaf);
1267 ret = 0;
1268fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001269 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001270 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001271}
1272
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1274 struct btrfs_root *root,
1275 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001276 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001277{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001278 struct btrfs_key key;
1279 struct btrfs_extent_data_ref *ref1 = NULL;
1280 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001281 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001282 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001283 int ret = 0;
1284
1285 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1287
1288 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1289 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1290 struct btrfs_extent_data_ref);
1291 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1292 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1293 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1294 struct btrfs_shared_data_ref);
1295 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1296#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1297 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1298 struct btrfs_extent_ref_v0 *ref0;
1299 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1300 struct btrfs_extent_ref_v0);
1301 num_refs = btrfs_ref_count_v0(leaf, ref0);
1302#endif
1303 } else {
1304 BUG();
1305 }
1306
Chris Mason56bec292009-03-13 10:10:06 -04001307 BUG_ON(num_refs < refs_to_drop);
1308 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309
Zheng Yan31840ae2008-09-23 13:14:14 -04001310 if (num_refs == 0) {
1311 ret = btrfs_del_item(trans, root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001312 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001313 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001314 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1315 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1316 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1317 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1318#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1319 else {
1320 struct btrfs_extent_ref_v0 *ref0;
1321 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1322 struct btrfs_extent_ref_v0);
1323 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1324 }
1325#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001326 btrfs_mark_buffer_dirty(leaf);
1327 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328 return ret;
1329}
1330
1331static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1332 struct btrfs_path *path,
1333 struct btrfs_extent_inline_ref *iref)
1334{
1335 struct btrfs_key key;
1336 struct extent_buffer *leaf;
1337 struct btrfs_extent_data_ref *ref1;
1338 struct btrfs_shared_data_ref *ref2;
1339 u32 num_refs = 0;
1340
1341 leaf = path->nodes[0];
1342 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1343 if (iref) {
1344 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1345 BTRFS_EXTENT_DATA_REF_KEY) {
1346 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1347 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1348 } else {
1349 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1350 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1351 }
1352 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1353 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1354 struct btrfs_extent_data_ref);
1355 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1356 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1357 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1358 struct btrfs_shared_data_ref);
1359 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1360#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1361 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1362 struct btrfs_extent_ref_v0 *ref0;
1363 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1364 struct btrfs_extent_ref_v0);
1365 num_refs = btrfs_ref_count_v0(leaf, ref0);
1366#endif
1367 } else {
1368 WARN_ON(1);
1369 }
1370 return num_refs;
1371}
1372
1373static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1374 struct btrfs_root *root,
1375 struct btrfs_path *path,
1376 u64 bytenr, u64 parent,
1377 u64 root_objectid)
1378{
1379 struct btrfs_key key;
1380 int ret;
1381
1382 key.objectid = bytenr;
1383 if (parent) {
1384 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1385 key.offset = parent;
1386 } else {
1387 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1388 key.offset = root_objectid;
1389 }
1390
1391 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1392 if (ret > 0)
1393 ret = -ENOENT;
1394#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1395 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001396 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 key.type = BTRFS_EXTENT_REF_V0_KEY;
1398 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1399 if (ret > 0)
1400 ret = -ENOENT;
1401 }
1402#endif
1403 return ret;
1404}
1405
1406static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1407 struct btrfs_root *root,
1408 struct btrfs_path *path,
1409 u64 bytenr, u64 parent,
1410 u64 root_objectid)
1411{
1412 struct btrfs_key key;
1413 int ret;
1414
1415 key.objectid = bytenr;
1416 if (parent) {
1417 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1418 key.offset = parent;
1419 } else {
1420 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1421 key.offset = root_objectid;
1422 }
1423
1424 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001425 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001426 return ret;
1427}
1428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001429static inline int extent_ref_type(u64 parent, u64 owner)
1430{
1431 int type;
1432 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1433 if (parent > 0)
1434 type = BTRFS_SHARED_BLOCK_REF_KEY;
1435 else
1436 type = BTRFS_TREE_BLOCK_REF_KEY;
1437 } else {
1438 if (parent > 0)
1439 type = BTRFS_SHARED_DATA_REF_KEY;
1440 else
1441 type = BTRFS_EXTENT_DATA_REF_KEY;
1442 }
1443 return type;
1444}
1445
Yan Zheng2c47e6052009-06-27 21:07:35 -04001446static int find_next_key(struct btrfs_path *path, int level,
1447 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001448
1449{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001450 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001451 if (!path->nodes[level])
1452 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001453 if (path->slots[level] + 1 >=
1454 btrfs_header_nritems(path->nodes[level]))
1455 continue;
1456 if (level == 0)
1457 btrfs_item_key_to_cpu(path->nodes[level], key,
1458 path->slots[level] + 1);
1459 else
1460 btrfs_node_key_to_cpu(path->nodes[level], key,
1461 path->slots[level] + 1);
1462 return 0;
1463 }
1464 return 1;
1465}
1466
1467/*
1468 * look for inline back ref. if back ref is found, *ref_ret is set
1469 * to the address of inline back ref, and 0 is returned.
1470 *
1471 * if back ref isn't found, *ref_ret is set to the address where it
1472 * should be inserted, and -ENOENT is returned.
1473 *
1474 * if insert is true and there are too many inline back refs, the path
1475 * points to the extent item, and -EAGAIN is returned.
1476 *
1477 * NOTE: inline back refs are ordered in the same way that back ref
1478 * items in the tree are ordered.
1479 */
1480static noinline_for_stack
1481int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root,
1483 struct btrfs_path *path,
1484 struct btrfs_extent_inline_ref **ref_ret,
1485 u64 bytenr, u64 num_bytes,
1486 u64 parent, u64 root_objectid,
1487 u64 owner, u64 offset, int insert)
1488{
1489 struct btrfs_key key;
1490 struct extent_buffer *leaf;
1491 struct btrfs_extent_item *ei;
1492 struct btrfs_extent_inline_ref *iref;
1493 u64 flags;
1494 u64 item_size;
1495 unsigned long ptr;
1496 unsigned long end;
1497 int extra_size;
1498 int type;
1499 int want;
1500 int ret;
1501 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001502 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1503 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001504
1505 key.objectid = bytenr;
1506 key.type = BTRFS_EXTENT_ITEM_KEY;
1507 key.offset = num_bytes;
1508
1509 want = extent_ref_type(parent, owner);
1510 if (insert) {
1511 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001512 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001513 } else
1514 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001515
1516 /*
1517 * Owner is our parent level, so we can just add one to get the level
1518 * for the block we are interested in.
1519 */
1520 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1521 key.type = BTRFS_METADATA_ITEM_KEY;
1522 key.offset = owner;
1523 }
1524
1525again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001526 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1527 if (ret < 0) {
1528 err = ret;
1529 goto out;
1530 }
Josef Bacik3173a182013-03-07 14:22:04 -05001531
1532 /*
1533 * We may be a newly converted file system which still has the old fat
1534 * extent entries for metadata, so try and see if we have one of those.
1535 */
1536 if (ret > 0 && skinny_metadata) {
1537 skinny_metadata = false;
1538 if (path->slots[0]) {
1539 path->slots[0]--;
1540 btrfs_item_key_to_cpu(path->nodes[0], &key,
1541 path->slots[0]);
1542 if (key.objectid == bytenr &&
1543 key.type == BTRFS_EXTENT_ITEM_KEY &&
1544 key.offset == num_bytes)
1545 ret = 0;
1546 }
1547 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001548 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001549 key.type = BTRFS_EXTENT_ITEM_KEY;
1550 key.offset = num_bytes;
1551 btrfs_release_path(path);
1552 goto again;
1553 }
1554 }
1555
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001556 if (ret && !insert) {
1557 err = -ENOENT;
1558 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301559 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001560 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001561 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001562 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001563
1564 leaf = path->nodes[0];
1565 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1566#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1567 if (item_size < sizeof(*ei)) {
1568 if (!insert) {
1569 err = -ENOENT;
1570 goto out;
1571 }
1572 ret = convert_extent_item_v0(trans, root, path, owner,
1573 extra_size);
1574 if (ret < 0) {
1575 err = ret;
1576 goto out;
1577 }
1578 leaf = path->nodes[0];
1579 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1580 }
1581#endif
1582 BUG_ON(item_size < sizeof(*ei));
1583
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001584 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1585 flags = btrfs_extent_flags(leaf, ei);
1586
1587 ptr = (unsigned long)(ei + 1);
1588 end = (unsigned long)ei + item_size;
1589
Josef Bacik3173a182013-03-07 14:22:04 -05001590 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591 ptr += sizeof(struct btrfs_tree_block_info);
1592 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001593 }
1594
1595 err = -ENOENT;
1596 while (1) {
1597 if (ptr >= end) {
1598 WARN_ON(ptr > end);
1599 break;
1600 }
1601 iref = (struct btrfs_extent_inline_ref *)ptr;
1602 type = btrfs_extent_inline_ref_type(leaf, iref);
1603 if (want < type)
1604 break;
1605 if (want > type) {
1606 ptr += btrfs_extent_inline_ref_size(type);
1607 continue;
1608 }
1609
1610 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1611 struct btrfs_extent_data_ref *dref;
1612 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1613 if (match_extent_data_ref(leaf, dref, root_objectid,
1614 owner, offset)) {
1615 err = 0;
1616 break;
1617 }
1618 if (hash_extent_data_ref_item(leaf, dref) <
1619 hash_extent_data_ref(root_objectid, owner, offset))
1620 break;
1621 } else {
1622 u64 ref_offset;
1623 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1624 if (parent > 0) {
1625 if (parent == ref_offset) {
1626 err = 0;
1627 break;
1628 }
1629 if (ref_offset < parent)
1630 break;
1631 } else {
1632 if (root_objectid == ref_offset) {
1633 err = 0;
1634 break;
1635 }
1636 if (ref_offset < root_objectid)
1637 break;
1638 }
1639 }
1640 ptr += btrfs_extent_inline_ref_size(type);
1641 }
1642 if (err == -ENOENT && insert) {
1643 if (item_size + extra_size >=
1644 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1645 err = -EAGAIN;
1646 goto out;
1647 }
1648 /*
1649 * To add new inline back ref, we have to make sure
1650 * there is no corresponding back ref item.
1651 * For simplicity, we just do not add new inline back
1652 * ref if there is any kind of item for this block
1653 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001654 if (find_next_key(path, 0, &key) == 0 &&
1655 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001656 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 err = -EAGAIN;
1658 goto out;
1659 }
1660 }
1661 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1662out:
Yan Zheng85d41982009-06-11 08:51:10 -04001663 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001664 path->keep_locks = 0;
1665 btrfs_unlock_up_safe(path, 1);
1666 }
1667 return err;
1668}
1669
1670/*
1671 * helper to add new inline back ref
1672 */
1673static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001674void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001675 struct btrfs_path *path,
1676 struct btrfs_extent_inline_ref *iref,
1677 u64 parent, u64 root_objectid,
1678 u64 owner, u64 offset, int refs_to_add,
1679 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001680{
1681 struct extent_buffer *leaf;
1682 struct btrfs_extent_item *ei;
1683 unsigned long ptr;
1684 unsigned long end;
1685 unsigned long item_offset;
1686 u64 refs;
1687 int size;
1688 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689
1690 leaf = path->nodes[0];
1691 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1692 item_offset = (unsigned long)iref - (unsigned long)ei;
1693
1694 type = extent_ref_type(parent, owner);
1695 size = btrfs_extent_inline_ref_size(type);
1696
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001697 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698
1699 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1700 refs = btrfs_extent_refs(leaf, ei);
1701 refs += refs_to_add;
1702 btrfs_set_extent_refs(leaf, ei, refs);
1703 if (extent_op)
1704 __run_delayed_extent_op(extent_op, leaf, ei);
1705
1706 ptr = (unsigned long)ei + item_offset;
1707 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1708 if (ptr < end - size)
1709 memmove_extent_buffer(leaf, ptr + size, ptr,
1710 end - size - ptr);
1711
1712 iref = (struct btrfs_extent_inline_ref *)ptr;
1713 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1714 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1715 struct btrfs_extent_data_ref *dref;
1716 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1717 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1718 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1719 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1720 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1721 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1722 struct btrfs_shared_data_ref *sref;
1723 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1724 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1725 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1726 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1727 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1728 } else {
1729 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1730 }
1731 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001732}
1733
1734static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1735 struct btrfs_root *root,
1736 struct btrfs_path *path,
1737 struct btrfs_extent_inline_ref **ref_ret,
1738 u64 bytenr, u64 num_bytes, u64 parent,
1739 u64 root_objectid, u64 owner, u64 offset)
1740{
1741 int ret;
1742
1743 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1744 bytenr, num_bytes, parent,
1745 root_objectid, owner, offset, 0);
1746 if (ret != -ENOENT)
1747 return ret;
1748
David Sterbab3b4aa72011-04-21 01:20:15 +02001749 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001750 *ref_ret = NULL;
1751
1752 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1753 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1754 root_objectid);
1755 } else {
1756 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1757 root_objectid, owner, offset);
1758 }
1759 return ret;
1760}
1761
1762/*
1763 * helper to update/remove inline back ref
1764 */
1765static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001766void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001767 struct btrfs_path *path,
1768 struct btrfs_extent_inline_ref *iref,
1769 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001770 struct btrfs_delayed_extent_op *extent_op,
1771 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001772{
1773 struct extent_buffer *leaf;
1774 struct btrfs_extent_item *ei;
1775 struct btrfs_extent_data_ref *dref = NULL;
1776 struct btrfs_shared_data_ref *sref = NULL;
1777 unsigned long ptr;
1778 unsigned long end;
1779 u32 item_size;
1780 int size;
1781 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001782 u64 refs;
1783
1784 leaf = path->nodes[0];
1785 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1786 refs = btrfs_extent_refs(leaf, ei);
1787 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1788 refs += refs_to_mod;
1789 btrfs_set_extent_refs(leaf, ei, refs);
1790 if (extent_op)
1791 __run_delayed_extent_op(extent_op, leaf, ei);
1792
1793 type = btrfs_extent_inline_ref_type(leaf, iref);
1794
1795 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1796 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1797 refs = btrfs_extent_data_ref_count(leaf, dref);
1798 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1799 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1800 refs = btrfs_shared_data_ref_count(leaf, sref);
1801 } else {
1802 refs = 1;
1803 BUG_ON(refs_to_mod != -1);
1804 }
1805
1806 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1807 refs += refs_to_mod;
1808
1809 if (refs > 0) {
1810 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1811 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1812 else
1813 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1814 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001815 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001816 size = btrfs_extent_inline_ref_size(type);
1817 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1818 ptr = (unsigned long)iref;
1819 end = (unsigned long)ei + item_size;
1820 if (ptr + size < end)
1821 memmove_extent_buffer(leaf, ptr, ptr + size,
1822 end - ptr - size);
1823 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001824 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825 }
1826 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001827}
1828
1829static noinline_for_stack
1830int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1831 struct btrfs_root *root,
1832 struct btrfs_path *path,
1833 u64 bytenr, u64 num_bytes, u64 parent,
1834 u64 root_objectid, u64 owner,
1835 u64 offset, int refs_to_add,
1836 struct btrfs_delayed_extent_op *extent_op)
1837{
1838 struct btrfs_extent_inline_ref *iref;
1839 int ret;
1840
1841 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1842 bytenr, num_bytes, parent,
1843 root_objectid, owner, offset, 1);
1844 if (ret == 0) {
1845 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001846 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001847 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001848 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001849 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001850 root_objectid, owner, offset,
1851 refs_to_add, extent_op);
1852 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001853 }
1854 return ret;
1855}
1856
1857static int insert_extent_backref(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root,
1859 struct btrfs_path *path,
1860 u64 bytenr, u64 parent, u64 root_objectid,
1861 u64 owner, u64 offset, int refs_to_add)
1862{
1863 int ret;
1864 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1865 BUG_ON(refs_to_add != 1);
1866 ret = insert_tree_block_ref(trans, root, path, bytenr,
1867 parent, root_objectid);
1868 } else {
1869 ret = insert_extent_data_ref(trans, root, path, bytenr,
1870 parent, root_objectid,
1871 owner, offset, refs_to_add);
1872 }
1873 return ret;
1874}
1875
1876static int remove_extent_backref(struct btrfs_trans_handle *trans,
1877 struct btrfs_root *root,
1878 struct btrfs_path *path,
1879 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001880 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001882 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883
1884 BUG_ON(!is_data && refs_to_drop != 1);
1885 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001886 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001887 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001888 } else if (is_data) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001889 ret = remove_extent_data_ref(trans, root, path, refs_to_drop,
1890 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001892 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 ret = btrfs_del_item(trans, root, path);
1894 }
1895 return ret;
1896}
1897
Li Dongyang5378e602011-03-24 10:24:27 +00001898static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001899 u64 start, u64 len)
1900{
Li Dongyang5378e602011-03-24 10:24:27 +00001901 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001902}
Chris Mason15916de2008-11-19 21:17:22 -05001903
Liu Hui1f3c79a2009-01-05 15:57:51 -05001904static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001905 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001906{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001907 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001908 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001909 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001910
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001911
Liu Hui1f3c79a2009-01-05 15:57:51 -05001912 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001913 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001914 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001915 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001916 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001917 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001918 int i;
1919
Liu Hui1f3c79a2009-01-05 15:57:51 -05001920
Jan Schmidta1d3c472011-08-04 17:15:33 +02001921 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001922 if (!stripe->dev->can_discard)
1923 continue;
1924
Li Dongyang5378e602011-03-24 10:24:27 +00001925 ret = btrfs_issue_discard(stripe->dev->bdev,
1926 stripe->physical,
1927 stripe->length);
1928 if (!ret)
1929 discarded_bytes += stripe->length;
1930 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001932
1933 /*
1934 * Just in case we get back EOPNOTSUPP for some reason,
1935 * just ignore the return value so we don't screw up
1936 * people calling discard_extent.
1937 */
1938 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001939 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001940 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001941 }
Li Dongyang5378e602011-03-24 10:24:27 +00001942
1943 if (actual_bytes)
1944 *actual_bytes = discarded_bytes;
1945
Liu Hui1f3c79a2009-01-05 15:57:51 -05001946
David Woodhouse53b381b2013-01-29 18:40:14 -05001947 if (ret == -EOPNOTSUPP)
1948 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001949 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001950}
1951
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001952/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001953int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1954 struct btrfs_root *root,
1955 u64 bytenr, u64 num_bytes, u64 parent,
Josef Bacikfcebe452014-05-13 17:30:47 -07001956 u64 root_objectid, u64 owner, u64 offset,
1957 int no_quota)
Zheng Yan31840ae2008-09-23 13:14:14 -04001958{
1959 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001960 struct btrfs_fs_info *fs_info = root->fs_info;
1961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001962 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1963 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001964
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001965 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001966 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1967 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001968 parent, root_objectid, (int)owner,
Josef Bacikfcebe452014-05-13 17:30:47 -07001969 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001970 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001971 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1972 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001973 parent, root_objectid, owner, offset,
Josef Bacikfcebe452014-05-13 17:30:47 -07001974 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001975 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001976 return ret;
1977}
1978
Chris Mason925baed2008-06-25 16:01:30 -04001979static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001980 struct btrfs_root *root,
1981 u64 bytenr, u64 num_bytes,
1982 u64 parent, u64 root_objectid,
1983 u64 owner, u64 offset, int refs_to_add,
Josef Bacikfcebe452014-05-13 17:30:47 -07001984 int no_quota,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001985 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001986{
Josef Bacikfcebe452014-05-13 17:30:47 -07001987 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04001988 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001989 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001990 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001991 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001992 u64 refs;
1993 int ret;
Josef Bacikfcebe452014-05-13 17:30:47 -07001994 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_ADD_EXCL;
Chris Mason037e6392007-03-07 11:50:24 -05001995
Chris Mason5caf2a02007-04-02 11:20:42 -04001996 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001997 if (!path)
1998 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001999
Josef Bacikfcebe452014-05-13 17:30:47 -07002000 if (!is_fstree(root_objectid) || !root->fs_info->quota_enabled)
2001 no_quota = 1;
2002
Chris Mason3c12ac72008-04-21 12:01:38 -04002003 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002004 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002005 /* this will setup the path even if it fails to insert the back ref */
Josef Bacikfcebe452014-05-13 17:30:47 -07002006 ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
2007 bytenr, num_bytes, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008 root_objectid, owner, offset,
2009 refs_to_add, extent_op);
Josef Bacikfcebe452014-05-13 17:30:47 -07002010 if ((ret < 0 && ret != -EAGAIN) || (!ret && no_quota))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002011 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002012 /*
2013 * Ok we were able to insert an inline extent and it appears to be a new
2014 * reference, deal with the qgroup accounting.
2015 */
2016 if (!ret && !no_quota) {
2017 ASSERT(root->fs_info->quota_enabled);
2018 leaf = path->nodes[0];
2019 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2020 item = btrfs_item_ptr(leaf, path->slots[0],
2021 struct btrfs_extent_item);
2022 if (btrfs_extent_refs(leaf, item) > (u64)refs_to_add)
2023 type = BTRFS_QGROUP_OPER_ADD_SHARED;
2024 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002025
Josef Bacikfcebe452014-05-13 17:30:47 -07002026 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2027 bytenr, num_bytes, type, 0);
2028 goto out;
2029 }
2030
2031 /*
2032 * Ok we had -EAGAIN which means we didn't have space to insert and
2033 * inline extent ref, so just update the reference count and add a
2034 * normal backref.
2035 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002036 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002037 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002038 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2039 refs = btrfs_extent_refs(leaf, item);
Josef Bacikfcebe452014-05-13 17:30:47 -07002040 if (refs)
2041 type = BTRFS_QGROUP_OPER_ADD_SHARED;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002042 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2043 if (extent_op)
2044 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002045
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002046 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002047 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002048
Josef Bacikfcebe452014-05-13 17:30:47 -07002049 if (!no_quota) {
2050 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2051 bytenr, num_bytes, type, 0);
2052 if (ret)
2053 goto out;
2054 }
2055
Chris Mason3c12ac72008-04-21 12:01:38 -04002056 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002057 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002058 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002059 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002060 path, bytenr, parent, root_objectid,
2061 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002062 if (ret)
2063 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064out:
Chris Mason74493f72007-12-11 09:25:06 -05002065 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002066 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002067}
2068
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002069static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2070 struct btrfs_root *root,
2071 struct btrfs_delayed_ref_node *node,
2072 struct btrfs_delayed_extent_op *extent_op,
2073 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002074{
Chris Mason56bec292009-03-13 10:10:06 -04002075 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 struct btrfs_delayed_data_ref *ref;
2077 struct btrfs_key ins;
2078 u64 parent = 0;
2079 u64 ref_root = 0;
2080 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002082 ins.objectid = node->bytenr;
2083 ins.offset = node->num_bytes;
2084 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002085
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 ref = btrfs_delayed_node_to_data_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002087 trace_run_delayed_data_ref(node, ref, node->action);
2088
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002089 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2090 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002091 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002092
2093 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002094 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002095 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096 ret = alloc_reserved_file_extent(trans, root,
2097 parent, ref_root, flags,
2098 ref->objectid, ref->offset,
2099 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2101 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2102 node->num_bytes, parent,
2103 ref_root, ref->objectid,
2104 ref->offset, node->ref_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07002105 node->no_quota, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002106 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2107 ret = __btrfs_free_extent(trans, root, node->bytenr,
2108 node->num_bytes, parent,
2109 ref_root, ref->objectid,
2110 ref->offset, node->ref_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07002111 extent_op, node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002112 } else {
2113 BUG();
2114 }
Chris Mason56bec292009-03-13 10:10:06 -04002115 return ret;
2116}
2117
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002118static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2119 struct extent_buffer *leaf,
2120 struct btrfs_extent_item *ei)
2121{
2122 u64 flags = btrfs_extent_flags(leaf, ei);
2123 if (extent_op->update_flags) {
2124 flags |= extent_op->flags_to_set;
2125 btrfs_set_extent_flags(leaf, ei, flags);
2126 }
2127
2128 if (extent_op->update_key) {
2129 struct btrfs_tree_block_info *bi;
2130 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2131 bi = (struct btrfs_tree_block_info *)(ei + 1);
2132 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2133 }
2134}
2135
2136static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2137 struct btrfs_root *root,
2138 struct btrfs_delayed_ref_node *node,
2139 struct btrfs_delayed_extent_op *extent_op)
2140{
2141 struct btrfs_key key;
2142 struct btrfs_path *path;
2143 struct btrfs_extent_item *ei;
2144 struct extent_buffer *leaf;
2145 u32 item_size;
2146 int ret;
2147 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002148 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002149
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002150 if (trans->aborted)
2151 return 0;
2152
Josef Bacik3173a182013-03-07 14:22:04 -05002153 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2154 metadata = 0;
2155
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002156 path = btrfs_alloc_path();
2157 if (!path)
2158 return -ENOMEM;
2159
2160 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002161
Josef Bacik3173a182013-03-07 14:22:04 -05002162 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002163 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002164 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002165 } else {
2166 key.type = BTRFS_EXTENT_ITEM_KEY;
2167 key.offset = node->num_bytes;
2168 }
2169
2170again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002171 path->reada = 1;
2172 path->leave_spinning = 1;
2173 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2174 path, 0, 1);
2175 if (ret < 0) {
2176 err = ret;
2177 goto out;
2178 }
2179 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002180 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002181 if (path->slots[0] > 0) {
2182 path->slots[0]--;
2183 btrfs_item_key_to_cpu(path->nodes[0], &key,
2184 path->slots[0]);
2185 if (key.objectid == node->bytenr &&
2186 key.type == BTRFS_EXTENT_ITEM_KEY &&
2187 key.offset == node->num_bytes)
2188 ret = 0;
2189 }
2190 if (ret > 0) {
2191 btrfs_release_path(path);
2192 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002193
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002194 key.objectid = node->bytenr;
2195 key.offset = node->num_bytes;
2196 key.type = BTRFS_EXTENT_ITEM_KEY;
2197 goto again;
2198 }
2199 } else {
2200 err = -EIO;
2201 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002202 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002203 }
2204
2205 leaf = path->nodes[0];
2206 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2207#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2208 if (item_size < sizeof(*ei)) {
2209 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2210 path, (u64)-1, 0);
2211 if (ret < 0) {
2212 err = ret;
2213 goto out;
2214 }
2215 leaf = path->nodes[0];
2216 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2217 }
2218#endif
2219 BUG_ON(item_size < sizeof(*ei));
2220 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2221 __run_delayed_extent_op(extent_op, leaf, ei);
2222
2223 btrfs_mark_buffer_dirty(leaf);
2224out:
2225 btrfs_free_path(path);
2226 return err;
2227}
2228
2229static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2230 struct btrfs_root *root,
2231 struct btrfs_delayed_ref_node *node,
2232 struct btrfs_delayed_extent_op *extent_op,
2233 int insert_reserved)
2234{
2235 int ret = 0;
2236 struct btrfs_delayed_tree_ref *ref;
2237 struct btrfs_key ins;
2238 u64 parent = 0;
2239 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002240 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2241 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242
2243 ref = btrfs_delayed_node_to_tree_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002244 trace_run_delayed_tree_ref(node, ref, node->action);
2245
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2247 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002248 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002249
Josef Bacik3173a182013-03-07 14:22:04 -05002250 ins.objectid = node->bytenr;
2251 if (skinny_metadata) {
2252 ins.offset = ref->level;
2253 ins.type = BTRFS_METADATA_ITEM_KEY;
2254 } else {
2255 ins.offset = node->num_bytes;
2256 ins.type = BTRFS_EXTENT_ITEM_KEY;
2257 }
2258
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002259 BUG_ON(node->ref_mod != 1);
2260 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002261 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002262 ret = alloc_reserved_tree_block(trans, root,
2263 parent, ref_root,
2264 extent_op->flags_to_set,
2265 &extent_op->key,
Josef Bacikfcebe452014-05-13 17:30:47 -07002266 ref->level, &ins,
2267 node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2269 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2270 node->num_bytes, parent, ref_root,
Josef Bacikfcebe452014-05-13 17:30:47 -07002271 ref->level, 0, 1, node->no_quota,
2272 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002273 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2274 ret = __btrfs_free_extent(trans, root, node->bytenr,
2275 node->num_bytes, parent, ref_root,
Josef Bacikfcebe452014-05-13 17:30:47 -07002276 ref->level, 0, 1, extent_op,
2277 node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002278 } else {
2279 BUG();
2280 }
2281 return ret;
2282}
2283
Chris Mason56bec292009-03-13 10:10:06 -04002284/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002285static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2286 struct btrfs_root *root,
2287 struct btrfs_delayed_ref_node *node,
2288 struct btrfs_delayed_extent_op *extent_op,
2289 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002290{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291 int ret = 0;
2292
Josef Bacik857cc2f2013-10-07 15:21:08 -04002293 if (trans->aborted) {
2294 if (insert_reserved)
2295 btrfs_pin_extent(root, node->bytenr,
2296 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002297 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002298 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002299
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002300 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002301 struct btrfs_delayed_ref_head *head;
2302 /*
2303 * we've hit the end of the chain and we were supposed
2304 * to insert this extent into the tree. But, it got
2305 * deleted before we ever needed to insert it, so all
2306 * we have to do is clean up the accounting
2307 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002308 BUG_ON(extent_op);
2309 head = btrfs_delayed_node_to_head(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002310 trace_run_delayed_ref_head(node, head, node->action);
2311
Chris Mason56bec292009-03-13 10:10:06 -04002312 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002313 btrfs_pin_extent(root, node->bytenr,
2314 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002315 if (head->is_data) {
2316 ret = btrfs_del_csums(trans, root,
2317 node->bytenr,
2318 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002319 }
Chris Mason56bec292009-03-13 10:10:06 -04002320 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002321 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002322 }
Josef Bacikeb099672009-02-12 09:27:38 -05002323
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002324 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2325 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2326 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2327 insert_reserved);
2328 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2329 node->type == BTRFS_SHARED_DATA_REF_KEY)
2330 ret = run_delayed_data_ref(trans, root, node, extent_op,
2331 insert_reserved);
2332 else
2333 BUG();
2334 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002335}
2336
Chris Mason56bec292009-03-13 10:10:06 -04002337static noinline struct btrfs_delayed_ref_node *
2338select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002339{
Chris Mason56bec292009-03-13 10:10:06 -04002340 struct rb_node *node;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002341 struct btrfs_delayed_ref_node *ref, *last = NULL;;
2342
Chris Mason56bec292009-03-13 10:10:06 -04002343 /*
2344 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2345 * this prevents ref count from going down to zero when
2346 * there still are pending delayed ref.
2347 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002348 node = rb_first(&head->ref_root);
2349 while (node) {
Chris Mason56bec292009-03-13 10:10:06 -04002350 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2351 rb_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002352 if (ref->action == BTRFS_ADD_DELAYED_REF)
Chris Mason56bec292009-03-13 10:10:06 -04002353 return ref;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002354 else if (last == NULL)
2355 last = ref;
2356 node = rb_next(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002357 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002358 return last;
Chris Mason56bec292009-03-13 10:10:06 -04002359}
2360
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002361/*
2362 * Returns 0 on success or if called with an already aborted transaction.
2363 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2364 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002365static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2366 struct btrfs_root *root,
2367 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002368{
Chris Mason56bec292009-03-13 10:10:06 -04002369 struct btrfs_delayed_ref_root *delayed_refs;
2370 struct btrfs_delayed_ref_node *ref;
2371 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002372 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002373 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002374 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002375 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002376 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002377 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002378 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002379
2380 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002381 while (1) {
2382 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002383 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002384 break;
Chris Mason56bec292009-03-13 10:10:06 -04002385
Josef Bacikd7df2c72014-01-23 09:21:38 -05002386 spin_lock(&delayed_refs->lock);
2387 locked_ref = btrfs_select_ref_head(trans);
2388 if (!locked_ref) {
2389 spin_unlock(&delayed_refs->lock);
2390 break;
2391 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002392
2393 /* grab the lock that says we are going to process
2394 * all the refs for this head */
2395 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002396 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002397 /*
2398 * we may have dropped the spin lock to get the head
2399 * mutex lock, and that might have given someone else
2400 * time to free the head. If that's true, it has been
2401 * removed from our list and we can move on.
2402 */
2403 if (ret == -EAGAIN) {
2404 locked_ref = NULL;
2405 count++;
2406 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002407 }
2408 }
2409
2410 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002411 * We need to try and merge add/drops of the same ref since we
2412 * can run into issues with relocate dropping the implicit ref
2413 * and then it being added back again before the drop can
2414 * finish. If we merged anything we need to re-loop so we can
2415 * get a good ref.
2416 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002417 spin_lock(&locked_ref->lock);
Josef Bacikae1e2062012-08-07 16:00:32 -04002418 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2419 locked_ref);
2420
2421 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002422 * locked_ref is the head node, so we have to go one
2423 * node back for any delayed ref updates
2424 */
2425 ref = select_delayed_ref(locked_ref);
2426
2427 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002428 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002429 spin_unlock(&locked_ref->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002430 btrfs_delayed_ref_unlock(locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002431 spin_lock(&delayed_refs->lock);
2432 locked_ref->processing = 0;
Arne Jansend1270cd2011-09-13 15:16:43 +02002433 delayed_refs->num_heads_ready++;
2434 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002435 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002436 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002437 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002438 continue;
2439 }
2440
2441 /*
Chris Mason56bec292009-03-13 10:10:06 -04002442 * record the must insert reserved flag before we
2443 * drop the spin lock.
2444 */
2445 must_insert_reserved = locked_ref->must_insert_reserved;
2446 locked_ref->must_insert_reserved = 0;
2447
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002448 extent_op = locked_ref->extent_op;
2449 locked_ref->extent_op = NULL;
2450
Chris Mason56bec292009-03-13 10:10:06 -04002451 if (!ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002452
2453
Chris Mason56bec292009-03-13 10:10:06 -04002454 /* All delayed refs have been processed, Go ahead
2455 * and send the head node to run_one_delayed_ref,
2456 * so that any accounting fixes can happen
2457 */
2458 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459
2460 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002461 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002462 extent_op = NULL;
2463 }
2464
2465 if (extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002466 spin_unlock(&locked_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002467 ret = run_delayed_extent_op(trans, root,
2468 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002469 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002471 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002472 /*
2473 * Need to reset must_insert_reserved if
2474 * there was an error so the abort stuff
2475 * can cleanup the reserved space
2476 * properly.
2477 */
2478 if (must_insert_reserved)
2479 locked_ref->must_insert_reserved = 1;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002480 locked_ref->processing = 0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002481 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Miao Xie093486c2012-12-19 08:10:10 +00002482 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002483 return ret;
2484 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002485 continue;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 }
Chris Mason56bec292009-03-13 10:10:06 -04002487
Josef Bacikd7df2c72014-01-23 09:21:38 -05002488 /*
2489 * Need to drop our head ref lock and re-aqcuire the
2490 * delayed ref lock and then re-check to make sure
2491 * nobody got added.
2492 */
2493 spin_unlock(&locked_ref->lock);
2494 spin_lock(&delayed_refs->lock);
2495 spin_lock(&locked_ref->lock);
Josef Bacik573a0752014-03-27 19:41:34 -04002496 if (rb_first(&locked_ref->ref_root) ||
2497 locked_ref->extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002498 spin_unlock(&locked_ref->lock);
2499 spin_unlock(&delayed_refs->lock);
2500 continue;
2501 }
2502 ref->in_tree = 0;
2503 delayed_refs->num_heads--;
Liu Boc46effa2013-10-14 12:59:45 +08002504 rb_erase(&locked_ref->href_node,
2505 &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002506 spin_unlock(&delayed_refs->lock);
2507 } else {
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002508 actual_count++;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002509 ref->in_tree = 0;
2510 rb_erase(&ref->rb_node, &locked_ref->ref_root);
Liu Boc46effa2013-10-14 12:59:45 +08002511 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002512 atomic_dec(&delayed_refs->num_entries);
2513
Miao Xie093486c2012-12-19 08:10:10 +00002514 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002515 /*
2516 * when we play the delayed ref, also correct the
2517 * ref_mod on head
2518 */
2519 switch (ref->action) {
2520 case BTRFS_ADD_DELAYED_REF:
2521 case BTRFS_ADD_DELAYED_EXTENT:
2522 locked_ref->node.ref_mod -= ref->ref_mod;
2523 break;
2524 case BTRFS_DROP_DELAYED_REF:
2525 locked_ref->node.ref_mod += ref->ref_mod;
2526 break;
2527 default:
2528 WARN_ON(1);
2529 }
2530 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002531 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002532
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002533 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002534 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002535
Miao Xie78a61842012-11-21 02:21:28 +00002536 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002537 if (ret) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002538 locked_ref->processing = 0;
Miao Xie093486c2012-12-19 08:10:10 +00002539 btrfs_delayed_ref_unlock(locked_ref);
2540 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002541 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002542 return ret;
2543 }
2544
Miao Xie093486c2012-12-19 08:10:10 +00002545 /*
2546 * If this node is a head, that means all the refs in this head
2547 * have been dealt with, and we will pick the next head to deal
2548 * with, so we must unlock the head and drop it from the cluster
2549 * list before we release it.
2550 */
2551 if (btrfs_delayed_ref_is_head(ref)) {
Miao Xie093486c2012-12-19 08:10:10 +00002552 btrfs_delayed_ref_unlock(locked_ref);
2553 locked_ref = NULL;
2554 }
2555 btrfs_put_delayed_ref(ref);
2556 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002557 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002558 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002559
2560 /*
2561 * We don't want to include ref heads since we can have empty ref heads
2562 * and those will drastically skew our runtime down since we just do
2563 * accounting, no actual extent tree updates.
2564 */
2565 if (actual_count > 0) {
2566 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2567 u64 avg;
2568
2569 /*
2570 * We weigh the current average higher than our current runtime
2571 * to avoid large swings in the average.
2572 */
2573 spin_lock(&delayed_refs->lock);
2574 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2575 avg = div64_u64(avg, 4);
2576 fs_info->avg_delayed_ref_runtime = avg;
2577 spin_unlock(&delayed_refs->lock);
2578 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002579 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002580}
2581
Arne Jansen709c0482011-09-12 12:22:57 +02002582#ifdef SCRAMBLE_DELAYED_REFS
2583/*
2584 * Normally delayed refs get processed in ascending bytenr order. This
2585 * correlates in most cases to the order added. To expose dependencies on this
2586 * order, we start to process the tree in the middle instead of the beginning
2587 */
2588static u64 find_middle(struct rb_root *root)
2589{
2590 struct rb_node *n = root->rb_node;
2591 struct btrfs_delayed_ref_node *entry;
2592 int alt = 1;
2593 u64 middle;
2594 u64 first = 0, last = 0;
2595
2596 n = rb_first(root);
2597 if (n) {
2598 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2599 first = entry->bytenr;
2600 }
2601 n = rb_last(root);
2602 if (n) {
2603 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2604 last = entry->bytenr;
2605 }
2606 n = root->rb_node;
2607
2608 while (n) {
2609 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2610 WARN_ON(!entry->in_tree);
2611
2612 middle = entry->bytenr;
2613
2614 if (alt)
2615 n = n->rb_left;
2616 else
2617 n = n->rb_right;
2618
2619 alt = 1 - alt;
2620 }
2621 return middle;
2622}
2623#endif
2624
Josef Bacik1be41b72013-06-12 13:56:06 -04002625static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2626{
2627 u64 num_bytes;
2628
2629 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2630 sizeof(struct btrfs_extent_inline_ref));
2631 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2632 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2633
2634 /*
2635 * We don't ever fill up leaves all the way so multiply by 2 just to be
2636 * closer to what we're really going to want to ouse.
2637 */
2638 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2639}
2640
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002641int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacik1be41b72013-06-12 13:56:06 -04002642 struct btrfs_root *root)
2643{
2644 struct btrfs_block_rsv *global_rsv;
2645 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2646 u64 num_bytes;
2647 int ret = 0;
2648
2649 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2650 num_heads = heads_to_leaves(root, num_heads);
2651 if (num_heads > 1)
2652 num_bytes += (num_heads - 1) * root->leafsize;
2653 num_bytes <<= 1;
2654 global_rsv = &root->fs_info->global_block_rsv;
2655
2656 /*
2657 * If we can't allocate any more chunks lets make sure we have _lots_ of
2658 * wiggle room since running delayed refs can create more delayed refs.
2659 */
2660 if (global_rsv->space_info->full)
2661 num_bytes <<= 1;
2662
2663 spin_lock(&global_rsv->lock);
2664 if (global_rsv->reserved <= num_bytes)
2665 ret = 1;
2666 spin_unlock(&global_rsv->lock);
2667 return ret;
2668}
2669
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002670int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2671 struct btrfs_root *root)
2672{
2673 struct btrfs_fs_info *fs_info = root->fs_info;
2674 u64 num_entries =
2675 atomic_read(&trans->transaction->delayed_refs.num_entries);
2676 u64 avg_runtime;
2677
2678 smp_mb();
2679 avg_runtime = fs_info->avg_delayed_ref_runtime;
2680 if (num_entries * avg_runtime >= NSEC_PER_SEC)
2681 return 1;
2682
2683 return btrfs_check_space_for_delayed_refs(trans, root);
2684}
2685
Chris Masonc3e69d52009-03-13 10:17:05 -04002686/*
2687 * this starts processing the delayed reference count updates and
2688 * extent insertions we have queued up so far. count can be
2689 * 0, which means to process everything in the tree at the start
2690 * of the run (but not newly added entries), or it can be some target
2691 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002692 *
2693 * Returns 0 on success or if called with an aborted transaction
2694 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002695 */
2696int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2697 struct btrfs_root *root, unsigned long count)
2698{
2699 struct rb_node *node;
2700 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002701 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002702 int ret;
2703 int run_all = count == (unsigned long)-1;
2704 int run_most = 0;
2705
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002706 /* We'll clean this up in btrfs_cleanup_transaction */
2707 if (trans->aborted)
2708 return 0;
2709
Chris Masonc3e69d52009-03-13 10:17:05 -04002710 if (root == root->fs_info->extent_root)
2711 root = root->fs_info->tree_root;
2712
2713 delayed_refs = &trans->transaction->delayed_refs;
Chris Masonbb721702013-01-29 18:44:12 -05002714 if (count == 0) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002715 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002716 run_most = 1;
2717 }
2718
Chris Masonc3e69d52009-03-13 10:17:05 -04002719again:
Arne Jansen709c0482011-09-12 12:22:57 +02002720#ifdef SCRAMBLE_DELAYED_REFS
2721 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2722#endif
Josef Bacikd7df2c72014-01-23 09:21:38 -05002723 ret = __btrfs_run_delayed_refs(trans, root, count);
2724 if (ret < 0) {
2725 btrfs_abort_transaction(trans, root, ret);
2726 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002727 }
2728
Chris Mason56bec292009-03-13 10:10:06 -04002729 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002730 if (!list_empty(&trans->new_bgs))
Josef Bacikea658ba2012-09-11 16:57:25 -04002731 btrfs_create_pending_block_groups(trans, root);
Josef Bacikea658ba2012-09-11 16:57:25 -04002732
Josef Bacikd7df2c72014-01-23 09:21:38 -05002733 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08002734 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002735 if (!node) {
2736 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002737 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002738 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002739 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002740
2741 while (node) {
Liu Boc46effa2013-10-14 12:59:45 +08002742 head = rb_entry(node, struct btrfs_delayed_ref_head,
2743 href_node);
2744 if (btrfs_delayed_ref_is_head(&head->node)) {
2745 struct btrfs_delayed_ref_node *ref;
Chris Mason56bec292009-03-13 10:10:06 -04002746
Liu Boc46effa2013-10-14 12:59:45 +08002747 ref = &head->node;
Chris Mason56bec292009-03-13 10:10:06 -04002748 atomic_inc(&ref->refs);
2749
2750 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002751 /*
2752 * Mutex was contended, block until it's
2753 * released and try again
2754 */
Chris Mason56bec292009-03-13 10:10:06 -04002755 mutex_lock(&head->mutex);
2756 mutex_unlock(&head->mutex);
2757
2758 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002759 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002760 goto again;
Liu Boc46effa2013-10-14 12:59:45 +08002761 } else {
2762 WARN_ON(1);
Chris Mason56bec292009-03-13 10:10:06 -04002763 }
2764 node = rb_next(node);
2765 }
2766 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002767 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002768 goto again;
2769 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002770out:
Josef Bacikfcebe452014-05-13 17:30:47 -07002771 ret = btrfs_delayed_qgroup_accounting(trans, root->fs_info);
2772 if (ret)
2773 return ret;
Jan Schmidtedf39272012-06-28 18:04:55 +02002774 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002775 return 0;
2776}
2777
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002778int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root,
2780 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002781 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002782{
2783 struct btrfs_delayed_extent_op *extent_op;
2784 int ret;
2785
Miao Xie78a61842012-11-21 02:21:28 +00002786 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002787 if (!extent_op)
2788 return -ENOMEM;
2789
2790 extent_op->flags_to_set = flags;
2791 extent_op->update_flags = 1;
2792 extent_op->update_key = 0;
2793 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002794 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002795
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002796 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2797 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002798 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002799 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002800 return ret;
2801}
2802
2803static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2804 struct btrfs_root *root,
2805 struct btrfs_path *path,
2806 u64 objectid, u64 offset, u64 bytenr)
2807{
2808 struct btrfs_delayed_ref_head *head;
2809 struct btrfs_delayed_ref_node *ref;
2810 struct btrfs_delayed_data_ref *data_ref;
2811 struct btrfs_delayed_ref_root *delayed_refs;
2812 struct rb_node *node;
2813 int ret = 0;
2814
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002815 delayed_refs = &trans->transaction->delayed_refs;
2816 spin_lock(&delayed_refs->lock);
2817 head = btrfs_find_delayed_ref_head(trans, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002818 if (!head) {
2819 spin_unlock(&delayed_refs->lock);
2820 return 0;
2821 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002822
2823 if (!mutex_trylock(&head->mutex)) {
2824 atomic_inc(&head->node.refs);
2825 spin_unlock(&delayed_refs->lock);
2826
David Sterbab3b4aa72011-04-21 01:20:15 +02002827 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002828
David Sterba8cc33e52011-05-02 15:29:25 +02002829 /*
2830 * Mutex was contended, block until it's released and let
2831 * caller try again
2832 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002833 mutex_lock(&head->mutex);
2834 mutex_unlock(&head->mutex);
2835 btrfs_put_delayed_ref(&head->node);
2836 return -EAGAIN;
2837 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002838 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002839
2840 spin_lock(&head->lock);
2841 node = rb_first(&head->ref_root);
2842 while (node) {
2843 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2844 node = rb_next(node);
2845
2846 /* If it's a shared ref we know a cross reference exists */
2847 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2848 ret = 1;
2849 break;
2850 }
2851
2852 data_ref = btrfs_delayed_node_to_data_ref(ref);
2853
2854 /*
2855 * If our ref doesn't match the one we're currently looking at
2856 * then we have a cross reference.
2857 */
2858 if (data_ref->root != root->root_key.objectid ||
2859 data_ref->objectid != objectid ||
2860 data_ref->offset != offset) {
2861 ret = 1;
2862 break;
2863 }
2864 }
2865 spin_unlock(&head->lock);
2866 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002867 return ret;
2868}
2869
2870static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2871 struct btrfs_root *root,
2872 struct btrfs_path *path,
2873 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002874{
2875 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002876 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002877 struct btrfs_extent_data_ref *ref;
2878 struct btrfs_extent_inline_ref *iref;
2879 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002880 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002881 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002882 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002883
Chris Masonbe20aa92007-12-17 20:14:01 -05002884 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002885 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002886 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002887
Chris Masonbe20aa92007-12-17 20:14:01 -05002888 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2889 if (ret < 0)
2890 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002891 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002892
2893 ret = -ENOENT;
2894 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002895 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002896
Zheng Yan31840ae2008-09-23 13:14:14 -04002897 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002898 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002899 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002901 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002902 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002903
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002904 ret = 1;
2905 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2906#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2907 if (item_size < sizeof(*ei)) {
2908 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2909 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002910 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002911#endif
2912 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2913
2914 if (item_size != sizeof(*ei) +
2915 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2916 goto out;
2917
2918 if (btrfs_extent_generation(leaf, ei) <=
2919 btrfs_root_last_snapshot(&root->root_item))
2920 goto out;
2921
2922 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2923 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2924 BTRFS_EXTENT_DATA_REF_KEY)
2925 goto out;
2926
2927 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2928 if (btrfs_extent_refs(leaf, ei) !=
2929 btrfs_extent_data_ref_count(leaf, ref) ||
2930 btrfs_extent_data_ref_root(leaf, ref) !=
2931 root->root_key.objectid ||
2932 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2933 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2934 goto out;
2935
Yan Zhengf321e492008-07-30 09:26:11 -04002936 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002937out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002938 return ret;
2939}
2940
2941int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2942 struct btrfs_root *root,
2943 u64 objectid, u64 offset, u64 bytenr)
2944{
2945 struct btrfs_path *path;
2946 int ret;
2947 int ret2;
2948
2949 path = btrfs_alloc_path();
2950 if (!path)
2951 return -ENOENT;
2952
2953 do {
2954 ret = check_committed_ref(trans, root, path, objectid,
2955 offset, bytenr);
2956 if (ret && ret != -ENOENT)
2957 goto out;
2958
2959 ret2 = check_delayed_ref(trans, root, path, objectid,
2960 offset, bytenr);
2961 } while (ret2 == -EAGAIN);
2962
2963 if (ret2 && ret2 != -ENOENT) {
2964 ret = ret2;
2965 goto out;
2966 }
2967
2968 if (ret != -ENOENT || ret2 != -ENOENT)
2969 ret = 0;
2970out:
Yan Zhengf321e492008-07-30 09:26:11 -04002971 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002972 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2973 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002974 return ret;
2975}
2976
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002977static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002978 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002979 struct extent_buffer *buf,
Josef Bacikfcebe452014-05-13 17:30:47 -07002980 int full_backref, int inc, int no_quota)
Zheng Yan31840ae2008-09-23 13:14:14 -04002981{
2982 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002983 u64 num_bytes;
2984 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002985 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002986 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002987 struct btrfs_key key;
2988 struct btrfs_file_extent_item *fi;
2989 int i;
2990 int level;
2991 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002992 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002993 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002994
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002995#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
2996 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
2997 return 0;
2998#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04002999 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003000 nritems = btrfs_header_nritems(buf);
3001 level = btrfs_header_level(buf);
3002
Miao Xie27cdeb72014-04-02 19:51:05 +08003003 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003004 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003005
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003006 if (inc)
3007 process_func = btrfs_inc_extent_ref;
3008 else
3009 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003010
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003011 if (full_backref)
3012 parent = buf->start;
3013 else
3014 parent = 0;
3015
Zheng Yan31840ae2008-09-23 13:14:14 -04003016 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003017 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003018 btrfs_item_key_to_cpu(buf, &key, i);
3019 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003020 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003021 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003022 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003023 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003024 BTRFS_FILE_EXTENT_INLINE)
3025 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003026 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3027 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003028 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003029
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003030 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3031 key.offset -= btrfs_file_extent_offset(buf, fi);
3032 ret = process_func(trans, root, bytenr, num_bytes,
3033 parent, ref_root, key.objectid,
Josef Bacikfcebe452014-05-13 17:30:47 -07003034 key.offset, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035 if (ret)
3036 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003037 } else {
3038 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003039 num_bytes = btrfs_level_size(root, level - 1);
3040 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003041 parent, ref_root, level - 1, 0,
Josef Bacikfcebe452014-05-13 17:30:47 -07003042 no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003043 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003044 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003045 }
3046 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003047 return 0;
3048fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003049 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003050}
3051
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003052int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacikfcebe452014-05-13 17:30:47 -07003053 struct extent_buffer *buf, int full_backref, int no_quota)
Zheng Yan31840ae2008-09-23 13:14:14 -04003054{
Josef Bacikfcebe452014-05-13 17:30:47 -07003055 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003056}
Zheng Yan31840ae2008-09-23 13:14:14 -04003057
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003058int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacikfcebe452014-05-13 17:30:47 -07003059 struct extent_buffer *buf, int full_backref, int no_quota)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003060{
Josef Bacikfcebe452014-05-13 17:30:47 -07003061 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, no_quota);
Zheng Yan31840ae2008-09-23 13:14:14 -04003062}
3063
Chris Mason9078a3e2007-04-26 16:46:15 -04003064static int write_one_cache_group(struct btrfs_trans_handle *trans,
3065 struct btrfs_root *root,
3066 struct btrfs_path *path,
3067 struct btrfs_block_group_cache *cache)
3068{
3069 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003070 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003071 unsigned long bi;
3072 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003073
Chris Mason9078a3e2007-04-26 16:46:15 -04003074 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003075 if (ret < 0)
3076 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003077 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003078
3079 leaf = path->nodes[0];
3080 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3081 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3082 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003083 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003084fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003085 if (ret) {
3086 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003087 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003088 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003089 return 0;
3090
3091}
3092
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003093static struct btrfs_block_group_cache *
3094next_block_group(struct btrfs_root *root,
3095 struct btrfs_block_group_cache *cache)
3096{
3097 struct rb_node *node;
3098 spin_lock(&root->fs_info->block_group_cache_lock);
3099 node = rb_next(&cache->cache_node);
3100 btrfs_put_block_group(cache);
3101 if (node) {
3102 cache = rb_entry(node, struct btrfs_block_group_cache,
3103 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003104 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003105 } else
3106 cache = NULL;
3107 spin_unlock(&root->fs_info->block_group_cache_lock);
3108 return cache;
3109}
3110
Josef Bacik0af3d002010-06-21 14:48:16 -04003111static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3112 struct btrfs_trans_handle *trans,
3113 struct btrfs_path *path)
3114{
3115 struct btrfs_root *root = block_group->fs_info->tree_root;
3116 struct inode *inode = NULL;
3117 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003118 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003119 int num_pages = 0;
3120 int retries = 0;
3121 int ret = 0;
3122
3123 /*
3124 * If this block group is smaller than 100 megs don't bother caching the
3125 * block group.
3126 */
3127 if (block_group->key.offset < (100 * 1024 * 1024)) {
3128 spin_lock(&block_group->lock);
3129 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3130 spin_unlock(&block_group->lock);
3131 return 0;
3132 }
3133
3134again:
3135 inode = lookup_free_space_inode(root, block_group, path);
3136 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3137 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003138 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003139 goto out;
3140 }
3141
3142 if (IS_ERR(inode)) {
3143 BUG_ON(retries);
3144 retries++;
3145
3146 if (block_group->ro)
3147 goto out_free;
3148
3149 ret = create_free_space_inode(root, trans, block_group, path);
3150 if (ret)
3151 goto out_free;
3152 goto again;
3153 }
3154
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003155 /* We've already setup this transaction, go ahead and exit */
3156 if (block_group->cache_generation == trans->transid &&
3157 i_size_read(inode)) {
3158 dcs = BTRFS_DC_SETUP;
3159 goto out_put;
3160 }
3161
Josef Bacik0af3d002010-06-21 14:48:16 -04003162 /*
3163 * We want to set the generation to 0, that way if anything goes wrong
3164 * from here on out we know not to trust this cache when we load up next
3165 * time.
3166 */
3167 BTRFS_I(inode)->generation = 0;
3168 ret = btrfs_update_inode(trans, root, inode);
3169 WARN_ON(ret);
3170
3171 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003172 ret = btrfs_check_trunc_cache_free_space(root,
3173 &root->fs_info->global_block_rsv);
3174 if (ret)
3175 goto out_put;
3176
Filipe David Borba Manana74514322013-09-20 14:46:51 +01003177 ret = btrfs_truncate_free_space_cache(root, trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003178 if (ret)
3179 goto out_put;
3180 }
3181
3182 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003183 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3184 !btrfs_test_opt(root, SPACE_CACHE)) {
3185 /*
3186 * don't bother trying to write stuff out _if_
3187 * a) we're not cached,
3188 * b) we're with nospace_cache mount option.
3189 */
Josef Bacik2b209822010-12-03 13:17:53 -05003190 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003191 spin_unlock(&block_group->lock);
3192 goto out_put;
3193 }
3194 spin_unlock(&block_group->lock);
3195
Josef Bacik6fc823b2012-08-06 13:46:38 -06003196 /*
3197 * Try to preallocate enough space based on how big the block group is.
3198 * Keep in mind this has to include any pinned space which could end up
3199 * taking up quite a bit since it's not folded into the other space
3200 * cache.
3201 */
3202 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003203 if (!num_pages)
3204 num_pages = 1;
3205
Josef Bacik0af3d002010-06-21 14:48:16 -04003206 num_pages *= 16;
3207 num_pages *= PAGE_CACHE_SIZE;
3208
3209 ret = btrfs_check_data_free_space(inode, num_pages);
3210 if (ret)
3211 goto out_put;
3212
3213 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3214 num_pages, num_pages,
3215 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003216 if (!ret)
3217 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003218 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003219
Josef Bacik0af3d002010-06-21 14:48:16 -04003220out_put:
3221 iput(inode);
3222out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003223 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003224out:
3225 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003226 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003227 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003228 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003229 spin_unlock(&block_group->lock);
3230
3231 return ret;
3232}
3233
Chris Mason96b51792007-10-15 16:15:19 -04003234int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3235 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003236{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003237 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003238 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003239 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003240 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003241
3242 path = btrfs_alloc_path();
3243 if (!path)
3244 return -ENOMEM;
3245
Josef Bacik0af3d002010-06-21 14:48:16 -04003246again:
3247 while (1) {
3248 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3249 while (cache) {
3250 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3251 break;
3252 cache = next_block_group(root, cache);
3253 }
3254 if (!cache) {
3255 if (last == 0)
3256 break;
3257 last = 0;
3258 continue;
3259 }
3260 err = cache_save_setup(cache, trans, path);
3261 last = cache->key.objectid + cache->key.offset;
3262 btrfs_put_block_group(cache);
3263 }
3264
Chris Masond3977122009-01-05 21:25:51 -05003265 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003266 if (last == 0) {
3267 err = btrfs_run_delayed_refs(trans, root,
3268 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003269 if (err) /* File system offline */
3270 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003271 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003272
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003273 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3274 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003275 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3276 btrfs_put_block_group(cache);
3277 goto again;
3278 }
3279
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003280 if (cache->dirty)
3281 break;
3282 cache = next_block_group(root, cache);
3283 }
3284 if (!cache) {
3285 if (last == 0)
3286 break;
3287 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003288 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003289 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003290
Josef Bacik0cb59c92010-07-02 12:14:14 -04003291 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3292 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003293 cache->dirty = 0;
3294 last = cache->key.objectid + cache->key.offset;
3295
3296 err = write_one_cache_group(trans, root, path, cache);
Filipe David Borba Mananae84cc142013-09-09 19:49:43 +01003297 btrfs_put_block_group(cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003298 if (err) /* File system offline */
3299 goto out;
Chris Mason9078a3e2007-04-26 16:46:15 -04003300 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003301
Josef Bacik0cb59c92010-07-02 12:14:14 -04003302 while (1) {
3303 /*
3304 * I don't think this is needed since we're just marking our
3305 * preallocated extent as written, but just in case it can't
3306 * hurt.
3307 */
3308 if (last == 0) {
3309 err = btrfs_run_delayed_refs(trans, root,
3310 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003311 if (err) /* File system offline */
3312 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003313 }
3314
3315 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3316 while (cache) {
3317 /*
3318 * Really this shouldn't happen, but it could if we
3319 * couldn't write the entire preallocated extent and
3320 * splitting the extent resulted in a new block.
3321 */
3322 if (cache->dirty) {
3323 btrfs_put_block_group(cache);
3324 goto again;
3325 }
3326 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3327 break;
3328 cache = next_block_group(root, cache);
3329 }
3330 if (!cache) {
3331 if (last == 0)
3332 break;
3333 last = 0;
3334 continue;
3335 }
3336
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003337 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003338
3339 /*
3340 * If we didn't have an error then the cache state is still
3341 * NEED_WRITE, so we can set it to WRITTEN.
3342 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003343 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003344 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3345 last = cache->key.objectid + cache->key.offset;
3346 btrfs_put_block_group(cache);
3347 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003348out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003349
Chris Mason9078a3e2007-04-26 16:46:15 -04003350 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003351 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003352}
3353
Yan Zhengd2fb3432008-12-11 16:30:39 -05003354int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3355{
3356 struct btrfs_block_group_cache *block_group;
3357 int readonly = 0;
3358
3359 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3360 if (!block_group || block_group->ro)
3361 readonly = 1;
3362 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003363 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003364 return readonly;
3365}
3366
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003367static const char *alloc_name(u64 flags)
3368{
3369 switch (flags) {
3370 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3371 return "mixed";
3372 case BTRFS_BLOCK_GROUP_METADATA:
3373 return "metadata";
3374 case BTRFS_BLOCK_GROUP_DATA:
3375 return "data";
3376 case BTRFS_BLOCK_GROUP_SYSTEM:
3377 return "system";
3378 default:
3379 WARN_ON(1);
3380 return "invalid-combination";
3381 };
3382}
3383
Chris Mason593060d2008-03-25 16:50:33 -04003384static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3385 u64 total_bytes, u64 bytes_used,
3386 struct btrfs_space_info **space_info)
3387{
3388 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003389 int i;
3390 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003391 int ret;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003392
3393 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3394 BTRFS_BLOCK_GROUP_RAID10))
3395 factor = 2;
3396 else
3397 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003398
3399 found = __find_space_info(info, flags);
3400 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003401 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003402 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003403 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003404 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003405 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003406 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003407 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003408 *space_info = found;
3409 return 0;
3410 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003411 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003412 if (!found)
3413 return -ENOMEM;
3414
Josef Bacikb150a4f2013-06-19 15:00:04 -04003415 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3416 if (ret) {
3417 kfree(found);
3418 return ret;
3419 }
3420
Miao Xie536cd962013-12-17 12:01:12 +08003421 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04003422 INIT_LIST_HEAD(&found->block_groups[i]);
Miao Xie536cd962013-12-17 12:01:12 +08003423 kobject_init(&found->block_group_kobjs[i], &btrfs_raid_ktype);
3424 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003425 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003426 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003427 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003428 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003429 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003430 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003431 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003432 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003433 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003434 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003435 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003436 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003437 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003438 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003439 found->flush = 0;
3440 init_waitqueue_head(&found->wait);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003441
3442 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3443 info->space_info_kobj, "%s",
3444 alloc_name(found->flags));
3445 if (ret) {
3446 kfree(found);
3447 return ret;
3448 }
3449
Chris Mason593060d2008-03-25 16:50:33 -04003450 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003451 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003452 if (flags & BTRFS_BLOCK_GROUP_DATA)
3453 info->data_sinfo = found;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003454
3455 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04003456}
3457
Chris Mason8790d502008-04-03 16:29:03 -04003458static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3459{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003460 u64 extra_flags = chunk_to_extended(flags) &
3461 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003462
Miao Xiede98ced2013-01-29 10:13:12 +00003463 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003464 if (flags & BTRFS_BLOCK_GROUP_DATA)
3465 fs_info->avail_data_alloc_bits |= extra_flags;
3466 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3467 fs_info->avail_metadata_alloc_bits |= extra_flags;
3468 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3469 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003470 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003471}
Chris Mason593060d2008-03-25 16:50:33 -04003472
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003473/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003474 * returns target flags in extended format or 0 if restripe for this
3475 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003476 *
3477 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003478 */
3479static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3480{
3481 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3482 u64 target = 0;
3483
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003484 if (!bctl)
3485 return 0;
3486
3487 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3488 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3489 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3490 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3491 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3492 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3493 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3494 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3495 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3496 }
3497
3498 return target;
3499}
3500
3501/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003502 * @flags: available profiles in extended format (see ctree.h)
3503 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003504 * Returns reduced profile in chunk format. If profile changing is in
3505 * progress (either running or paused) picks the target profile (if it's
3506 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003507 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003508static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003509{
Chris Masoncd02dca2010-12-13 14:56:23 -05003510 /*
3511 * we add in the count of missing devices because we want
3512 * to make sure that any RAID levels on a degraded FS
3513 * continue to be honored.
3514 */
3515 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3516 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003517 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003518 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003519
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003520 /*
3521 * see if restripe for this chunk_type is in progress, if so
3522 * try to reduce to the target profile
3523 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003524 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003525 target = get_restripe_target(root->fs_info, flags);
3526 if (target) {
3527 /* pick target profile only if it's already available */
3528 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003529 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003530 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003531 }
3532 }
3533 spin_unlock(&root->fs_info->balance_lock);
3534
David Woodhouse53b381b2013-01-29 18:40:14 -05003535 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003536 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003537 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3538 BTRFS_BLOCK_GROUP_RAID5);
3539 if (num_devices < 3)
3540 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003541 if (num_devices < 4)
3542 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3543
David Woodhouse53b381b2013-01-29 18:40:14 -05003544 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3545 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3546 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3547 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003548
David Woodhouse53b381b2013-01-29 18:40:14 -05003549 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3550 tmp = BTRFS_BLOCK_GROUP_RAID6;
3551 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3552 tmp = BTRFS_BLOCK_GROUP_RAID5;
3553 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3554 tmp = BTRFS_BLOCK_GROUP_RAID10;
3555 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3556 tmp = BTRFS_BLOCK_GROUP_RAID1;
3557 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3558 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003559
David Woodhouse53b381b2013-01-29 18:40:14 -05003560 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003561}
3562
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003563static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003564{
Miao Xiede98ced2013-01-29 10:13:12 +00003565 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003566 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003567
3568 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003569 flags = orig_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003570 seq = read_seqbegin(&root->fs_info->profiles_lock);
3571
3572 if (flags & BTRFS_BLOCK_GROUP_DATA)
3573 flags |= root->fs_info->avail_data_alloc_bits;
3574 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3575 flags |= root->fs_info->avail_system_alloc_bits;
3576 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3577 flags |= root->fs_info->avail_metadata_alloc_bits;
3578 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003579
Yan, Zhengb742bb82010-05-16 10:46:24 -04003580 return btrfs_reduce_alloc_profile(root, flags);
3581}
Josef Bacik6a632092009-02-20 11:00:09 -05003582
Miao Xie6d07bce2011-01-05 10:07:31 +00003583u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003584{
3585 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003586 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003587
Yan, Zhengb742bb82010-05-16 10:46:24 -04003588 if (data)
3589 flags = BTRFS_BLOCK_GROUP_DATA;
3590 else if (root == root->fs_info->chunk_root)
3591 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3592 else
3593 flags = BTRFS_BLOCK_GROUP_METADATA;
3594
David Woodhouse53b381b2013-01-29 18:40:14 -05003595 ret = get_alloc_profile(root, flags);
3596 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003597}
3598
Josef Bacik6a632092009-02-20 11:00:09 -05003599/*
3600 * This will check the space that the inode allocates from to make sure we have
3601 * enough space for bytes.
3602 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003603int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003604{
3605 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003606 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003607 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003608 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003609 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003610
3611 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003612 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003613
Miao Xie9dced182013-10-25 17:33:36 +08003614 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003615 committed = 1;
Miao Xie9dced182013-10-25 17:33:36 +08003616 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04003617 }
3618
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003619 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003620 if (!data_sinfo)
3621 goto alloc;
3622
Josef Bacik6a632092009-02-20 11:00:09 -05003623again:
3624 /* make sure we have enough space to handle the data first */
3625 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003626 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3627 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3628 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003629
3630 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003631 struct btrfs_trans_handle *trans;
3632
Josef Bacik6a632092009-02-20 11:00:09 -05003633 /*
3634 * if we don't have enough free bytes in this space then we need
3635 * to alloc a new chunk.
3636 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003637 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003638 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003639
Chris Mason0e4f8f82011-04-15 16:05:44 -04003640 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003641 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003642alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003643 alloc_target = btrfs_get_alloc_profile(root, 1);
Miao Xie9dced182013-10-25 17:33:36 +08003644 /*
3645 * It is ugly that we don't call nolock join
3646 * transaction for the free space inode case here.
3647 * But it is safe because we only do the data space
3648 * reservation for the free space cache in the
3649 * transaction context, the common join transaction
3650 * just increase the counter of the current transaction
3651 * handler, doesn't try to acquire the trans_lock of
3652 * the fs.
3653 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003654 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003655 if (IS_ERR(trans))
3656 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003657
3658 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003659 alloc_target,
3660 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003661 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003662 if (ret < 0) {
3663 if (ret != -ENOSPC)
3664 return ret;
3665 else
3666 goto commit_trans;
3667 }
Chris Mason33b4d472009-09-22 14:45:50 -04003668
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003669 if (!data_sinfo)
3670 data_sinfo = fs_info->data_sinfo;
3671
Josef Bacik6a632092009-02-20 11:00:09 -05003672 goto again;
3673 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003674
3675 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04003676 * If we don't have enough pinned space to deal with this
3677 * allocation don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003678 */
Josef Bacikb150a4f2013-06-19 15:00:04 -04003679 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3680 bytes) < 0)
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003681 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003682 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003683
3684 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003685commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003686 if (!committed &&
3687 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003688 committed = 1;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003689
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003690 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691 if (IS_ERR(trans))
3692 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003693 ret = btrfs_commit_transaction(trans, root);
3694 if (ret)
3695 return ret;
3696 goto again;
3697 }
3698
Jeff Mahoneycab45e22013-10-16 16:27:01 -04003699 trace_btrfs_space_reservation(root->fs_info,
3700 "space_info:enospc",
3701 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003702 return -ENOSPC;
3703 }
3704 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003705 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003706 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003707 spin_unlock(&data_sinfo->lock);
3708
Josef Bacik9ed74f22009-09-11 16:12:44 -04003709 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003710}
3711
3712/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003713 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003714 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003715void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003716{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003717 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003718 struct btrfs_space_info *data_sinfo;
3719
3720 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003721 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003722
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003723 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003724 spin_lock(&data_sinfo->lock);
Josef Bacik7ee9e442013-06-21 16:37:03 -04003725 WARN_ON(data_sinfo->bytes_may_use < bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003726 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003727 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003728 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003729 spin_unlock(&data_sinfo->lock);
3730}
3731
Josef Bacik97e728d2009-04-21 17:40:57 -04003732static void force_metadata_allocation(struct btrfs_fs_info *info)
3733{
3734 struct list_head *head = &info->space_info;
3735 struct btrfs_space_info *found;
3736
3737 rcu_read_lock();
3738 list_for_each_entry_rcu(found, head, list) {
3739 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003740 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003741 }
3742 rcu_read_unlock();
3743}
3744
Miao Xie3c76cd82013-04-25 10:12:38 +00003745static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3746{
3747 return (global->size << 1);
3748}
3749
Chris Masone5bc2452010-10-26 13:37:56 -04003750static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003751 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003752{
Josef Bacikfb25e912011-07-26 17:00:46 -04003753 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003754 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003755 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003756 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003757
Chris Mason0e4f8f82011-04-15 16:05:44 -04003758 if (force == CHUNK_ALLOC_FORCE)
3759 return 1;
3760
3761 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003762 * We need to take into account the global rsv because for all intents
3763 * and purposes it's used space. Don't worry about locking the
3764 * global_rsv, it doesn't change except when the transaction commits.
3765 */
Josef Bacik54338b52012-08-14 16:20:52 -04003766 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003767 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003768
3769 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003770 * in limited mode, we want to have some free space up to
3771 * about 1% of the FS size.
3772 */
3773 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003774 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003775 thresh = max_t(u64, 64 * 1024 * 1024,
3776 div_factor_fine(thresh, 1));
3777
3778 if (num_bytes - num_allocated < thresh)
3779 return 1;
3780 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003781
Josef Bacik698d0082012-09-12 14:08:47 -04003782 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003783 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003784 return 1;
3785}
3786
Liu Bo15d1ff82012-03-29 09:57:44 -04003787static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3788{
3789 u64 num_dev;
3790
David Woodhouse53b381b2013-01-29 18:40:14 -05003791 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3792 BTRFS_BLOCK_GROUP_RAID0 |
3793 BTRFS_BLOCK_GROUP_RAID5 |
3794 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003795 num_dev = root->fs_info->fs_devices->rw_devices;
3796 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3797 num_dev = 2;
3798 else
3799 num_dev = 1; /* DUP or single */
3800
3801 /* metadata for updaing devices and chunk tree */
3802 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3803}
3804
3805static void check_system_chunk(struct btrfs_trans_handle *trans,
3806 struct btrfs_root *root, u64 type)
3807{
3808 struct btrfs_space_info *info;
3809 u64 left;
3810 u64 thresh;
3811
3812 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3813 spin_lock(&info->lock);
3814 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3815 info->bytes_reserved - info->bytes_readonly;
3816 spin_unlock(&info->lock);
3817
3818 thresh = get_system_chunk_thresh(root, type);
3819 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003820 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3821 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003822 dump_space_info(info, 0, 0);
3823 }
3824
3825 if (left < thresh) {
3826 u64 flags;
3827
3828 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3829 btrfs_alloc_chunk(trans, root, flags);
3830 }
3831}
3832
Chris Mason6324fbf2008-03-24 15:01:59 -04003833static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003834 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003835{
3836 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003837 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003838 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003839 int ret = 0;
3840
Josef Bacikc6b305a2012-12-18 09:16:16 -05003841 /* Don't re-enter if we're already allocating a chunk */
3842 if (trans->allocating_chunk)
3843 return -ENOSPC;
3844
Chris Mason6324fbf2008-03-24 15:01:59 -04003845 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003846 if (!space_info) {
3847 ret = update_space_info(extent_root->fs_info, flags,
3848 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003849 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003850 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003851 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003852
Josef Bacik6d741192011-04-11 20:20:11 -04003853again:
Josef Bacik25179202008-10-29 14:49:05 -04003854 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003855 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003856 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003857 if (space_info->full) {
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01003858 if (should_alloc_chunk(extent_root, space_info, force))
3859 ret = -ENOSPC;
3860 else
3861 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003862 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01003863 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04003864 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003865
Josef Bacik698d0082012-09-12 14:08:47 -04003866 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003867 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003868 return 0;
3869 } else if (space_info->chunk_alloc) {
3870 wait_for_alloc = 1;
3871 } else {
3872 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003873 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003874
Josef Bacik25179202008-10-29 14:49:05 -04003875 spin_unlock(&space_info->lock);
3876
Josef Bacik6d741192011-04-11 20:20:11 -04003877 mutex_lock(&fs_info->chunk_mutex);
3878
3879 /*
3880 * The chunk_mutex is held throughout the entirety of a chunk
3881 * allocation, so once we've acquired the chunk_mutex we know that the
3882 * other guy is done and we need to recheck and see if we should
3883 * allocate.
3884 */
3885 if (wait_for_alloc) {
3886 mutex_unlock(&fs_info->chunk_mutex);
3887 wait_for_alloc = 0;
3888 goto again;
3889 }
3890
Josef Bacikc6b305a2012-12-18 09:16:16 -05003891 trans->allocating_chunk = true;
3892
Josef Bacik97e728d2009-04-21 17:40:57 -04003893 /*
Josef Bacik67377732010-09-16 16:19:09 -04003894 * If we have mixed data/metadata chunks we want to make sure we keep
3895 * allocating mixed chunks instead of individual chunks.
3896 */
3897 if (btrfs_mixed_space_info(space_info))
3898 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3899
3900 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003901 * if we're doing a data chunk, go ahead and make sure that
3902 * we keep a reasonable number of metadata chunks allocated in the
3903 * FS as well.
3904 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003905 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003906 fs_info->data_chunk_allocations++;
3907 if (!(fs_info->data_chunk_allocations %
3908 fs_info->metadata_ratio))
3909 force_metadata_allocation(fs_info);
3910 }
3911
Liu Bo15d1ff82012-03-29 09:57:44 -04003912 /*
3913 * Check if we have enough space in SYSTEM chunk because we may need
3914 * to update devices.
3915 */
3916 check_system_chunk(trans, extent_root, flags);
3917
Yan Zheng2b820322008-11-17 21:11:30 -05003918 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003919 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003920
Josef Bacik9ed74f22009-09-11 16:12:44 -04003921 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003922 if (ret < 0 && ret != -ENOSPC)
3923 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003924 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003925 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003926 else
3927 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003928
Chris Mason0e4f8f82011-04-15 16:05:44 -04003929 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003930out:
Josef Bacik6d741192011-04-11 20:20:11 -04003931 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003932 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003933 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003934 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003935}
3936
Josef Bacika80c8dc2012-09-06 16:59:33 -04003937static int can_overcommit(struct btrfs_root *root,
3938 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003939 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003940{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003941 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003942 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00003943 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003944 u64 avail;
3945 u64 used;
3946
3947 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003948 space_info->bytes_pinned + space_info->bytes_readonly;
3949
Josef Bacik96f1bb52013-01-30 17:02:51 -05003950 /*
3951 * We only want to allow over committing if we have lots of actual space
3952 * free, but if we don't have enough space to handle the global reserve
3953 * space then we could end up having a real enospc problem when trying
3954 * to allocate a chunk or some other such important allocation.
3955 */
Miao Xie3c76cd82013-04-25 10:12:38 +00003956 spin_lock(&global_rsv->lock);
3957 space_size = calc_global_rsv_need_space(global_rsv);
3958 spin_unlock(&global_rsv->lock);
3959 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05003960 return 0;
3961
3962 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003963
3964 spin_lock(&root->fs_info->free_chunk_lock);
3965 avail = root->fs_info->free_chunk_space;
3966 spin_unlock(&root->fs_info->free_chunk_lock);
3967
3968 /*
3969 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003970 * space is actually useable. For raid56, the space info used
3971 * doesn't include the parity drive, so we don't have to
3972 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003973 */
3974 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3975 BTRFS_BLOCK_GROUP_RAID1 |
3976 BTRFS_BLOCK_GROUP_RAID10))
3977 avail >>= 1;
3978
3979 /*
Miao Xie561c2942012-10-16 11:32:18 +00003980 * If we aren't flushing all things, let us overcommit up to
3981 * 1/2th of the space. If we can flush, don't let us overcommit
3982 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003983 */
Miao Xie08e007d2012-10-16 11:33:38 +00003984 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04003985 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003986 else
Josef Bacik14575ae2013-09-17 10:48:00 -04003987 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003988
Josef Bacik14575ae2013-09-17 10:48:00 -04003989 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003990 return 1;
3991 return 0;
3992}
3993
Eric Sandeen48a3b632013-04-25 20:41:01 +00003994static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
Miao Xie6c255e62014-03-06 13:55:01 +08003995 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00003996{
3997 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00003998
Josef Bacik925a6ef2013-06-20 12:31:27 -04003999 if (down_read_trylock(&sb->s_umount)) {
4000 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4001 up_read(&sb->s_umount);
4002 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004003 /*
4004 * We needn't worry the filesystem going from r/w to r/o though
4005 * we don't acquire ->s_umount mutex, because the filesystem
4006 * should guarantee the delalloc inodes list be empty after
4007 * the filesystem is readonly(all dirty pages are written to
4008 * the disk).
4009 */
Miao Xie6c255e62014-03-06 13:55:01 +08004010 btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004011 if (!current->journal_info)
Miao Xie6c255e62014-03-06 13:55:01 +08004012 btrfs_wait_ordered_roots(root->fs_info, nr_items);
Miao Xieda633a42012-12-20 11:19:09 +00004013 }
4014}
4015
Miao Xie18cd8ea2013-11-04 23:13:22 +08004016static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4017{
4018 u64 bytes;
4019 int nr;
4020
4021 bytes = btrfs_calc_trans_metadata_size(root, 1);
4022 nr = (int)div64_u64(to_reclaim, bytes);
4023 if (!nr)
4024 nr = 1;
4025 return nr;
4026}
4027
Miao Xiec61a16a2013-11-04 23:13:23 +08004028#define EXTENT_SIZE_PER_ITEM (256 * 1024)
4029
Yan, Zheng5da9d012010-05-16 10:46:25 -04004030/*
4031 * shrink metadata reservation for delalloc
4032 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04004033static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4034 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004035{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004036 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004037 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004038 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004039 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004040 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004041 long time_left;
Miao Xied3ee29e2013-11-04 23:13:20 +08004042 unsigned long nr_pages;
4043 int loops;
Miao Xieb0244192013-11-04 23:13:25 +08004044 int items;
Miao Xie08e007d2012-10-16 11:33:38 +00004045 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004046
Miao Xiec61a16a2013-11-04 23:13:23 +08004047 /* Calc the number of the pages we need flush for space reservation */
Miao Xieb0244192013-11-04 23:13:25 +08004048 items = calc_reclaim_items_nr(root, to_reclaim);
4049 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004050
Josef Bacik663350a2011-11-03 22:54:25 -04004051 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004052 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004053 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004054
Miao Xie963d6782013-01-29 10:10:51 +00004055 delalloc_bytes = percpu_counter_sum_positive(
4056 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004057 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004058 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004059 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004060 if (wait_ordered)
Miao Xieb0244192013-11-04 23:13:25 +08004061 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004062 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004063 }
4064
Miao Xied3ee29e2013-11-04 23:13:20 +08004065 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004066 while (delalloc_bytes && loops < 3) {
4067 max_reclaim = min(delalloc_bytes, to_reclaim);
4068 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xie6c255e62014-03-06 13:55:01 +08004069 btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004070 /*
4071 * We need to wait for the async pages to actually start before
4072 * we do anything.
4073 */
Miao Xie9f3a0742013-11-04 23:13:24 +08004074 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
4075 if (!max_reclaim)
4076 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004077
Miao Xie9f3a0742013-11-04 23:13:24 +08004078 if (max_reclaim <= nr_pages)
4079 max_reclaim = 0;
4080 else
4081 max_reclaim -= nr_pages;
4082
4083 wait_event(root->fs_info->async_submit_wait,
4084 atomic_read(&root->fs_info->async_delalloc_pages) <=
4085 (int)max_reclaim);
4086skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004087 if (!trans)
4088 flush = BTRFS_RESERVE_FLUSH_ALL;
4089 else
4090 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004091 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004092 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004093 spin_unlock(&space_info->lock);
4094 break;
4095 }
Josef Bacik0019f102010-10-15 15:18:40 -04004096 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004097
Chris Mason36e39c42011-03-12 07:08:42 -05004098 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004099 if (wait_ordered && !trans) {
Miao Xieb0244192013-11-04 23:13:25 +08004100 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf104d042011-10-14 13:56:58 -04004101 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004102 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004103 if (time_left)
4104 break;
4105 }
Miao Xie963d6782013-01-29 10:10:51 +00004106 delalloc_bytes = percpu_counter_sum_positive(
4107 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004108 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004109}
4110
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004111/**
Josef Bacik663350a2011-11-03 22:54:25 -04004112 * maybe_commit_transaction - possibly commit the transaction if its ok to
4113 * @root - the root we're allocating for
4114 * @bytes - the number of bytes we want to reserve
4115 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004116 *
Josef Bacik663350a2011-11-03 22:54:25 -04004117 * This will check to make sure that committing the transaction will actually
4118 * get us somewhere and then commit the transaction if it does. Otherwise it
4119 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004120 */
Josef Bacik663350a2011-11-03 22:54:25 -04004121static int may_commit_transaction(struct btrfs_root *root,
4122 struct btrfs_space_info *space_info,
4123 u64 bytes, int force)
4124{
4125 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4126 struct btrfs_trans_handle *trans;
4127
4128 trans = (struct btrfs_trans_handle *)current->journal_info;
4129 if (trans)
4130 return -EAGAIN;
4131
4132 if (force)
4133 goto commit;
4134
4135 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004136 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004137 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004138 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004139
4140 /*
4141 * See if there is some space in the delayed insertion reservation for
4142 * this reservation.
4143 */
4144 if (space_info != delayed_rsv->space_info)
4145 return -ENOSPC;
4146
4147 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004148 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4149 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004150 spin_unlock(&delayed_rsv->lock);
4151 return -ENOSPC;
4152 }
4153 spin_unlock(&delayed_rsv->lock);
4154
4155commit:
4156 trans = btrfs_join_transaction(root);
4157 if (IS_ERR(trans))
4158 return -ENOSPC;
4159
4160 return btrfs_commit_transaction(trans, root);
4161}
4162
Josef Bacik96c3f432012-06-21 14:05:49 -04004163enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004164 FLUSH_DELAYED_ITEMS_NR = 1,
4165 FLUSH_DELAYED_ITEMS = 2,
4166 FLUSH_DELALLOC = 3,
4167 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004168 ALLOC_CHUNK = 5,
4169 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004170};
4171
4172static int flush_space(struct btrfs_root *root,
4173 struct btrfs_space_info *space_info, u64 num_bytes,
4174 u64 orig_bytes, int state)
4175{
4176 struct btrfs_trans_handle *trans;
4177 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004178 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004179
4180 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004181 case FLUSH_DELAYED_ITEMS_NR:
4182 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004183 if (state == FLUSH_DELAYED_ITEMS_NR)
4184 nr = calc_reclaim_items_nr(root, num_bytes) * 2;
4185 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004186 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004187
Josef Bacik96c3f432012-06-21 14:05:49 -04004188 trans = btrfs_join_transaction(root);
4189 if (IS_ERR(trans)) {
4190 ret = PTR_ERR(trans);
4191 break;
4192 }
4193 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4194 btrfs_end_transaction(trans, root);
4195 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004196 case FLUSH_DELALLOC:
4197 case FLUSH_DELALLOC_WAIT:
Miao Xie24af7dd2014-03-06 13:55:00 +08004198 shrink_delalloc(root, num_bytes * 2, orig_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004199 state == FLUSH_DELALLOC_WAIT);
4200 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004201 case ALLOC_CHUNK:
4202 trans = btrfs_join_transaction(root);
4203 if (IS_ERR(trans)) {
4204 ret = PTR_ERR(trans);
4205 break;
4206 }
4207 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004208 btrfs_get_alloc_profile(root, 0),
4209 CHUNK_ALLOC_NO_FORCE);
4210 btrfs_end_transaction(trans, root);
4211 if (ret == -ENOSPC)
4212 ret = 0;
4213 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004214 case COMMIT_TRANS:
4215 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4216 break;
4217 default:
4218 ret = -ENOSPC;
4219 break;
4220 }
4221
4222 return ret;
4223}
Miao Xie21c7e752014-05-13 17:29:04 -07004224
4225static inline u64
4226btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4227 struct btrfs_space_info *space_info)
4228{
4229 u64 used;
4230 u64 expected;
4231 u64 to_reclaim;
4232
4233 to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
4234 16 * 1024 * 1024);
4235 spin_lock(&space_info->lock);
4236 if (can_overcommit(root, space_info, to_reclaim,
4237 BTRFS_RESERVE_FLUSH_ALL)) {
4238 to_reclaim = 0;
4239 goto out;
4240 }
4241
4242 used = space_info->bytes_used + space_info->bytes_reserved +
4243 space_info->bytes_pinned + space_info->bytes_readonly +
4244 space_info->bytes_may_use;
4245 if (can_overcommit(root, space_info, 1024 * 1024,
4246 BTRFS_RESERVE_FLUSH_ALL))
4247 expected = div_factor_fine(space_info->total_bytes, 95);
4248 else
4249 expected = div_factor_fine(space_info->total_bytes, 90);
4250
4251 if (used > expected)
4252 to_reclaim = used - expected;
4253 else
4254 to_reclaim = 0;
4255 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4256 space_info->bytes_reserved);
4257out:
4258 spin_unlock(&space_info->lock);
4259
4260 return to_reclaim;
4261}
4262
4263static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4264 struct btrfs_fs_info *fs_info, u64 used)
4265{
4266 return (used >= div_factor_fine(space_info->total_bytes, 98) &&
4267 !btrfs_fs_closing(fs_info) &&
4268 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
4269}
4270
4271static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
4272 struct btrfs_fs_info *fs_info)
4273{
4274 u64 used;
4275
4276 spin_lock(&space_info->lock);
4277 used = space_info->bytes_used + space_info->bytes_reserved +
4278 space_info->bytes_pinned + space_info->bytes_readonly +
4279 space_info->bytes_may_use;
4280 if (need_do_async_reclaim(space_info, fs_info, used)) {
4281 spin_unlock(&space_info->lock);
4282 return 1;
4283 }
4284 spin_unlock(&space_info->lock);
4285
4286 return 0;
4287}
4288
4289static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4290{
4291 struct btrfs_fs_info *fs_info;
4292 struct btrfs_space_info *space_info;
4293 u64 to_reclaim;
4294 int flush_state;
4295
4296 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4297 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4298
4299 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4300 space_info);
4301 if (!to_reclaim)
4302 return;
4303
4304 flush_state = FLUSH_DELAYED_ITEMS_NR;
4305 do {
4306 flush_space(fs_info->fs_root, space_info, to_reclaim,
4307 to_reclaim, flush_state);
4308 flush_state++;
4309 if (!btrfs_need_do_async_reclaim(space_info, fs_info))
4310 return;
4311 } while (flush_state <= COMMIT_TRANS);
4312
4313 if (btrfs_need_do_async_reclaim(space_info, fs_info))
4314 queue_work(system_unbound_wq, work);
4315}
4316
4317void btrfs_init_async_reclaim_work(struct work_struct *work)
4318{
4319 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
4320}
4321
Josef Bacik663350a2011-11-03 22:54:25 -04004322/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004323 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4324 * @root - the root we're allocating for
4325 * @block_rsv - the block_rsv we're allocating for
4326 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004327 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004328 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004329 * This will reserve orgi_bytes number of bytes from the space info associated
4330 * with the block_rsv. If there is not enough space it will make an attempt to
4331 * flush out space to make room. It will do this by flushing delalloc if
4332 * possible or committing the transaction. If flush is 0 then no attempts to
4333 * regain reservations will be made and this will fail if there is not enough
4334 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004335 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004336static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004337 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004338 u64 orig_bytes,
4339 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004340{
4341 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004342 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004343 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004344 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004345 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004346 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004347
4348again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004349 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004350 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004351 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004352 * We only want to wait if somebody other than us is flushing and we
4353 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004354 */
Miao Xie08e007d2012-10-16 11:33:38 +00004355 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4356 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004357 spin_unlock(&space_info->lock);
4358 /*
4359 * If we have a trans handle we can't wait because the flusher
4360 * may have to commit the transaction, which would mean we would
4361 * deadlock since we are waiting for the flusher to finish, but
4362 * hold the current transaction open.
4363 */
Josef Bacik663350a2011-11-03 22:54:25 -04004364 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004365 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004366 ret = wait_event_killable(space_info->wait, !space_info->flush);
4367 /* Must have been killed, return */
4368 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004369 return -EINTR;
4370
4371 spin_lock(&space_info->lock);
4372 }
4373
4374 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004375 used = space_info->bytes_used + space_info->bytes_reserved +
4376 space_info->bytes_pinned + space_info->bytes_readonly +
4377 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004378
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004379 /*
4380 * The idea here is that we've not already over-reserved the block group
4381 * then we can go ahead and save our reservation first and then start
4382 * flushing if we need to. Otherwise if we've already overcommitted
4383 * lets start flushing stuff first and then come back and try to make
4384 * our reservation.
4385 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004386 if (used <= space_info->total_bytes) {
4387 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004388 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004389 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004390 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004391 ret = 0;
4392 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004393 /*
4394 * Ok set num_bytes to orig_bytes since we aren't
4395 * overocmmitted, this way we only try and reclaim what
4396 * we need.
4397 */
4398 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004399 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004400 } else {
4401 /*
4402 * Ok we're over committed, set num_bytes to the overcommitted
4403 * amount plus the amount of bytes that we need for this
4404 * reservation.
4405 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004406 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004407 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004408 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004409
Josef Bacik44734ed2012-09-28 16:04:19 -04004410 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4411 space_info->bytes_may_use += orig_bytes;
4412 trace_btrfs_space_reservation(root->fs_info, "space_info",
4413 space_info->flags, orig_bytes,
4414 1);
4415 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004416 }
4417
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004418 /*
4419 * Couldn't make our reservation, save our place so while we're trying
4420 * to reclaim space we can actually use it instead of somebody else
4421 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004422 *
4423 * We make the other tasks wait for the flush only when we can flush
4424 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004425 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004426 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004427 flushing = true;
4428 space_info->flush = 1;
Miao Xie21c7e752014-05-13 17:29:04 -07004429 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
4430 used += orig_bytes;
4431 if (need_do_async_reclaim(space_info, root->fs_info, used) &&
4432 !work_busy(&root->fs_info->async_reclaim_work))
4433 queue_work(system_unbound_wq,
4434 &root->fs_info->async_reclaim_work);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004435 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004436 spin_unlock(&space_info->lock);
4437
Miao Xie08e007d2012-10-16 11:33:38 +00004438 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004439 goto out;
4440
Josef Bacik96c3f432012-06-21 14:05:49 -04004441 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4442 flush_state);
4443 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004444
4445 /*
4446 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4447 * would happen. So skip delalloc flush.
4448 */
4449 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4450 (flush_state == FLUSH_DELALLOC ||
4451 flush_state == FLUSH_DELALLOC_WAIT))
4452 flush_state = ALLOC_CHUNK;
4453
Josef Bacik96c3f432012-06-21 14:05:49 -04004454 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004455 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004456 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4457 flush_state < COMMIT_TRANS)
4458 goto again;
4459 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4460 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004461 goto again;
4462
4463out:
Josef Bacik5d803662013-02-07 16:06:02 -05004464 if (ret == -ENOSPC &&
4465 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4466 struct btrfs_block_rsv *global_rsv =
4467 &root->fs_info->global_block_rsv;
4468
4469 if (block_rsv != global_rsv &&
4470 !block_rsv_use_bytes(global_rsv, orig_bytes))
4471 ret = 0;
4472 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004473 if (ret == -ENOSPC)
4474 trace_btrfs_space_reservation(root->fs_info,
4475 "space_info:enospc",
4476 space_info->flags, orig_bytes, 1);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004477 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004478 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004479 space_info->flush = 0;
4480 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004481 spin_unlock(&space_info->lock);
4482 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004483 return ret;
4484}
4485
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004486static struct btrfs_block_rsv *get_block_rsv(
4487 const struct btrfs_trans_handle *trans,
4488 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004489{
Josef Bacik4c13d752011-08-30 11:31:29 -04004490 struct btrfs_block_rsv *block_rsv = NULL;
4491
Miao Xie27cdeb72014-04-02 19:51:05 +08004492 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Josef Bacik0e721102012-06-26 16:13:18 -04004493 block_rsv = trans->block_rsv;
4494
4495 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004496 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004497
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004498 if (root == root->fs_info->uuid_root)
4499 block_rsv = trans->block_rsv;
4500
Josef Bacik4c13d752011-08-30 11:31:29 -04004501 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004502 block_rsv = root->block_rsv;
4503
4504 if (!block_rsv)
4505 block_rsv = &root->fs_info->empty_block_rsv;
4506
4507 return block_rsv;
4508}
4509
4510static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4511 u64 num_bytes)
4512{
4513 int ret = -ENOSPC;
4514 spin_lock(&block_rsv->lock);
4515 if (block_rsv->reserved >= num_bytes) {
4516 block_rsv->reserved -= num_bytes;
4517 if (block_rsv->reserved < block_rsv->size)
4518 block_rsv->full = 0;
4519 ret = 0;
4520 }
4521 spin_unlock(&block_rsv->lock);
4522 return ret;
4523}
4524
4525static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4526 u64 num_bytes, int update_size)
4527{
4528 spin_lock(&block_rsv->lock);
4529 block_rsv->reserved += num_bytes;
4530 if (update_size)
4531 block_rsv->size += num_bytes;
4532 else if (block_rsv->reserved >= block_rsv->size)
4533 block_rsv->full = 1;
4534 spin_unlock(&block_rsv->lock);
4535}
4536
Josef Bacikd52be812013-05-29 14:54:47 -04004537int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4538 struct btrfs_block_rsv *dest, u64 num_bytes,
4539 int min_factor)
4540{
4541 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4542 u64 min_bytes;
4543
4544 if (global_rsv->space_info != dest->space_info)
4545 return -ENOSPC;
4546
4547 spin_lock(&global_rsv->lock);
4548 min_bytes = div_factor(global_rsv->size, min_factor);
4549 if (global_rsv->reserved < min_bytes + num_bytes) {
4550 spin_unlock(&global_rsv->lock);
4551 return -ENOSPC;
4552 }
4553 global_rsv->reserved -= num_bytes;
4554 if (global_rsv->reserved < global_rsv->size)
4555 global_rsv->full = 0;
4556 spin_unlock(&global_rsv->lock);
4557
4558 block_rsv_add_bytes(dest, num_bytes, 1);
4559 return 0;
4560}
4561
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004562static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4563 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004564 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004565{
4566 struct btrfs_space_info *space_info = block_rsv->space_info;
4567
4568 spin_lock(&block_rsv->lock);
4569 if (num_bytes == (u64)-1)
4570 num_bytes = block_rsv->size;
4571 block_rsv->size -= num_bytes;
4572 if (block_rsv->reserved >= block_rsv->size) {
4573 num_bytes = block_rsv->reserved - block_rsv->size;
4574 block_rsv->reserved = block_rsv->size;
4575 block_rsv->full = 1;
4576 } else {
4577 num_bytes = 0;
4578 }
4579 spin_unlock(&block_rsv->lock);
4580
4581 if (num_bytes > 0) {
4582 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004583 spin_lock(&dest->lock);
4584 if (!dest->full) {
4585 u64 bytes_to_add;
4586
4587 bytes_to_add = dest->size - dest->reserved;
4588 bytes_to_add = min(num_bytes, bytes_to_add);
4589 dest->reserved += bytes_to_add;
4590 if (dest->reserved >= dest->size)
4591 dest->full = 1;
4592 num_bytes -= bytes_to_add;
4593 }
4594 spin_unlock(&dest->lock);
4595 }
4596 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004597 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004598 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004599 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004600 space_info->flags, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004601 spin_unlock(&space_info->lock);
4602 }
4603 }
4604}
4605
4606static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4607 struct btrfs_block_rsv *dst, u64 num_bytes)
4608{
4609 int ret;
4610
4611 ret = block_rsv_use_bytes(src, num_bytes);
4612 if (ret)
4613 return ret;
4614
4615 block_rsv_add_bytes(dst, num_bytes, 1);
4616 return 0;
4617}
4618
Miao Xie66d8f3d2012-09-06 04:02:28 -06004619void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004620{
4621 memset(rsv, 0, sizeof(*rsv));
4622 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004623 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004624}
4625
Miao Xie66d8f3d2012-09-06 04:02:28 -06004626struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4627 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004628{
4629 struct btrfs_block_rsv *block_rsv;
4630 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004631
4632 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4633 if (!block_rsv)
4634 return NULL;
4635
Miao Xie66d8f3d2012-09-06 04:02:28 -06004636 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004637 block_rsv->space_info = __find_space_info(fs_info,
4638 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004639 return block_rsv;
4640}
4641
4642void btrfs_free_block_rsv(struct btrfs_root *root,
4643 struct btrfs_block_rsv *rsv)
4644{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004645 if (!rsv)
4646 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004647 btrfs_block_rsv_release(root, rsv, (u64)-1);
4648 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004649}
4650
Miao Xie08e007d2012-10-16 11:33:38 +00004651int btrfs_block_rsv_add(struct btrfs_root *root,
4652 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4653 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004654{
4655 int ret;
4656
4657 if (num_bytes == 0)
4658 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004659
Miao Xie61b520a2011-11-10 20:45:05 -05004660 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004661 if (!ret) {
4662 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4663 return 0;
4664 }
4665
Yan, Zhengf0486c62010-05-16 10:46:25 -04004666 return ret;
4667}
4668
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004669int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004670 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004671{
4672 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004673 int ret = -ENOSPC;
4674
4675 if (!block_rsv)
4676 return 0;
4677
4678 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004679 num_bytes = div_factor(block_rsv->size, min_factor);
4680 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004681 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004682 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004683
Josef Bacik36ba0222011-10-18 12:15:48 -04004684 return ret;
4685}
4686
Miao Xie08e007d2012-10-16 11:33:38 +00004687int btrfs_block_rsv_refill(struct btrfs_root *root,
4688 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4689 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004690{
4691 u64 num_bytes = 0;
4692 int ret = -ENOSPC;
4693
4694 if (!block_rsv)
4695 return 0;
4696
4697 spin_lock(&block_rsv->lock);
4698 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004699 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004700 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004701 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004702 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004703 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004704
Yan, Zhengf0486c62010-05-16 10:46:25 -04004705 if (!ret)
4706 return 0;
4707
Miao Xieaa38a712011-11-18 17:43:00 +08004708 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004709 if (!ret) {
4710 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004711 return 0;
4712 }
4713
Josef Bacik13553e52011-08-08 13:33:21 -04004714 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004715}
4716
4717int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4718 struct btrfs_block_rsv *dst_rsv,
4719 u64 num_bytes)
4720{
4721 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4722}
4723
4724void btrfs_block_rsv_release(struct btrfs_root *root,
4725 struct btrfs_block_rsv *block_rsv,
4726 u64 num_bytes)
4727{
4728 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Liu Bo17504582013-12-29 21:44:50 +08004729 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04004730 block_rsv->space_info != global_rsv->space_info)
4731 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004732 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4733 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004734}
4735
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004736/*
4737 * helper to calculate size of global block reservation.
4738 * the desired value is sum of space used by extent tree,
4739 * checksum tree and root tree
4740 */
4741static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4742{
4743 struct btrfs_space_info *sinfo;
4744 u64 num_bytes;
4745 u64 meta_used;
4746 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004747 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004748
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004749 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4750 spin_lock(&sinfo->lock);
4751 data_used = sinfo->bytes_used;
4752 spin_unlock(&sinfo->lock);
4753
4754 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4755 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004756 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4757 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004758 meta_used = sinfo->bytes_used;
4759 spin_unlock(&sinfo->lock);
4760
4761 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4762 csum_size * 2;
4763 num_bytes += div64_u64(data_used + meta_used, 50);
4764
4765 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004766 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004767
4768 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4769}
4770
4771static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4772{
4773 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4774 struct btrfs_space_info *sinfo = block_rsv->space_info;
4775 u64 num_bytes;
4776
4777 num_bytes = calc_global_metadata_size(fs_info);
4778
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004779 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004780 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004781
Josef Bacikfdf30d12013-03-26 15:31:45 -04004782 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004783
4784 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004785 sinfo->bytes_reserved + sinfo->bytes_readonly +
4786 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004787
4788 if (sinfo->total_bytes > num_bytes) {
4789 num_bytes = sinfo->total_bytes - num_bytes;
4790 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004791 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004792 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004793 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004794 }
4795
4796 if (block_rsv->reserved >= block_rsv->size) {
4797 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004798 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004799 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004800 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004801 block_rsv->reserved = block_rsv->size;
4802 block_rsv->full = 1;
4803 }
David Sterba182608c2011-05-05 13:13:16 +02004804
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004805 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004806 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004807}
4808
Yan, Zhengf0486c62010-05-16 10:46:25 -04004809static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4810{
4811 struct btrfs_space_info *space_info;
4812
4813 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4814 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004815
4816 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004817 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004818 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004819 fs_info->trans_block_rsv.space_info = space_info;
4820 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004821 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004822
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004823 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4824 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4825 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4826 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00004827 if (fs_info->quota_root)
4828 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004829 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004830
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004831 update_global_block_rsv(fs_info);
4832}
4833
4834static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4835{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004836 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4837 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004838 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4839 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4840 WARN_ON(fs_info->trans_block_rsv.size > 0);
4841 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4842 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4843 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004844 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4845 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004846}
4847
Yan, Zhenga22285a2010-05-16 10:48:46 -04004848void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4849 struct btrfs_root *root)
4850{
Josef Bacik0e721102012-06-26 16:13:18 -04004851 if (!trans->block_rsv)
4852 return;
4853
Yan, Zhenga22285a2010-05-16 10:48:46 -04004854 if (!trans->bytes_reserved)
4855 return;
4856
Chris Masone77266e2012-02-24 10:39:05 -05004857 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004858 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004859 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004860 trans->bytes_reserved = 0;
4861}
4862
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004863/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004864int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4865 struct inode *inode)
4866{
4867 struct btrfs_root *root = BTRFS_I(inode)->root;
4868 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4869 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4870
4871 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004872 * We need to hold space in order to delete our orphan item once we've
4873 * added it, so this takes the reservation so we can release it later
4874 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004875 */
Chris Masonff5714c2011-05-28 07:00:39 -04004876 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004877 trace_btrfs_space_reservation(root->fs_info, "orphan",
4878 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004879 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4880}
4881
4882void btrfs_orphan_release_metadata(struct inode *inode)
4883{
4884 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004885 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004886 trace_btrfs_space_reservation(root->fs_info, "orphan",
4887 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004888 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4889}
4890
Miao Xied5c12072013-02-28 10:04:33 +00004891/*
4892 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4893 * root: the root of the parent directory
4894 * rsv: block reservation
4895 * items: the number of items that we need do reservation
4896 * qgroup_reserved: used to return the reserved size in qgroup
4897 *
4898 * This function is used to reserve the space for snapshot/subvolume
4899 * creation and deletion. Those operations are different with the
4900 * common file/directory operations, they change two fs/file trees
4901 * and root tree, the number of items that the qgroup reserves is
4902 * different with the free space reservation. So we can not use
4903 * the space reseravtion mechanism in start_transaction().
4904 */
4905int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4906 struct btrfs_block_rsv *rsv,
4907 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004908 u64 *qgroup_reserved,
4909 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004910{
Miao Xied5c12072013-02-28 10:04:33 +00004911 u64 num_bytes;
4912 int ret;
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004913 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00004914
4915 if (root->fs_info->quota_enabled) {
4916 /* One for parent inode, two for dir entries */
4917 num_bytes = 3 * root->leafsize;
4918 ret = btrfs_qgroup_reserve(root, num_bytes);
4919 if (ret)
4920 return ret;
4921 } else {
4922 num_bytes = 0;
4923 }
4924
4925 *qgroup_reserved = num_bytes;
4926
4927 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4928 rsv->space_info = __find_space_info(root->fs_info,
4929 BTRFS_BLOCK_GROUP_METADATA);
4930 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4931 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004932
4933 if (ret == -ENOSPC && use_global_rsv)
4934 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
4935
Miao Xied5c12072013-02-28 10:04:33 +00004936 if (ret) {
4937 if (*qgroup_reserved)
4938 btrfs_qgroup_free(root, *qgroup_reserved);
4939 }
4940
4941 return ret;
4942}
4943
4944void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4945 struct btrfs_block_rsv *rsv,
4946 u64 qgroup_reserved)
4947{
4948 btrfs_block_rsv_release(root, rsv, (u64)-1);
4949 if (qgroup_reserved)
4950 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004951}
4952
Josef Bacik7709cde2011-08-04 10:25:02 -04004953/**
4954 * drop_outstanding_extent - drop an outstanding extent
4955 * @inode: the inode we're dropping the extent for
4956 *
4957 * This is called when we are freeing up an outstanding extent, either called
4958 * after an error or after an extent is written. This will return the number of
4959 * reserved extents that need to be freed. This must be called with
4960 * BTRFS_I(inode)->lock held.
4961 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004962static unsigned drop_outstanding_extent(struct inode *inode)
4963{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004964 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004965 unsigned dropped_extents = 0;
4966
Josef Bacik9e0baf62011-07-15 15:16:44 +00004967 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4968 BTRFS_I(inode)->outstanding_extents--;
4969
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004970 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004971 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4972 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004973 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004974
Josef Bacik9e0baf62011-07-15 15:16:44 +00004975 /*
4976 * If we have more or the same amount of outsanding extents than we have
4977 * reserved then we need to leave the reserved extents count alone.
4978 */
4979 if (BTRFS_I(inode)->outstanding_extents >=
4980 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004981 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004982
4983 dropped_extents = BTRFS_I(inode)->reserved_extents -
4984 BTRFS_I(inode)->outstanding_extents;
4985 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004986 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004987}
4988
Josef Bacik7709cde2011-08-04 10:25:02 -04004989/**
4990 * calc_csum_metadata_size - return the amount of metada space that must be
4991 * reserved/free'd for the given bytes.
4992 * @inode: the inode we're manipulating
4993 * @num_bytes: the number of bytes in question
4994 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4995 *
4996 * This adjusts the number of csum_bytes in the inode and then returns the
4997 * correct amount of metadata that must either be reserved or freed. We
4998 * calculate how many checksums we can fit into one leaf and then divide the
4999 * number of bytes that will need to be checksumed by this value to figure out
5000 * how many checksums will be required. If we are adding bytes then the number
5001 * may go up and we will return the number of additional bytes that must be
5002 * reserved. If it is going down we will return the number of bytes that must
5003 * be freed.
5004 *
5005 * This must be called with BTRFS_I(inode)->lock held.
5006 */
5007static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5008 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005009{
Josef Bacik7709cde2011-08-04 10:25:02 -04005010 struct btrfs_root *root = BTRFS_I(inode)->root;
5011 u64 csum_size;
5012 int num_csums_per_leaf;
5013 int num_csums;
5014 int old_csums;
5015
5016 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5017 BTRFS_I(inode)->csum_bytes == 0)
5018 return 0;
5019
5020 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5021 if (reserve)
5022 BTRFS_I(inode)->csum_bytes += num_bytes;
5023 else
5024 BTRFS_I(inode)->csum_bytes -= num_bytes;
5025 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
5026 num_csums_per_leaf = (int)div64_u64(csum_size,
5027 sizeof(struct btrfs_csum_item) +
5028 sizeof(struct btrfs_disk_key));
5029 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5030 num_csums = num_csums + num_csums_per_leaf - 1;
5031 num_csums = num_csums / num_csums_per_leaf;
5032
5033 old_csums = old_csums + num_csums_per_leaf - 1;
5034 old_csums = old_csums / num_csums_per_leaf;
5035
5036 /* No change, no need to reserve more */
5037 if (old_csums == num_csums)
5038 return 0;
5039
5040 if (reserve)
5041 return btrfs_calc_trans_metadata_size(root,
5042 num_csums - old_csums);
5043
5044 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005045}
5046
5047int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5048{
5049 struct btrfs_root *root = BTRFS_I(inode)->root;
5050 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005051 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005052 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005053 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005054 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00005055 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07005056 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005057 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005058 u64 to_free = 0;
5059 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005060
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005061 /* If we are a free space inode we need to not flush since we will be in
5062 * the middle of a transaction commit. We also don't need the delalloc
5063 * mutex since we won't race with anybody. We need this mostly to make
5064 * lockdep shut its filthy mouth.
5065 */
5066 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00005067 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005068 delalloc_lock = false;
5069 }
Josef Bacikc09544e2011-08-30 10:19:10 -04005070
Miao Xie08e007d2012-10-16 11:33:38 +00005071 if (flush != BTRFS_RESERVE_NO_FLUSH &&
5072 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005073 schedule_timeout(1);
5074
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005075 if (delalloc_lock)
5076 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5077
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005078 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005079
Josef Bacik9e0baf62011-07-15 15:16:44 +00005080 spin_lock(&BTRFS_I(inode)->lock);
5081 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05005082
Josef Bacik9e0baf62011-07-15 15:16:44 +00005083 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05005084 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005085 nr_extents = BTRFS_I(inode)->outstanding_extents -
5086 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005087
5088 /*
5089 * Add an item to reserve for updating the inode when we complete the
5090 * delalloc io.
5091 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04005092 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5093 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005094 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05005095 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005096 }
5097
5098 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04005099 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05005100 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005101 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05005102
Wang Shilong88e081bf2013-03-01 11:36:01 +00005103 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02005104 ret = btrfs_qgroup_reserve(root, num_bytes +
5105 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005106 if (ret)
5107 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00005108 }
Arne Jansenc5567232011-09-14 15:44:05 +02005109
Wang Shilong88e081bf2013-03-01 11:36:01 +00005110 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
5111 if (unlikely(ret)) {
5112 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00005113 btrfs_qgroup_free(root, num_bytes +
5114 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005115 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005116 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005117
Josef Bacik660d3f62011-12-09 11:18:51 -05005118 spin_lock(&BTRFS_I(inode)->lock);
5119 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04005120 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5121 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05005122 nr_extents--;
5123 }
5124 BTRFS_I(inode)->reserved_extents += nr_extents;
5125 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005126
5127 if (delalloc_lock)
5128 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05005129
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005130 if (to_reserve)
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05305131 trace_btrfs_space_reservation(root->fs_info, "delalloc",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005132 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005133 block_rsv_add_bytes(block_rsv, to_reserve, 1);
5134
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005135 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005136
5137out_fail:
5138 spin_lock(&BTRFS_I(inode)->lock);
5139 dropped = drop_outstanding_extent(inode);
5140 /*
5141 * If the inodes csum_bytes is the same as the original
5142 * csum_bytes then we know we haven't raced with any free()ers
5143 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00005144 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04005145 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00005146 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04005147 } else {
5148 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5149 u64 bytes;
5150
5151 /*
5152 * This is tricky, but first we need to figure out how much we
5153 * free'd from any free-ers that occured during this
5154 * reservation, so we reset ->csum_bytes to the csum_bytes
5155 * before we dropped our lock, and then call the free for the
5156 * number of bytes that were freed while we were trying our
5157 * reservation.
5158 */
5159 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5160 BTRFS_I(inode)->csum_bytes = csum_bytes;
5161 to_free = calc_csum_metadata_size(inode, bytes, 0);
5162
5163
5164 /*
5165 * Now we need to see how much we would have freed had we not
5166 * been making this reservation and our ->csum_bytes were not
5167 * artificially inflated.
5168 */
5169 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5170 bytes = csum_bytes - orig_csum_bytes;
5171 bytes = calc_csum_metadata_size(inode, bytes, 0);
5172
5173 /*
5174 * Now reset ->csum_bytes to what it should be. If bytes is
5175 * more than to_free then we would have free'd more space had we
5176 * not had an artificially high ->csum_bytes, so we need to free
5177 * the remainder. If bytes is the same or less then we don't
5178 * need to do anything, the other free-ers did the correct
5179 * thing.
5180 */
5181 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5182 if (bytes > to_free)
5183 to_free = bytes - to_free;
5184 else
5185 to_free = 0;
5186 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005187 spin_unlock(&BTRFS_I(inode)->lock);
5188 if (dropped)
5189 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5190
5191 if (to_free) {
5192 btrfs_block_rsv_release(root, block_rsv, to_free);
5193 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5194 btrfs_ino(inode), to_free, 0);
5195 }
5196 if (delalloc_lock)
5197 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5198 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005199}
5200
Josef Bacik7709cde2011-08-04 10:25:02 -04005201/**
5202 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5203 * @inode: the inode to release the reservation for
5204 * @num_bytes: the number of bytes we're releasing
5205 *
5206 * This will release the metadata reservation for an inode. This can be called
5207 * once we complete IO for a given set of bytes to release their metadata
5208 * reservations.
5209 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005210void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5211{
5212 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005213 u64 to_free = 0;
5214 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005215
5216 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005217 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005218 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005219
Miao Xie09348562013-02-07 10:12:07 +00005220 if (num_bytes)
5221 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005222 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005223 if (dropped > 0)
5224 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005225
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005226 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5227 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005228 if (root->fs_info->quota_enabled) {
5229 btrfs_qgroup_free(root, num_bytes +
5230 dropped * root->leafsize);
5231 }
5232
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005233 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5234 to_free);
5235}
5236
Josef Bacik7709cde2011-08-04 10:25:02 -04005237/**
5238 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5239 * @inode: inode we're writing to
5240 * @num_bytes: the number of bytes we want to allocate
5241 *
5242 * This will do the following things
5243 *
5244 * o reserve space in the data space info for num_bytes
5245 * o reserve space in the metadata space info based on number of outstanding
5246 * extents and how much csums will be needed
5247 * o add to the inodes ->delalloc_bytes
5248 * o add it to the fs_info's delalloc inodes list.
5249 *
5250 * This will return 0 for success and -ENOSPC if there is no space left.
5251 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005252int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5253{
5254 int ret;
5255
5256 ret = btrfs_check_data_free_space(inode, num_bytes);
5257 if (ret)
5258 return ret;
5259
5260 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5261 if (ret) {
5262 btrfs_free_reserved_data_space(inode, num_bytes);
5263 return ret;
5264 }
5265
5266 return 0;
5267}
5268
Josef Bacik7709cde2011-08-04 10:25:02 -04005269/**
5270 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5271 * @inode: inode we're releasing space for
5272 * @num_bytes: the number of bytes we want to free up
5273 *
5274 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5275 * called in the case that we don't need the metadata AND data reservations
5276 * anymore. So if there is an error or we insert an inline extent.
5277 *
5278 * This function will release the metadata space that was not used and will
5279 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5280 * list if there are no delalloc bytes left.
5281 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005282void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5283{
5284 btrfs_delalloc_release_metadata(inode, num_bytes);
5285 btrfs_free_reserved_data_space(inode, num_bytes);
5286}
5287
Liu Boc53d6132012-12-27 09:01:19 +00005288static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005289 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005290{
Josef Bacik0af3d002010-06-21 14:48:16 -04005291 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005292 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005293 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005294 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005295 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005296 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005297
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005298 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005299 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005300 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005301 if (alloc)
5302 old_val += num_bytes;
5303 else
5304 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005305 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005306 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005307
Chris Masond3977122009-01-05 21:25:51 -05005308 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005309 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005310 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005311 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005312 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5313 BTRFS_BLOCK_GROUP_RAID1 |
5314 BTRFS_BLOCK_GROUP_RAID10))
5315 factor = 2;
5316 else
5317 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005318 /*
5319 * If this block group has free space cache written out, we
5320 * need to make sure to load it if we are removing space. This
5321 * is because we need the unpinning stage to actually add the
5322 * space back to the block group, otherwise we will leak space.
5323 */
5324 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005325 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005326
Chris Masondb945352007-10-15 16:15:53 -04005327 byte_in_group = bytenr - cache->key.objectid;
5328 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005329
Josef Bacik25179202008-10-29 14:49:05 -04005330 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005331 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005332
Josef Bacik73bc1872011-10-03 14:07:49 -04005333 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005334 cache->disk_cache_state < BTRFS_DC_CLEAR)
5335 cache->disk_cache_state = BTRFS_DC_CLEAR;
5336
Josef Bacik0f9dd462008-09-23 13:14:11 -04005337 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005338 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005339 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005340 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005341 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005342 btrfs_set_block_group_used(&cache->item, old_val);
5343 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005344 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005345 cache->space_info->bytes_used += num_bytes;
5346 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005347 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005348 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005349 } else {
Chris Masondb945352007-10-15 16:15:53 -04005350 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005351 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005352 cache->pinned += num_bytes;
5353 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005354 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005355 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005356 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005357 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005358
Yan, Zhengf0486c62010-05-16 10:46:25 -04005359 set_extent_dirty(info->pinned_extents,
5360 bytenr, bytenr + num_bytes - 1,
5361 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005362 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005363 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005364 total -= num_bytes;
5365 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005366 }
5367 return 0;
5368}
Chris Mason6324fbf2008-03-24 15:01:59 -04005369
Chris Masona061fc82008-05-07 11:43:44 -04005370static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5371{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005372 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005373 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005374
Liu Boa1897fd2012-12-27 09:01:23 +00005375 spin_lock(&root->fs_info->block_group_cache_lock);
5376 bytenr = root->fs_info->first_logical_byte;
5377 spin_unlock(&root->fs_info->block_group_cache_lock);
5378
5379 if (bytenr < (u64)-1)
5380 return bytenr;
5381
Josef Bacik0f9dd462008-09-23 13:14:11 -04005382 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5383 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005384 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005385
Yan Zhengd2fb3432008-12-11 16:30:39 -05005386 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005387 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005388
5389 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005390}
5391
Yan, Zhengf0486c62010-05-16 10:46:25 -04005392static int pin_down_extent(struct btrfs_root *root,
5393 struct btrfs_block_group_cache *cache,
5394 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005395{
Yan Zheng11833d62009-09-11 16:11:19 -04005396 spin_lock(&cache->space_info->lock);
5397 spin_lock(&cache->lock);
5398 cache->pinned += num_bytes;
5399 cache->space_info->bytes_pinned += num_bytes;
5400 if (reserved) {
5401 cache->reserved -= num_bytes;
5402 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005403 }
Yan Zheng11833d62009-09-11 16:11:19 -04005404 spin_unlock(&cache->lock);
5405 spin_unlock(&cache->space_info->lock);
5406
Yan, Zhengf0486c62010-05-16 10:46:25 -04005407 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5408 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Josef Bacik0be5dc62013-10-07 15:18:52 -04005409 if (reserved)
5410 trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
Yan324ae4d2007-11-16 14:57:08 -05005411 return 0;
5412}
Chris Mason9078a3e2007-04-26 16:46:15 -04005413
Yan, Zhengf0486c62010-05-16 10:46:25 -04005414/*
5415 * this function must be called within transaction
5416 */
5417int btrfs_pin_extent(struct btrfs_root *root,
5418 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005419{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005420 struct btrfs_block_group_cache *cache;
5421
5422 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005423 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005424
5425 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5426
5427 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005428 return 0;
5429}
Zheng Yane8569812008-09-26 10:05:48 -04005430
Yan, Zhengf0486c62010-05-16 10:46:25 -04005431/*
Chris Masone688b7252011-10-31 20:52:39 -04005432 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005433 */
Liu Bodcfac412012-12-27 09:01:20 +00005434int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005435 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005436{
Chris Masone688b7252011-10-31 20:52:39 -04005437 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005438 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04005439
5440 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005441 if (!cache)
5442 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04005443
5444 /*
5445 * pull in the free space cache (if any) so that our pin
5446 * removes the free space from the cache. We have load_only set
5447 * to one because the slow code to read in the free extents does check
5448 * the pinned extents.
5449 */
Liu Bof6373bf2012-12-27 09:01:18 +00005450 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005451
5452 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5453
5454 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005455 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04005456 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005457 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04005458}
5459
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005460static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5461{
5462 int ret;
5463 struct btrfs_block_group_cache *block_group;
5464 struct btrfs_caching_control *caching_ctl;
5465
5466 block_group = btrfs_lookup_block_group(root->fs_info, start);
5467 if (!block_group)
5468 return -EINVAL;
5469
5470 cache_block_group(block_group, 0);
5471 caching_ctl = get_caching_control(block_group);
5472
5473 if (!caching_ctl) {
5474 /* Logic error */
5475 BUG_ON(!block_group_cache_done(block_group));
5476 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5477 } else {
5478 mutex_lock(&caching_ctl->mutex);
5479
5480 if (start >= caching_ctl->progress) {
5481 ret = add_excluded_extent(root, start, num_bytes);
5482 } else if (start + num_bytes <= caching_ctl->progress) {
5483 ret = btrfs_remove_free_space(block_group,
5484 start, num_bytes);
5485 } else {
5486 num_bytes = caching_ctl->progress - start;
5487 ret = btrfs_remove_free_space(block_group,
5488 start, num_bytes);
5489 if (ret)
5490 goto out_lock;
5491
5492 num_bytes = (start + num_bytes) -
5493 caching_ctl->progress;
5494 start = caching_ctl->progress;
5495 ret = add_excluded_extent(root, start, num_bytes);
5496 }
5497out_lock:
5498 mutex_unlock(&caching_ctl->mutex);
5499 put_caching_control(caching_ctl);
5500 }
5501 btrfs_put_block_group(block_group);
5502 return ret;
5503}
5504
5505int btrfs_exclude_logged_extents(struct btrfs_root *log,
5506 struct extent_buffer *eb)
5507{
5508 struct btrfs_file_extent_item *item;
5509 struct btrfs_key key;
5510 int found_type;
5511 int i;
5512
5513 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5514 return 0;
5515
5516 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5517 btrfs_item_key_to_cpu(eb, &key, i);
5518 if (key.type != BTRFS_EXTENT_DATA_KEY)
5519 continue;
5520 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5521 found_type = btrfs_file_extent_type(eb, item);
5522 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5523 continue;
5524 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5525 continue;
5526 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5527 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5528 __exclude_logged_extent(log, key.objectid, key.offset);
5529 }
5530
5531 return 0;
5532}
5533
Josef Bacikfb25e912011-07-26 17:00:46 -04005534/**
5535 * btrfs_update_reserved_bytes - update the block_group and space info counters
5536 * @cache: The cache we are manipulating
5537 * @num_bytes: The number of bytes in question
5538 * @reserve: One of the reservation enums
5539 *
5540 * This is called by the allocator when it reserves space, or by somebody who is
5541 * freeing space that was never actually used on disk. For example if you
5542 * reserve some space for a new leaf in transaction A and before transaction A
5543 * commits you free that leaf, you call this with reserve set to 0 in order to
5544 * clear the reservation.
5545 *
5546 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5547 * ENOSPC accounting. For data we handle the reservation through clearing the
5548 * delalloc bits in the io_tree. We have to do this since we could end up
5549 * allocating less disk space for the amount of data we have reserved in the
5550 * case of compression.
5551 *
5552 * If this is a reservation and the block group has become read only we cannot
5553 * make the reservation and return -EAGAIN, otherwise this function always
5554 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005555 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005556static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5557 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005558{
Josef Bacikfb25e912011-07-26 17:00:46 -04005559 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005560 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005561
Josef Bacikfb25e912011-07-26 17:00:46 -04005562 spin_lock(&space_info->lock);
5563 spin_lock(&cache->lock);
5564 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005565 if (cache->ro) {
5566 ret = -EAGAIN;
5567 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005568 cache->reserved += num_bytes;
5569 space_info->bytes_reserved += num_bytes;
5570 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005571 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005572 "space_info", space_info->flags,
5573 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005574 space_info->bytes_may_use -= num_bytes;
5575 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005576 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005577 } else {
5578 if (cache->ro)
5579 space_info->bytes_readonly += num_bytes;
5580 cache->reserved -= num_bytes;
5581 space_info->bytes_reserved -= num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005582 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005583 spin_unlock(&cache->lock);
5584 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005585 return ret;
5586}
5587
Jeff Mahoney143bede2012-03-01 14:56:26 +01005588void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005589 struct btrfs_root *root)
5590{
5591 struct btrfs_fs_info *fs_info = root->fs_info;
5592 struct btrfs_caching_control *next;
5593 struct btrfs_caching_control *caching_ctl;
5594 struct btrfs_block_group_cache *cache;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005595 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04005596
Josef Bacik9e351cc2014-03-13 15:42:13 -04005597 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04005598
5599 list_for_each_entry_safe(caching_ctl, next,
5600 &fs_info->caching_block_groups, list) {
5601 cache = caching_ctl->block_group;
5602 if (block_group_cache_done(cache)) {
5603 cache->last_byte_to_unpin = (u64)-1;
5604 list_del_init(&caching_ctl->list);
5605 put_caching_control(caching_ctl);
5606 } else {
5607 cache->last_byte_to_unpin = caching_ctl->progress;
5608 }
5609 }
5610
5611 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5612 fs_info->pinned_extents = &fs_info->freed_extents[1];
5613 else
5614 fs_info->pinned_extents = &fs_info->freed_extents[0];
5615
Josef Bacik9e351cc2014-03-13 15:42:13 -04005616 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005617
Josef Bacikb150a4f2013-06-19 15:00:04 -04005618 list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
5619 percpu_counter_set(&space_info->total_bytes_pinned, 0);
5620
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005621 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005622}
5623
5624static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5625{
5626 struct btrfs_fs_info *fs_info = root->fs_info;
5627 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005628 struct btrfs_space_info *space_info;
5629 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005630 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005631 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005632
5633 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005634 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005635 if (!cache ||
5636 start >= cache->key.objectid + cache->key.offset) {
5637 if (cache)
5638 btrfs_put_block_group(cache);
5639 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005640 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005641 }
5642
5643 len = cache->key.objectid + cache->key.offset - start;
5644 len = min(len, end + 1 - start);
5645
5646 if (start < cache->last_byte_to_unpin) {
5647 len = min(len, cache->last_byte_to_unpin - start);
5648 btrfs_add_free_space(cache, start, len);
5649 }
Josef Bacik25179202008-10-29 14:49:05 -04005650
Yan, Zhengf0486c62010-05-16 10:46:25 -04005651 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005652 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005653
Josef Bacik7b398f82012-10-22 15:52:28 -04005654 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005655 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005656 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005657 space_info->bytes_pinned -= len;
5658 if (cache->ro) {
5659 space_info->bytes_readonly += len;
5660 readonly = true;
5661 }
Josef Bacik25179202008-10-29 14:49:05 -04005662 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005663 if (!readonly && global_rsv->space_info == space_info) {
5664 spin_lock(&global_rsv->lock);
5665 if (!global_rsv->full) {
5666 len = min(len, global_rsv->size -
5667 global_rsv->reserved);
5668 global_rsv->reserved += len;
5669 space_info->bytes_may_use += len;
5670 if (global_rsv->reserved >= global_rsv->size)
5671 global_rsv->full = 1;
5672 }
5673 spin_unlock(&global_rsv->lock);
5674 }
5675 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005676 }
5677
5678 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005679 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005680 return 0;
5681}
5682
5683int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005684 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005685{
Yan Zheng11833d62009-09-11 16:11:19 -04005686 struct btrfs_fs_info *fs_info = root->fs_info;
5687 struct extent_io_tree *unpin;
Chris Mason1a5bc1672007-10-15 16:15:26 -04005688 u64 start;
5689 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005690 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005691
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005692 if (trans->aborted)
5693 return 0;
5694
Yan Zheng11833d62009-09-11 16:11:19 -04005695 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5696 unpin = &fs_info->freed_extents[1];
5697 else
5698 unpin = &fs_info->freed_extents[0];
5699
Chris Masond3977122009-01-05 21:25:51 -05005700 while (1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04005701 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005702 EXTENT_DIRTY, NULL);
Chris Mason1a5bc1672007-10-15 16:15:26 -04005703 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005704 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005705
Li Dongyang5378e602011-03-24 10:24:27 +00005706 if (btrfs_test_opt(root, DISCARD))
5707 ret = btrfs_discard_extent(root, start,
5708 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005709
Chris Mason1a5bc1672007-10-15 16:15:26 -04005710 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005711 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005712 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005713 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005714
Chris Masone20d96d2007-03-22 12:13:20 -04005715 return 0;
5716}
5717
Josef Bacikb150a4f2013-06-19 15:00:04 -04005718static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5719 u64 owner, u64 root_objectid)
5720{
5721 struct btrfs_space_info *space_info;
5722 u64 flags;
5723
5724 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5725 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5726 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5727 else
5728 flags = BTRFS_BLOCK_GROUP_METADATA;
5729 } else {
5730 flags = BTRFS_BLOCK_GROUP_DATA;
5731 }
5732
5733 space_info = __find_space_info(fs_info, flags);
5734 BUG_ON(!space_info); /* Logic bug */
5735 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5736}
5737
5738
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005739static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5740 struct btrfs_root *root,
5741 u64 bytenr, u64 num_bytes, u64 parent,
5742 u64 root_objectid, u64 owner_objectid,
5743 u64 owner_offset, int refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07005744 struct btrfs_delayed_extent_op *extent_op,
5745 int no_quota)
Chris Masona28ec192007-03-06 20:08:01 -05005746{
Chris Masone2fa7222007-03-12 16:22:34 -04005747 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005748 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005749 struct btrfs_fs_info *info = root->fs_info;
5750 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005751 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005752 struct btrfs_extent_item *ei;
5753 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005754 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005755 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005756 int extent_slot = 0;
5757 int found_extent = 0;
5758 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005759 u32 item_size;
5760 u64 refs;
Josef Bacikfcebe452014-05-13 17:30:47 -07005761 int last_ref = 0;
5762 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_SUB_EXCL;
Josef Bacik3173a182013-03-07 14:22:04 -05005763 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5764 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005765
Josef Bacikfcebe452014-05-13 17:30:47 -07005766 if (!info->quota_enabled || !is_fstree(root_objectid))
5767 no_quota = 1;
5768
Chris Mason5caf2a02007-04-02 11:20:42 -04005769 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005770 if (!path)
5771 return -ENOMEM;
5772
Chris Mason3c12ac72008-04-21 12:01:38 -04005773 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005774 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005775
5776 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5777 BUG_ON(!is_data && refs_to_drop != 1);
5778
Josef Bacik3173a182013-03-07 14:22:04 -05005779 if (is_data)
5780 skinny_metadata = 0;
5781
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005782 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5783 bytenr, num_bytes, parent,
5784 root_objectid, owner_objectid,
5785 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005786 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005787 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005788 while (extent_slot >= 0) {
5789 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005790 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005791 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005792 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005793 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5794 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005795 found_extent = 1;
5796 break;
5797 }
Josef Bacik3173a182013-03-07 14:22:04 -05005798 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5799 key.offset == owner_objectid) {
5800 found_extent = 1;
5801 break;
5802 }
Chris Mason952fcca2008-02-18 16:33:44 -05005803 if (path->slots[0] - extent_slot > 5)
5804 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005805 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005806 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005807#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5808 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5809 if (found_extent && item_size < sizeof(*ei))
5810 found_extent = 0;
5811#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005812 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005813 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005814 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005815 NULL, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07005816 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06005817 if (ret) {
5818 btrfs_abort_transaction(trans, extent_root, ret);
5819 goto out;
5820 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005821 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005822 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005823
5824 key.objectid = bytenr;
5825 key.type = BTRFS_EXTENT_ITEM_KEY;
5826 key.offset = num_bytes;
5827
Josef Bacik3173a182013-03-07 14:22:04 -05005828 if (!is_data && skinny_metadata) {
5829 key.type = BTRFS_METADATA_ITEM_KEY;
5830 key.offset = owner_objectid;
5831 }
5832
Zheng Yan31840ae2008-09-23 13:14:14 -04005833 ret = btrfs_search_slot(trans, extent_root,
5834 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005835 if (ret > 0 && skinny_metadata && path->slots[0]) {
5836 /*
5837 * Couldn't find our skinny metadata item,
5838 * see if we have ye olde extent item.
5839 */
5840 path->slots[0]--;
5841 btrfs_item_key_to_cpu(path->nodes[0], &key,
5842 path->slots[0]);
5843 if (key.objectid == bytenr &&
5844 key.type == BTRFS_EXTENT_ITEM_KEY &&
5845 key.offset == num_bytes)
5846 ret = 0;
5847 }
5848
5849 if (ret > 0 && skinny_metadata) {
5850 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01005851 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05005852 key.type = BTRFS_EXTENT_ITEM_KEY;
5853 key.offset = num_bytes;
5854 btrfs_release_path(path);
5855 ret = btrfs_search_slot(trans, extent_root,
5856 &key, path, -1, 1);
5857 }
5858
Josef Bacikf3465ca2008-11-12 14:19:50 -05005859 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005860 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005861 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005862 if (ret > 0)
5863 btrfs_print_leaf(extent_root,
5864 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005865 }
David Sterba005d6422012-09-18 07:52:32 -06005866 if (ret < 0) {
5867 btrfs_abort_transaction(trans, extent_root, ret);
5868 goto out;
5869 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005870 extent_slot = path->slots[0];
5871 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305872 } else if (WARN_ON(ret == -ENOENT)) {
Chris Mason7bb86312007-12-11 09:25:06 -05005873 btrfs_print_leaf(extent_root, path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005874 btrfs_err(info,
5875 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005876 bytenr, parent, root_objectid, owner_objectid,
5877 owner_offset);
Josef Bacikc4a050b2014-03-14 16:36:53 -04005878 btrfs_abort_transaction(trans, extent_root, ret);
5879 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005880 } else {
David Sterba005d6422012-09-18 07:52:32 -06005881 btrfs_abort_transaction(trans, extent_root, ret);
5882 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005883 }
Chris Mason5f39d392007-10-15 16:14:19 -04005884
5885 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005886 item_size = btrfs_item_size_nr(leaf, extent_slot);
5887#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5888 if (item_size < sizeof(*ei)) {
5889 BUG_ON(found_extent || extent_slot != path->slots[0]);
5890 ret = convert_extent_item_v0(trans, extent_root, path,
5891 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005892 if (ret < 0) {
5893 btrfs_abort_transaction(trans, extent_root, ret);
5894 goto out;
5895 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005896
David Sterbab3b4aa72011-04-21 01:20:15 +02005897 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005898 path->leave_spinning = 1;
5899
5900 key.objectid = bytenr;
5901 key.type = BTRFS_EXTENT_ITEM_KEY;
5902 key.offset = num_bytes;
5903
5904 ret = btrfs_search_slot(trans, extent_root, &key, path,
5905 -1, 1);
5906 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005907 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005908 ret, bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005909 btrfs_print_leaf(extent_root, path->nodes[0]);
5910 }
David Sterba005d6422012-09-18 07:52:32 -06005911 if (ret < 0) {
5912 btrfs_abort_transaction(trans, extent_root, ret);
5913 goto out;
5914 }
5915
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005916 extent_slot = path->slots[0];
5917 leaf = path->nodes[0];
5918 item_size = btrfs_item_size_nr(leaf, extent_slot);
5919 }
5920#endif
5921 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005922 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005923 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005924 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5925 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005926 struct btrfs_tree_block_info *bi;
5927 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5928 bi = (struct btrfs_tree_block_info *)(ei + 1);
5929 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005930 }
5931
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005932 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04005933 if (refs < refs_to_drop) {
5934 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
David Sterba351fd352014-05-15 16:48:20 +02005935 "for bytenr %Lu", refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04005936 ret = -EINVAL;
5937 btrfs_abort_transaction(trans, extent_root, ret);
5938 goto out;
5939 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005940 refs -= refs_to_drop;
5941
5942 if (refs > 0) {
Josef Bacikfcebe452014-05-13 17:30:47 -07005943 type = BTRFS_QGROUP_OPER_SUB_SHARED;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005944 if (extent_op)
5945 __run_delayed_extent_op(extent_op, leaf, ei);
5946 /*
5947 * In the case of inline back ref, reference count will
5948 * be updated by remove_extent_backref
5949 */
5950 if (iref) {
5951 BUG_ON(!found_extent);
5952 } else {
5953 btrfs_set_extent_refs(leaf, ei, refs);
5954 btrfs_mark_buffer_dirty(leaf);
5955 }
5956 if (found_extent) {
5957 ret = remove_extent_backref(trans, extent_root, path,
5958 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07005959 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06005960 if (ret) {
5961 btrfs_abort_transaction(trans, extent_root, ret);
5962 goto out;
5963 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005964 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04005965 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
5966 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005967 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005968 if (found_extent) {
5969 BUG_ON(is_data && refs_to_drop !=
5970 extent_data_ref_count(root, path, iref));
5971 if (iref) {
5972 BUG_ON(path->slots[0] != extent_slot);
5973 } else {
5974 BUG_ON(path->slots[0] != extent_slot + 1);
5975 path->slots[0] = extent_slot;
5976 num_to_del = 2;
5977 }
Chris Mason78fae272007-03-25 11:35:08 -04005978 }
Chris Masonb9473432009-03-13 11:00:37 -04005979
Josef Bacikfcebe452014-05-13 17:30:47 -07005980 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05005981 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5982 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005983 if (ret) {
5984 btrfs_abort_transaction(trans, extent_root, ret);
5985 goto out;
5986 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005987 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005988
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005989 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005990 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005991 if (ret) {
5992 btrfs_abort_transaction(trans, extent_root, ret);
5993 goto out;
5994 }
Chris Mason459931e2008-12-10 09:10:46 -05005995 }
5996
Liu Boc53d6132012-12-27 09:01:19 +00005997 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005998 if (ret) {
5999 btrfs_abort_transaction(trans, extent_root, ret);
6000 goto out;
6001 }
Chris Masona28ec192007-03-06 20:08:01 -05006002 }
Josef Bacikfcebe452014-05-13 17:30:47 -07006003 btrfs_release_path(path);
6004
6005 /* Deal with the quota accounting */
6006 if (!ret && last_ref && !no_quota) {
6007 int mod_seq = 0;
6008
6009 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
6010 type == BTRFS_QGROUP_OPER_SUB_SHARED)
6011 mod_seq = 1;
6012
6013 ret = btrfs_qgroup_record_ref(trans, info, root_objectid,
6014 bytenr, num_bytes, type,
6015 mod_seq);
6016 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006017out:
Chris Mason5caf2a02007-04-02 11:20:42 -04006018 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05006019 return ret;
6020}
6021
6022/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006023 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04006024 * delayed ref for that extent as well. This searches the delayed ref tree for
6025 * a given extent, and if there are no other delayed refs to be processed, it
6026 * removes it from the tree.
6027 */
6028static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
6029 struct btrfs_root *root, u64 bytenr)
6030{
6031 struct btrfs_delayed_ref_head *head;
6032 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006033 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04006034
6035 delayed_refs = &trans->transaction->delayed_refs;
6036 spin_lock(&delayed_refs->lock);
6037 head = btrfs_find_delayed_ref_head(trans, bytenr);
6038 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08006039 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04006040
Josef Bacikd7df2c72014-01-23 09:21:38 -05006041 spin_lock(&head->lock);
6042 if (rb_first(&head->ref_root))
Chris Mason1887be62009-03-13 10:11:24 -04006043 goto out;
6044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006045 if (head->extent_op) {
6046 if (!head->must_insert_reserved)
6047 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00006048 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006049 head->extent_op = NULL;
6050 }
6051
Chris Mason1887be62009-03-13 10:11:24 -04006052 /*
6053 * waiting for the lock here would deadlock. If someone else has it
6054 * locked they are already in the process of dropping it anyway
6055 */
6056 if (!mutex_trylock(&head->mutex))
6057 goto out;
6058
6059 /*
6060 * at this point we have a head with no other entries. Go
6061 * ahead and process it.
6062 */
6063 head->node.in_tree = 0;
Liu Boc46effa2013-10-14 12:59:45 +08006064 rb_erase(&head->href_node, &delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -04006065
Josef Bacikd7df2c72014-01-23 09:21:38 -05006066 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04006067
6068 /*
6069 * we don't take a ref on the node because we're removing it from the
6070 * tree, so we just steal the ref the tree was holding.
6071 */
Chris Masonc3e69d52009-03-13 10:17:05 -04006072 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006073 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04006074 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006075 head->processing = 0;
6076 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04006077 spin_unlock(&delayed_refs->lock);
6078
Yan, Zhengf0486c62010-05-16 10:46:25 -04006079 BUG_ON(head->extent_op);
6080 if (head->must_insert_reserved)
6081 ret = 1;
6082
6083 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04006084 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006085 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04006086out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05006087 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08006088
6089out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04006090 spin_unlock(&delayed_refs->lock);
6091 return 0;
6092}
6093
Yan, Zhengf0486c62010-05-16 10:46:25 -04006094void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
6095 struct btrfs_root *root,
6096 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02006097 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006098{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006099 struct btrfs_block_group_cache *cache = NULL;
Josef Bacikb150a4f2013-06-19 15:00:04 -04006100 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006101 int ret;
6102
6103 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006104 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6105 buf->start, buf->len,
6106 parent, root->root_key.objectid,
6107 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02006108 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006109 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006110 }
6111
6112 if (!last_ref)
6113 return;
6114
Yan, Zhengf0486c62010-05-16 10:46:25 -04006115 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006116
6117 if (btrfs_header_generation(buf) == trans->transid) {
6118 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6119 ret = check_ref_cleanup(trans, root, buf->start);
6120 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04006121 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006122 }
6123
6124 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
6125 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04006126 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006127 }
6128
6129 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6130
6131 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04006132 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Josef Bacik0be5dc62013-10-07 15:18:52 -04006133 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
Josef Bacikb150a4f2013-06-19 15:00:04 -04006134 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006135 }
6136out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04006137 if (pin)
6138 add_pinned_bytes(root->fs_info, buf->len,
6139 btrfs_header_level(buf),
6140 root->root_key.objectid);
6141
Josef Bacika826d6d2011-03-16 13:42:43 -04006142 /*
6143 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6144 * anymore.
6145 */
6146 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006147 btrfs_put_block_group(cache);
6148}
6149
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006150/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006151int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6152 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Josef Bacikfcebe452014-05-13 17:30:47 -07006153 u64 owner, u64 offset, int no_quota)
Chris Mason925baed2008-06-25 16:01:30 -04006154{
6155 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006156 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04006157
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04006158#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
6159 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
6160 return 0;
6161#endif
Josef Bacikb150a4f2013-06-19 15:00:04 -04006162 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
6163
Chris Mason56bec292009-03-13 10:10:06 -04006164 /*
6165 * tree log blocks never actually go into the extent allocation
6166 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04006167 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006168 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6169 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04006170 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04006171 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04006172 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006173 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006174 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6175 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006176 parent, root_objectid, (int)owner,
Josef Bacikfcebe452014-05-13 17:30:47 -07006177 BTRFS_DROP_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006178 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006179 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6180 num_bytes,
6181 parent, root_objectid, owner,
6182 offset, BTRFS_DROP_DELAYED_REF,
Josef Bacikfcebe452014-05-13 17:30:47 -07006183 NULL, no_quota);
Chris Mason56bec292009-03-13 10:10:06 -04006184 }
Chris Mason925baed2008-06-25 16:01:30 -04006185 return ret;
6186}
6187
David Woodhouse53b381b2013-01-29 18:40:14 -05006188static u64 stripe_align(struct btrfs_root *root,
6189 struct btrfs_block_group_cache *cache,
6190 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05006191{
Qu Wenruofda28322013-02-26 08:10:22 +00006192 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05006193 return ret;
6194}
6195
Chris Masonfec577f2007-02-26 10:40:21 -05006196/*
Josef Bacik817d52f2009-07-13 21:29:25 -04006197 * when we wait for progress in the block group caching, its because
6198 * our allocation attempt failed at least once. So, we must sleep
6199 * and let some progress happen before we try again.
6200 *
6201 * This function will sleep at least once waiting for new free space to
6202 * show up, and then it will check the block group free space numbers
6203 * for our min num_bytes. Another option is to have it go ahead
6204 * and look in the rbtree for a free extent of a given size, but this
6205 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04006206 *
6207 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6208 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04006209 */
Josef Bacik36cce922013-08-05 11:15:21 -04006210static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04006211wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6212 u64 num_bytes)
6213{
Yan Zheng11833d62009-09-11 16:11:19 -04006214 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006215
Yan Zheng11833d62009-09-11 16:11:19 -04006216 caching_ctl = get_caching_control(cache);
6217 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006218 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04006219
Yan Zheng11833d62009-09-11 16:11:19 -04006220 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08006221 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006222
6223 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04006224}
6225
6226static noinline int
6227wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6228{
6229 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04006230 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006231
6232 caching_ctl = get_caching_control(cache);
6233 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006234 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006235
6236 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04006237 if (cache->cached == BTRFS_CACHE_ERROR)
6238 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04006239 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04006240 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04006241}
6242
Liu Bo31e50222012-11-21 14:18:10 +00006243int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04006244{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006245 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006246 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006247 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006248 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006249 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006250 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006251 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006252 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006253 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006254 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006255 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006256 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006257
Chris Masone942f882013-02-20 14:06:05 -05006258 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04006259}
6260
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006261int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03006262{
Liu Bo31e50222012-11-21 14:18:10 +00006263 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006264}
6265
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006266static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
6267 [BTRFS_RAID_RAID10] = "raid10",
6268 [BTRFS_RAID_RAID1] = "raid1",
6269 [BTRFS_RAID_DUP] = "dup",
6270 [BTRFS_RAID_RAID0] = "raid0",
6271 [BTRFS_RAID_SINGLE] = "single",
6272 [BTRFS_RAID_RAID5] = "raid5",
6273 [BTRFS_RAID_RAID6] = "raid6",
6274};
6275
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05006276static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006277{
6278 if (type >= BTRFS_NR_RAID_TYPES)
6279 return NULL;
6280
6281 return btrfs_raid_type_names[type];
6282}
6283
Josef Bacik817d52f2009-07-13 21:29:25 -04006284enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006285 LOOP_CACHING_NOWAIT = 0,
6286 LOOP_CACHING_WAIT = 1,
6287 LOOP_ALLOC_CHUNK = 2,
6288 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006289};
6290
6291/*
Chris Masonfec577f2007-02-26 10:40:21 -05006292 * walks the btree of allocated extents and find a hole of a given size.
6293 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08006294 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04006295 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08006296 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05006297 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08006298 *
6299 * If there is no suitable free space, we will record the max size of
6300 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05006301 */
Josef Bacik00361582013-08-14 14:02:47 -04006302static noinline int find_free_extent(struct btrfs_root *orig_root,
Chris Mason98ed5172008-01-03 10:01:48 -05006303 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05006304 u64 hint_byte, struct btrfs_key *ins,
David Sterbab6919a52013-04-29 13:39:40 +00006305 u64 flags)
Chris Masonfec577f2007-02-26 10:40:21 -05006306{
Josef Bacik80eb2342008-10-29 14:49:05 -04006307 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05006308 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006309 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04006310 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05006311 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08006312 u64 max_extent_size = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04006313 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04006314 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006315 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00006316 int index = __get_raid_index(flags);
6317 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04006318 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik0a243252009-09-11 16:11:20 -04006319 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006320 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04006321 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006322 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006323
Chris Masondb945352007-10-15 16:15:53 -04006324 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04006325 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04006326 ins->objectid = 0;
6327 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006328
David Sterbab6919a52013-04-29 13:39:40 +00006329 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006330
David Sterbab6919a52013-04-29 13:39:40 +00006331 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006332 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006333 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006334 return -ENOSPC;
6335 }
Josef Bacik2552d172009-04-03 10:14:19 -04006336
Josef Bacik67377732010-09-16 16:19:09 -04006337 /*
6338 * If the space info is for both data and metadata it means we have a
6339 * small filesystem and we can't use the clustering stuff.
6340 */
6341 if (btrfs_mixed_space_info(space_info))
6342 use_cluster = false;
6343
David Sterbab6919a52013-04-29 13:39:40 +00006344 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006345 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05006346 if (!btrfs_test_opt(root, SSD))
6347 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04006348 }
6349
David Sterbab6919a52013-04-29 13:39:40 +00006350 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
Josef Bacik67377732010-09-16 16:19:09 -04006351 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006352 last_ptr = &root->fs_info->data_alloc_cluster;
6353 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006354
Chris Mason239b14b2008-03-24 15:02:07 -04006355 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006356 spin_lock(&last_ptr->lock);
6357 if (last_ptr->block_group)
6358 hint_byte = last_ptr->window_start;
6359 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04006360 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006361
Chris Masona061fc82008-05-07 11:43:44 -04006362 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04006363 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04006364
Josef Bacik817d52f2009-07-13 21:29:25 -04006365 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006366 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006367
Josef Bacik2552d172009-04-03 10:14:19 -04006368 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04006369 block_group = btrfs_lookup_block_group(root->fs_info,
6370 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04006371 /*
6372 * we don't want to use the block group if it doesn't match our
6373 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05006374 *
6375 * However if we are re-searching with an ideal block group
6376 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006377 */
David Sterbab6919a52013-04-29 13:39:40 +00006378 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006379 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006380 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006381 if (list_empty(&block_group->list) ||
6382 block_group->ro) {
6383 /*
6384 * someone is removing this block group,
6385 * we can't jump into the have_block_group
6386 * target because our list pointers are not
6387 * valid
6388 */
6389 btrfs_put_block_group(block_group);
6390 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006391 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006392 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006393 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006394 }
Josef Bacik2552d172009-04-03 10:14:19 -04006395 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006396 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006397 }
Chris Mason42e70e72008-11-07 18:17:11 -05006398 }
Josef Bacik2552d172009-04-03 10:14:19 -04006399search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006400 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006401 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006402 list_for_each_entry(block_group, &space_info->block_groups[index],
6403 list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006404 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006405 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05006406
Josef Bacik11dfe352009-11-13 20:12:59 +00006407 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006408 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006409
Chris Mason83a50de2010-12-13 15:06:46 -05006410 /*
6411 * this can happen if we end up cycling through all the
6412 * raid types, but we want to make sure we only allocate
6413 * for the proper type.
6414 */
David Sterbab6919a52013-04-29 13:39:40 +00006415 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05006416 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6417 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006418 BTRFS_BLOCK_GROUP_RAID5 |
6419 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006420 BTRFS_BLOCK_GROUP_RAID10;
6421
6422 /*
6423 * if they asked for extra copies and this block group
6424 * doesn't provide them, bail. This does allow us to
6425 * fill raid0 from raid1.
6426 */
David Sterbab6919a52013-04-29 13:39:40 +00006427 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05006428 goto loop;
6429 }
6430
Josef Bacik2552d172009-04-03 10:14:19 -04006431have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006432 cached = block_group_cache_done(block_group);
6433 if (unlikely(!cached)) {
Liu Bof6373bf2012-12-27 09:01:18 +00006434 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006435 BUG_ON(ret < 0);
6436 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006437 }
6438
Josef Bacik36cce922013-08-05 11:15:21 -04006439 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
6440 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05006441 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006442 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006443
Josef Bacik0a243252009-09-11 16:11:20 -04006444 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006445 * Ok we want to try and use the cluster allocator, so
6446 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006447 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006448 if (last_ptr) {
Miao Xie215a63d2014-01-15 20:00:56 +08006449 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05006450 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006451 /*
6452 * the refill lock keeps out other
6453 * people trying to start a new cluster
6454 */
6455 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006456 used_block_group = last_ptr->block_group;
6457 if (used_block_group != block_group &&
6458 (!used_block_group ||
6459 used_block_group->ro ||
Miao Xie215a63d2014-01-15 20:00:56 +08006460 !block_group_bits(used_block_group, flags)))
Chris Mason44fb5512009-06-04 15:34:51 -04006461 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04006462
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006463 if (used_block_group != block_group)
6464 btrfs_get_block_group(used_block_group);
6465
6466 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08006467 last_ptr,
6468 num_bytes,
6469 used_block_group->key.objectid,
6470 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006471 if (offset) {
6472 /* we have a block, we're done */
6473 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006474 trace_btrfs_reserve_extent_cluster(root,
Miao Xie89d43462014-01-15 20:00:57 +08006475 used_block_group,
6476 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08006477 if (used_block_group != block_group) {
6478 btrfs_put_block_group(block_group);
6479 block_group = used_block_group;
6480 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006481 goto checks;
6482 }
6483
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006484 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xie215a63d2014-01-15 20:00:56 +08006485 if (used_block_group != block_group)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006486 btrfs_put_block_group(used_block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006487refill_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006488 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6489 * set up a new clusters, so lets just skip it
6490 * and let the allocator find whatever block
6491 * it can find. If we reach this point, we
6492 * will have tried the cluster allocator
6493 * plenty of times and not have found
6494 * anything, so we are likely way too
6495 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006496 * anything.
6497 *
6498 * However, if the cluster is taken from the
6499 * current block group, release the cluster
6500 * first, so that we stand a better chance of
6501 * succeeding in the unclustered
6502 * allocation. */
6503 if (loop >= LOOP_NO_EMPTY_SIZE &&
6504 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006505 spin_unlock(&last_ptr->refill_lock);
6506 goto unclustered_alloc;
6507 }
6508
Chris Masonfa9c0d792009-04-03 09:47:43 -04006509 /*
6510 * this cluster didn't work out, free it and
6511 * start over
6512 */
6513 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6514
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006515 if (loop >= LOOP_NO_EMPTY_SIZE) {
6516 spin_unlock(&last_ptr->refill_lock);
6517 goto unclustered_alloc;
6518 }
6519
Chris Mason8de972b2013-01-04 15:39:43 -05006520 aligned_cluster = max_t(unsigned long,
6521 empty_cluster + empty_size,
6522 block_group->full_stripe_len);
6523
Chris Masonfa9c0d792009-04-03 09:47:43 -04006524 /* allocate a cluster in this block group */
Josef Bacik00361582013-08-14 14:02:47 -04006525 ret = btrfs_find_space_cluster(root, block_group,
6526 last_ptr, search_start,
6527 num_bytes,
6528 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006529 if (ret == 0) {
6530 /*
6531 * now pull our allocation out of this
6532 * cluster
6533 */
6534 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08006535 last_ptr,
6536 num_bytes,
6537 search_start,
6538 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006539 if (offset) {
6540 /* we found one, proceed */
6541 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006542 trace_btrfs_reserve_extent_cluster(root,
6543 block_group, search_start,
6544 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006545 goto checks;
6546 }
Josef Bacik0a243252009-09-11 16:11:20 -04006547 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6548 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006549 spin_unlock(&last_ptr->refill_lock);
6550
Josef Bacik0a243252009-09-11 16:11:20 -04006551 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006552 wait_block_group_cache_progress(block_group,
6553 num_bytes + empty_cluster + empty_size);
6554 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006555 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006556
Chris Masonfa9c0d792009-04-03 09:47:43 -04006557 /*
6558 * at this point we either didn't find a cluster
6559 * or we weren't able to allocate a block from our
6560 * cluster. Free the cluster we've been trying
6561 * to use, and go to the next block group
6562 */
Josef Bacik0a243252009-09-11 16:11:20 -04006563 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006564 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006565 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006566 }
6567
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006568unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006569 spin_lock(&block_group->free_space_ctl->tree_lock);
6570 if (cached &&
6571 block_group->free_space_ctl->free_space <
6572 num_bytes + empty_cluster + empty_size) {
Miao Xiea4820392013-09-09 13:19:42 +08006573 if (block_group->free_space_ctl->free_space >
6574 max_extent_size)
6575 max_extent_size =
6576 block_group->free_space_ctl->free_space;
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006577 spin_unlock(&block_group->free_space_ctl->tree_lock);
6578 goto loop;
6579 }
6580 spin_unlock(&block_group->free_space_ctl->tree_lock);
6581
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006582 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08006583 num_bytes, empty_size,
6584 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006585 /*
6586 * If we didn't find a chunk, and we haven't failed on this
6587 * block group before, and this block group is in the middle of
6588 * caching and we are ok with waiting, then go ahead and wait
6589 * for progress to be made, and set failed_alloc to true.
6590 *
6591 * If failed_alloc is true then we've already waited on this
6592 * block group once and should move on to the next block group.
6593 */
6594 if (!offset && !failed_alloc && !cached &&
6595 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006596 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006597 num_bytes + empty_size);
6598 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006599 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006600 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006601 if (!cached)
6602 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006603 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006604 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006605checks:
Miao Xie215a63d2014-01-15 20:00:56 +08006606 search_start = stripe_align(root, block_group,
David Woodhouse53b381b2013-01-29 18:40:14 -05006607 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006608
Josef Bacik2552d172009-04-03 10:14:19 -04006609 /* move on to the next group */
6610 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08006611 block_group->key.objectid + block_group->key.offset) {
6612 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006613 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006614 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006615
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006616 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08006617 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006618 search_start - offset);
6619 BUG_ON(offset > search_start);
6620
Miao Xie215a63d2014-01-15 20:00:56 +08006621 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006622 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006623 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08006624 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006625 goto loop;
6626 }
Yan Zheng11833d62009-09-11 16:11:19 -04006627
Josef Bacik2552d172009-04-03 10:14:19 -04006628 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006629 ins->objectid = search_start;
6630 ins->offset = num_bytes;
6631
Josef Bacik3f7de032011-11-10 08:29:20 -05006632 trace_btrfs_reserve_extent(orig_root, block_group,
6633 search_start, num_bytes);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006634 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006635 break;
6636loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006637 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006638 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006639 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04006640 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006641 }
6642 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006643
Miao Xie60d2adb2011-09-09 17:34:35 +08006644 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6645 goto search;
6646
Yan, Zhengb742bb82010-05-16 10:46:24 -04006647 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6648 goto search;
6649
Josef Bacik285ff5a2012-01-13 15:27:45 -05006650 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006651 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6652 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006653 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6654 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6655 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6656 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006657 */
Josef Bacik723bda22011-05-27 16:11:38 -04006658 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006659 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006660 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006661 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04006662 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08006663 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04006664
Wang Shilongf017f152014-03-13 13:19:47 +08006665 trans = current->journal_info;
6666 if (trans)
6667 exist = 1;
6668 else
6669 trans = btrfs_join_transaction(root);
6670
Josef Bacik00361582013-08-14 14:02:47 -04006671 if (IS_ERR(trans)) {
6672 ret = PTR_ERR(trans);
6673 goto out;
6674 }
6675
David Sterbab6919a52013-04-29 13:39:40 +00006676 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04006677 CHUNK_ALLOC_FORCE);
6678 /*
6679 * Do not bail out on ENOSPC since we
6680 * can do more things.
6681 */
Josef Bacik00361582013-08-14 14:02:47 -04006682 if (ret < 0 && ret != -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04006683 btrfs_abort_transaction(trans,
6684 root, ret);
Josef Bacik00361582013-08-14 14:02:47 -04006685 else
6686 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08006687 if (!exist)
6688 btrfs_end_transaction(trans, root);
Josef Bacik00361582013-08-14 14:02:47 -04006689 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04006690 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006691 }
6692
6693 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006694 empty_size = 0;
6695 empty_cluster = 0;
6696 }
Chris Mason42e70e72008-11-07 18:17:11 -05006697
Josef Bacik723bda22011-05-27 16:11:38 -04006698 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006699 } else if (!ins->objectid) {
6700 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006701 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006702 ret = 0;
6703 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006704out:
Miao Xiea4820392013-09-09 13:19:42 +08006705 if (ret == -ENOSPC)
6706 ins->offset = max_extent_size;
Chris Mason0f70abe2007-02-28 16:46:22 -05006707 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006708}
Chris Masonec44a352008-04-28 15:29:52 -04006709
Josef Bacik9ed74f22009-09-11 16:12:44 -04006710static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6711 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006712{
6713 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006714 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006715
Josef Bacik9ed74f22009-09-11 16:12:44 -04006716 spin_lock(&info->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05006717 printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006718 info->flags,
6719 info->total_bytes - info->bytes_used - info->bytes_pinned -
6720 info->bytes_reserved - info->bytes_readonly,
Chris Masond3977122009-01-05 21:25:51 -05006721 (info->full) ? "" : "not ");
Frank Holtonefe120a2013-12-20 11:37:06 -05006722 printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006723 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006724 info->total_bytes, info->bytes_used, info->bytes_pinned,
6725 info->bytes_reserved, info->bytes_may_use,
6726 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006727 spin_unlock(&info->lock);
6728
6729 if (!dump_block_groups)
6730 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006731
Josef Bacik80eb2342008-10-29 14:49:05 -04006732 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006733again:
6734 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006735 spin_lock(&cache->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05006736 printk(KERN_INFO "BTRFS: "
6737 "block group %llu has %llu bytes, "
6738 "%llu used %llu pinned %llu reserved %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006739 cache->key.objectid, cache->key.offset,
6740 btrfs_block_group_used(&cache->item), cache->pinned,
6741 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006742 btrfs_dump_free_space(cache, bytes);
6743 spin_unlock(&cache->lock);
6744 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006745 if (++index < BTRFS_NR_RAID_TYPES)
6746 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006747 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006748}
Zheng Yane8569812008-09-26 10:05:48 -04006749
Josef Bacik00361582013-08-14 14:02:47 -04006750int btrfs_reserve_extent(struct btrfs_root *root,
Yan Zheng11833d62009-09-11 16:11:19 -04006751 u64 num_bytes, u64 min_alloc_size,
6752 u64 empty_size, u64 hint_byte,
David Sterbab6919a52013-04-29 13:39:40 +00006753 struct btrfs_key *ins, int is_data)
Chris Masonfec577f2007-02-26 10:40:21 -05006754{
Miao Xie9e622d62012-01-26 15:01:12 -05006755 bool final_tried = false;
David Sterbab6919a52013-04-29 13:39:40 +00006756 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05006757 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006758
David Sterbab6919a52013-04-29 13:39:40 +00006759 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04006760again:
Chris Masondb945352007-10-15 16:15:53 -04006761 WARN_ON(num_bytes < root->sectorsize);
Josef Bacik00361582013-08-14 14:02:47 -04006762 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
6763 flags);
Chris Mason3b951512008-04-17 11:29:12 -04006764
Miao Xie9e622d62012-01-26 15:01:12 -05006765 if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08006766 if (!final_tried && ins->offset) {
6767 num_bytes = min(num_bytes >> 1, ins->offset);
Zach Brown24542bf2012-11-16 00:04:43 +00006768 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006769 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006770 if (num_bytes == min_alloc_size)
6771 final_tried = true;
6772 goto again;
6773 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6774 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006775
David Sterbab6919a52013-04-29 13:39:40 +00006776 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006777 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006778 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006779 if (sinfo)
6780 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006781 }
Chris Mason925baed2008-06-25 16:01:30 -04006782 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006783
6784 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006785}
6786
Chris Masone688b7252011-10-31 20:52:39 -04006787static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6788 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006789{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006790 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006791 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006792
Josef Bacik0f9dd462008-09-23 13:14:11 -04006793 cache = btrfs_lookup_block_group(root->fs_info, start);
6794 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006795 btrfs_err(root->fs_info, "Unable to find block group for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006796 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006797 return -ENOSPC;
6798 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006799
Li Dongyang5378e602011-03-24 10:24:27 +00006800 if (btrfs_test_opt(root, DISCARD))
6801 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006802
Chris Masone688b7252011-10-31 20:52:39 -04006803 if (pin)
6804 pin_down_extent(root, cache, start, len, 1);
6805 else {
6806 btrfs_add_free_space(cache, start, len);
6807 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6808 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006809 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006810
liubo1abe9b82011-03-24 11:18:59 +00006811 trace_btrfs_reserved_extent_free(root, start, len);
6812
Chris Masone6dcd2d2008-07-17 12:53:50 -04006813 return ret;
6814}
6815
Chris Masone688b7252011-10-31 20:52:39 -04006816int btrfs_free_reserved_extent(struct btrfs_root *root,
6817 u64 start, u64 len)
6818{
6819 return __btrfs_free_reserved_extent(root, start, len, 0);
6820}
6821
6822int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6823 u64 start, u64 len)
6824{
6825 return __btrfs_free_reserved_extent(root, start, len, 1);
6826}
6827
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006828static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6829 struct btrfs_root *root,
6830 u64 parent, u64 root_objectid,
6831 u64 flags, u64 owner, u64 offset,
6832 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006833{
6834 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006835 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006836 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006837 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006838 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006839 struct extent_buffer *leaf;
6840 int type;
6841 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006842
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006843 if (parent > 0)
6844 type = BTRFS_SHARED_DATA_REF_KEY;
6845 else
6846 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006847
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006848 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006849
6850 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006851 if (!path)
6852 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006853
Chris Masonb9473432009-03-13 11:00:37 -04006854 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006855 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6856 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006857 if (ret) {
6858 btrfs_free_path(path);
6859 return ret;
6860 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006861
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006862 leaf = path->nodes[0];
6863 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006864 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006865 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6866 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6867 btrfs_set_extent_flags(leaf, extent_item,
6868 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006869
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006870 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6871 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6872 if (parent > 0) {
6873 struct btrfs_shared_data_ref *ref;
6874 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6875 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6876 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6877 } else {
6878 struct btrfs_extent_data_ref *ref;
6879 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6880 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6881 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6882 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6883 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6884 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006885
6886 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006887 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006888
Josef Bacikfcebe452014-05-13 17:30:47 -07006889 /* Always set parent to 0 here since its exclusive anyway. */
6890 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
6891 ins->objectid, ins->offset,
6892 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
6893 if (ret)
6894 return ret;
6895
Liu Boc53d6132012-12-27 09:01:19 +00006896 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006897 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006898 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006899 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006900 BUG();
6901 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04006902 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006903 return ret;
6904}
6905
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006906static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6907 struct btrfs_root *root,
6908 u64 parent, u64 root_objectid,
6909 u64 flags, struct btrfs_disk_key *key,
Josef Bacikfcebe452014-05-13 17:30:47 -07006910 int level, struct btrfs_key *ins,
6911 int no_quota)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006912{
6913 int ret;
6914 struct btrfs_fs_info *fs_info = root->fs_info;
6915 struct btrfs_extent_item *extent_item;
6916 struct btrfs_tree_block_info *block_info;
6917 struct btrfs_extent_inline_ref *iref;
6918 struct btrfs_path *path;
6919 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006920 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07006921 u64 num_bytes = ins->offset;
Josef Bacik3173a182013-03-07 14:22:04 -05006922 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6923 SKINNY_METADATA);
6924
6925 if (!skinny_metadata)
6926 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006927
6928 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04006929 if (!path) {
6930 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
6931 root->leafsize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07006932 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04006933 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006934
6935 path->leave_spinning = 1;
6936 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6937 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006938 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04006939 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
6940 root->leafsize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006941 btrfs_free_path(path);
6942 return ret;
6943 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006944
6945 leaf = path->nodes[0];
6946 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6947 struct btrfs_extent_item);
6948 btrfs_set_extent_refs(leaf, extent_item, 1);
6949 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6950 btrfs_set_extent_flags(leaf, extent_item,
6951 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006952
Josef Bacik3173a182013-03-07 14:22:04 -05006953 if (skinny_metadata) {
6954 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Josef Bacikfcebe452014-05-13 17:30:47 -07006955 num_bytes = root->leafsize;
Josef Bacik3173a182013-03-07 14:22:04 -05006956 } else {
6957 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6958 btrfs_set_tree_block_key(leaf, block_info, key);
6959 btrfs_set_tree_block_level(leaf, block_info, level);
6960 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6961 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006962
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006963 if (parent > 0) {
6964 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6965 btrfs_set_extent_inline_ref_type(leaf, iref,
6966 BTRFS_SHARED_BLOCK_REF_KEY);
6967 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6968 } else {
6969 btrfs_set_extent_inline_ref_type(leaf, iref,
6970 BTRFS_TREE_BLOCK_REF_KEY);
6971 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6972 }
6973
6974 btrfs_mark_buffer_dirty(leaf);
6975 btrfs_free_path(path);
6976
Josef Bacikfcebe452014-05-13 17:30:47 -07006977 if (!no_quota) {
6978 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
6979 ins->objectid, num_bytes,
6980 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
6981 if (ret)
6982 return ret;
6983 }
6984
Josef Bacik3173a182013-03-07 14:22:04 -05006985 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006986 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006987 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006988 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006989 BUG();
6990 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04006991
6992 trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->leafsize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006993 return ret;
6994}
6995
6996int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6997 struct btrfs_root *root,
6998 u64 root_objectid, u64 owner,
6999 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007000{
7001 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04007002
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007003 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04007004
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007005 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
7006 ins->offset, 0,
7007 root_objectid, owner, offset,
7008 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007009 return ret;
7010}
Chris Masone02119d2008-09-05 16:13:11 -04007011
7012/*
7013 * this is used by the tree logging recovery code. It records that
7014 * an extent has been allocated and makes sure to clear the free
7015 * space cache bits as well
7016 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007017int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
7018 struct btrfs_root *root,
7019 u64 root_objectid, u64 owner, u64 offset,
7020 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04007021{
7022 int ret;
7023 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007024
7025 /*
7026 * Mixed block groups will exclude before processing the log so we only
7027 * need to do the exlude dance if this fs isn't mixed.
7028 */
7029 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
7030 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
7031 if (ret)
7032 return ret;
7033 }
Chris Masone02119d2008-09-05 16:13:11 -04007034
Chris Masone02119d2008-09-05 16:13:11 -04007035 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007036 if (!block_group)
7037 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04007038
Josef Bacikfb25e912011-07-26 17:00:46 -04007039 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
7040 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007041 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007042 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
7043 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04007044 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04007045 return ret;
7046}
7047
Eric Sandeen48a3b632013-04-25 20:41:01 +00007048static struct extent_buffer *
7049btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
7050 u64 bytenr, u32 blocksize, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04007051{
7052 struct extent_buffer *buf;
7053
7054 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
7055 if (!buf)
7056 return ERR_PTR(-ENOMEM);
7057 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04007058 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04007059 btrfs_tree_lock(buf);
7060 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05007061 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007062
7063 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04007064 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007065
Chris Masond0c803c2008-09-11 16:17:57 -04007066 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00007067 /*
7068 * we allow two log transactions at a time, use different
7069 * EXENT bit to differentiate dirty pages.
7070 */
7071 if (root->log_transid % 2 == 0)
7072 set_extent_dirty(&root->dirty_log_pages, buf->start,
7073 buf->start + buf->len - 1, GFP_NOFS);
7074 else
7075 set_extent_new(&root->dirty_log_pages, buf->start,
7076 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04007077 } else {
7078 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
7079 buf->start + buf->len - 1, GFP_NOFS);
7080 }
Chris Mason65b51a02008-08-01 15:11:20 -04007081 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05007082 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04007083 return buf;
7084}
7085
Yan, Zhengf0486c62010-05-16 10:46:25 -04007086static struct btrfs_block_rsv *
7087use_block_rsv(struct btrfs_trans_handle *trans,
7088 struct btrfs_root *root, u32 blocksize)
7089{
7090 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00007091 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007092 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00007093 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007094
7095 block_rsv = get_block_rsv(trans, root);
7096
Miao Xieb586b322013-05-13 13:55:10 +00007097 if (unlikely(block_rsv->size == 0))
7098 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00007099again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007100 ret = block_rsv_use_bytes(block_rsv, blocksize);
7101 if (!ret)
7102 return block_rsv;
7103
Miao Xieb586b322013-05-13 13:55:10 +00007104 if (block_rsv->failfast)
7105 return ERR_PTR(ret);
7106
Miao Xied88033d2013-05-13 13:55:12 +00007107 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
7108 global_updated = true;
7109 update_global_block_rsv(root->fs_info);
7110 goto again;
7111 }
7112
Miao Xieb586b322013-05-13 13:55:10 +00007113 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7114 static DEFINE_RATELIMIT_STATE(_rs,
7115 DEFAULT_RATELIMIT_INTERVAL * 10,
7116 /*DEFAULT_RATELIMIT_BURST*/ 1);
7117 if (__ratelimit(&_rs))
7118 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05007119 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00007120 }
7121try_reserve:
7122 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
7123 BTRFS_RESERVE_NO_FLUSH);
7124 if (!ret)
7125 return block_rsv;
7126 /*
7127 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00007128 * the global reserve if its space type is the same as the global
7129 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00007130 */
Miao Xie5881cfc2013-05-13 13:55:11 +00007131 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
7132 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00007133 ret = block_rsv_use_bytes(global_rsv, blocksize);
7134 if (!ret)
7135 return global_rsv;
7136 }
7137 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007138}
7139
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007140static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
7141 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007142{
7143 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007144 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007145}
7146
Chris Masonfec577f2007-02-26 10:40:21 -05007147/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007148 * finds a free extent and does all the dirty work required for allocation
7149 * returns the key for the extent through ins, and a tree buffer for
7150 * the first block of the extent through buf.
7151 *
Chris Masonfec577f2007-02-26 10:40:21 -05007152 * returns the tree buffer or NULL.
7153 */
Chris Mason5f39d392007-10-15 16:14:19 -04007154struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007155 struct btrfs_root *root, u32 blocksize,
7156 u64 parent, u64 root_objectid,
7157 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02007158 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05007159{
Chris Masone2fa7222007-03-12 16:22:34 -04007160 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007161 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04007162 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007163 u64 flags = 0;
7164 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05007165 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7166 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007167
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007168#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7169 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state))) {
7170 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
7171 blocksize, level);
7172 if (!IS_ERR(buf))
7173 root->alloc_bytenr += blocksize;
7174 return buf;
7175 }
7176#endif
Yan, Zhengf0486c62010-05-16 10:46:25 -04007177 block_rsv = use_block_rsv(trans, root, blocksize);
7178 if (IS_ERR(block_rsv))
7179 return ERR_CAST(block_rsv);
7180
Josef Bacik00361582013-08-14 14:02:47 -04007181 ret = btrfs_reserve_extent(root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007182 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05007183 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007184 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007185 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05007186 }
Chris Mason55c69072008-01-09 15:55:33 -05007187
Chris Mason4008c042009-02-12 14:09:45 -05007188 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
7189 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007190 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007191
7192 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
7193 if (parent == 0)
7194 parent = ins.objectid;
7195 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7196 } else
7197 BUG_ON(parent > 0);
7198
7199 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
7200 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00007201 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007202 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007203 if (key)
7204 memcpy(&extent_op->key, key, sizeof(extent_op->key));
7205 else
7206 memset(&extent_op->key, 0, sizeof(extent_op->key));
7207 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05007208 if (skinny_metadata)
7209 extent_op->update_key = 0;
7210 else
7211 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007212 extent_op->update_flags = 1;
7213 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04007214 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007215
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007216 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
7217 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007218 ins.offset, parent, root_objectid,
7219 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02007220 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007221 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007222 }
Chris Masonfec577f2007-02-26 10:40:21 -05007223 return buf;
7224}
Chris Masona28ec192007-03-06 20:08:01 -05007225
Yan Zheng2c47e6052009-06-27 21:07:35 -04007226struct walk_control {
7227 u64 refs[BTRFS_MAX_LEVEL];
7228 u64 flags[BTRFS_MAX_LEVEL];
7229 struct btrfs_key update_progress;
7230 int stage;
7231 int level;
7232 int shared_level;
7233 int update_ref;
7234 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007235 int reada_slot;
7236 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007237 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007238};
7239
7240#define DROP_REFERENCE 1
7241#define UPDATE_BACKREF 2
7242
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007243static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
7244 struct btrfs_root *root,
7245 struct walk_control *wc,
7246 struct btrfs_path *path)
7247{
7248 u64 bytenr;
7249 u64 generation;
7250 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007251 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007252 u32 nritems;
7253 u32 blocksize;
7254 struct btrfs_key key;
7255 struct extent_buffer *eb;
7256 int ret;
7257 int slot;
7258 int nread = 0;
7259
7260 if (path->slots[wc->level] < wc->reada_slot) {
7261 wc->reada_count = wc->reada_count * 2 / 3;
7262 wc->reada_count = max(wc->reada_count, 2);
7263 } else {
7264 wc->reada_count = wc->reada_count * 3 / 2;
7265 wc->reada_count = min_t(int, wc->reada_count,
7266 BTRFS_NODEPTRS_PER_BLOCK(root));
7267 }
7268
7269 eb = path->nodes[wc->level];
7270 nritems = btrfs_header_nritems(eb);
7271 blocksize = btrfs_level_size(root, wc->level - 1);
7272
7273 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7274 if (nread >= wc->reada_count)
7275 break;
7276
7277 cond_resched();
7278 bytenr = btrfs_node_blockptr(eb, slot);
7279 generation = btrfs_node_ptr_generation(eb, slot);
7280
7281 if (slot == path->slots[wc->level])
7282 goto reada;
7283
7284 if (wc->stage == UPDATE_BACKREF &&
7285 generation <= root->root_key.offset)
7286 continue;
7287
Yan, Zheng94fcca92009-10-09 09:25:16 -04007288 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05007289 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7290 wc->level - 1, 1, &refs,
7291 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007292 /* We don't care about errors in readahead. */
7293 if (ret < 0)
7294 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007295 BUG_ON(refs == 0);
7296
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007297 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007298 if (refs == 1)
7299 goto reada;
7300
Yan, Zheng94fcca92009-10-09 09:25:16 -04007301 if (wc->level == 1 &&
7302 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7303 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007304 if (!wc->update_ref ||
7305 generation <= root->root_key.offset)
7306 continue;
7307 btrfs_node_key_to_cpu(eb, &key, slot);
7308 ret = btrfs_comp_cpu_keys(&key,
7309 &wc->update_progress);
7310 if (ret < 0)
7311 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007312 } else {
7313 if (wc->level == 1 &&
7314 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7315 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007316 }
7317reada:
7318 ret = readahead_tree_block(root, bytenr, blocksize,
7319 generation);
7320 if (ret)
7321 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007322 nread++;
7323 }
7324 wc->reada_slot = slot;
7325}
7326
Chris Mason9aca1d52007-03-13 11:09:37 -04007327/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007328 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007329 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04007330 * when wc->stage == UPDATE_BACKREF, this function updates
7331 * back refs for pointers in the block.
7332 *
7333 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04007334 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007335static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7336 struct btrfs_root *root,
7337 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007338 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04007339{
7340 int level = wc->level;
7341 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04007342 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7343 int ret;
7344
7345 if (wc->stage == UPDATE_BACKREF &&
7346 btrfs_header_owner(eb) != root->root_key.objectid)
7347 return 1;
7348
7349 /*
7350 * when reference count of tree block is 1, it won't increase
7351 * again. once full backref flag is set, we never clear it.
7352 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04007353 if (lookup_info &&
7354 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7355 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007356 BUG_ON(!path->locks[level]);
7357 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007358 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007359 &wc->refs[level],
7360 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007361 BUG_ON(ret == -ENOMEM);
7362 if (ret)
7363 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007364 BUG_ON(wc->refs[level] == 0);
7365 }
7366
Yan Zheng2c47e6052009-06-27 21:07:35 -04007367 if (wc->stage == DROP_REFERENCE) {
7368 if (wc->refs[level] > 1)
7369 return 1;
7370
7371 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04007372 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007373 path->locks[level] = 0;
7374 }
7375 return 0;
7376 }
7377
7378 /* wc->stage == UPDATE_BACKREF */
7379 if (!(wc->flags[level] & flag)) {
7380 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007381 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007382 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007383 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007384 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007385 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04007386 eb->len, flag,
7387 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007388 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007389 wc->flags[level] |= flag;
7390 }
7391
7392 /*
7393 * the block is shared by multiple trees, so it's not good to
7394 * keep the tree lock
7395 */
7396 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04007397 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007398 path->locks[level] = 0;
7399 }
7400 return 0;
7401}
7402
7403/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007404 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007405 *
7406 * when wc->stage == DROP_REFERENCE, this function checks
7407 * reference count of the block pointed to. if the block
7408 * is shared and we need update back refs for the subtree
7409 * rooted at the block, this function changes wc->stage to
7410 * UPDATE_BACKREF. if the block is shared and there is no
7411 * need to update back, this function drops the reference
7412 * to the block.
7413 *
7414 * NOTE: return value 1 means we should stop walking down.
7415 */
7416static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7417 struct btrfs_root *root,
7418 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007419 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007420{
7421 u64 bytenr;
7422 u64 generation;
7423 u64 parent;
7424 u32 blocksize;
7425 struct btrfs_key key;
7426 struct extent_buffer *next;
7427 int level = wc->level;
7428 int reada = 0;
7429 int ret = 0;
7430
7431 generation = btrfs_node_ptr_generation(path->nodes[level],
7432 path->slots[level]);
7433 /*
7434 * if the lower level block was created before the snapshot
7435 * was created, we know there is no need to update back refs
7436 * for the subtree
7437 */
7438 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007439 generation <= root->root_key.offset) {
7440 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007441 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007442 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007443
7444 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7445 blocksize = btrfs_level_size(root, level - 1);
7446
7447 next = btrfs_find_tree_block(root, bytenr, blocksize);
7448 if (!next) {
7449 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00007450 if (!next)
7451 return -ENOMEM;
Josef Bacikb2aaaa32013-07-05 17:05:38 -04007452 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
7453 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007454 reada = 1;
7455 }
7456 btrfs_tree_lock(next);
7457 btrfs_set_lock_blocking(next);
7458
Josef Bacik3173a182013-03-07 14:22:04 -05007459 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007460 &wc->refs[level - 1],
7461 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007462 if (ret < 0) {
7463 btrfs_tree_unlock(next);
7464 return ret;
7465 }
7466
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007467 if (unlikely(wc->refs[level - 1] == 0)) {
7468 btrfs_err(root->fs_info, "Missing references.");
7469 BUG();
7470 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007471 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007472
Yan, Zheng94fcca92009-10-09 09:25:16 -04007473 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007474 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007475 if (level == 1 &&
7476 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7477 goto skip;
7478
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007479 if (!wc->update_ref ||
7480 generation <= root->root_key.offset)
7481 goto skip;
7482
7483 btrfs_node_key_to_cpu(path->nodes[level], &key,
7484 path->slots[level]);
7485 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7486 if (ret < 0)
7487 goto skip;
7488
7489 wc->stage = UPDATE_BACKREF;
7490 wc->shared_level = level - 1;
7491 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007492 } else {
7493 if (level == 1 &&
7494 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7495 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007496 }
7497
Chris Masonb9fab912012-05-06 07:23:47 -04007498 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007499 btrfs_tree_unlock(next);
7500 free_extent_buffer(next);
7501 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007502 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007503 }
7504
7505 if (!next) {
7506 if (reada && level == 1)
7507 reada_walk_down(trans, root, wc, path);
7508 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007509 if (!next || !extent_buffer_uptodate(next)) {
7510 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007511 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007512 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007513 btrfs_tree_lock(next);
7514 btrfs_set_lock_blocking(next);
7515 }
7516
7517 level--;
7518 BUG_ON(level != btrfs_header_level(next));
7519 path->nodes[level] = next;
7520 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007521 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007522 wc->level = level;
7523 if (wc->level == 1)
7524 wc->reada_slot = 0;
7525 return 0;
7526skip:
7527 wc->refs[level - 1] = 0;
7528 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007529 if (wc->stage == DROP_REFERENCE) {
7530 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7531 parent = path->nodes[level]->start;
7532 } else {
7533 BUG_ON(root->root_key.objectid !=
7534 btrfs_header_owner(path->nodes[level]));
7535 parent = 0;
7536 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007537
Yan, Zheng94fcca92009-10-09 09:25:16 -04007538 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007539 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007540 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007541 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007542 btrfs_tree_unlock(next);
7543 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007544 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007545 return 1;
7546}
7547
7548/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007549 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007550 *
7551 * when wc->stage == DROP_REFERENCE, this function drops
7552 * reference count on the block.
7553 *
7554 * when wc->stage == UPDATE_BACKREF, this function changes
7555 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7556 * to UPDATE_BACKREF previously while processing the block.
7557 *
7558 * NOTE: return value 1 means we should stop walking up.
7559 */
7560static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7561 struct btrfs_root *root,
7562 struct btrfs_path *path,
7563 struct walk_control *wc)
7564{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007565 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007566 int level = wc->level;
7567 struct extent_buffer *eb = path->nodes[level];
7568 u64 parent = 0;
7569
7570 if (wc->stage == UPDATE_BACKREF) {
7571 BUG_ON(wc->shared_level < level);
7572 if (level < wc->shared_level)
7573 goto out;
7574
Yan Zheng2c47e6052009-06-27 21:07:35 -04007575 ret = find_next_key(path, level + 1, &wc->update_progress);
7576 if (ret > 0)
7577 wc->update_ref = 0;
7578
7579 wc->stage = DROP_REFERENCE;
7580 wc->shared_level = -1;
7581 path->slots[level] = 0;
7582
7583 /*
7584 * check reference count again if the block isn't locked.
7585 * we should start walking down the tree again if reference
7586 * count is one.
7587 */
7588 if (!path->locks[level]) {
7589 BUG_ON(level == 0);
7590 btrfs_tree_lock(eb);
7591 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007592 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007593
7594 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007595 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007596 &wc->refs[level],
7597 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007598 if (ret < 0) {
7599 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007600 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007601 return ret;
7602 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007603 BUG_ON(wc->refs[level] == 0);
7604 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007605 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007606 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007607 return 1;
7608 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007609 }
7610 }
7611
7612 /* wc->stage == DROP_REFERENCE */
7613 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7614
7615 if (wc->refs[level] == 1) {
7616 if (level == 0) {
7617 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007618 ret = btrfs_dec_ref(trans, root, eb, 1,
7619 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007620 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007621 ret = btrfs_dec_ref(trans, root, eb, 0,
7622 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007623 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007624 }
7625 /* make block locked assertion in clean_tree_block happy */
7626 if (!path->locks[level] &&
7627 btrfs_header_generation(eb) == trans->transid) {
7628 btrfs_tree_lock(eb);
7629 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007630 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007631 }
7632 clean_tree_block(trans, root, eb);
7633 }
7634
7635 if (eb == root->node) {
7636 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7637 parent = eb->start;
7638 else
7639 BUG_ON(root->root_key.objectid !=
7640 btrfs_header_owner(eb));
7641 } else {
7642 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7643 parent = path->nodes[level + 1]->start;
7644 else
7645 BUG_ON(root->root_key.objectid !=
7646 btrfs_header_owner(path->nodes[level + 1]));
7647 }
7648
Jan Schmidt5581a512012-05-16 17:04:52 +02007649 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007650out:
7651 wc->refs[level] = 0;
7652 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007653 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007654}
7655
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007656static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7657 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007658 struct btrfs_path *path,
7659 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007660{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007661 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007662 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007663 int ret;
7664
Yan Zheng2c47e6052009-06-27 21:07:35 -04007665 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007666 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007667 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007668 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007669
Yan Zheng2c47e6052009-06-27 21:07:35 -04007670 if (level == 0)
7671 break;
7672
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007673 if (path->slots[level] >=
7674 btrfs_header_nritems(path->nodes[level]))
7675 break;
7676
Yan, Zheng94fcca92009-10-09 09:25:16 -04007677 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007678 if (ret > 0) {
7679 path->slots[level]++;
7680 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00007681 } else if (ret < 0)
7682 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007683 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007684 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007685 return 0;
7686}
7687
Chris Masond3977122009-01-05 21:25:51 -05007688static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007689 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007690 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007691 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007692{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007693 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007694 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007695
Yan Zheng2c47e6052009-06-27 21:07:35 -04007696 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7697 while (level < max_level && path->nodes[level]) {
7698 wc->level = level;
7699 if (path->slots[level] + 1 <
7700 btrfs_header_nritems(path->nodes[level])) {
7701 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007702 return 0;
7703 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007704 ret = walk_up_proc(trans, root, path, wc);
7705 if (ret > 0)
7706 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007707
Yan Zheng2c47e6052009-06-27 21:07:35 -04007708 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007709 btrfs_tree_unlock_rw(path->nodes[level],
7710 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007711 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007712 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007713 free_extent_buffer(path->nodes[level]);
7714 path->nodes[level] = NULL;
7715 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007716 }
7717 }
7718 return 1;
7719}
7720
Chris Mason9aca1d52007-03-13 11:09:37 -04007721/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007722 * drop a subvolume tree.
7723 *
7724 * this function traverses the tree freeing any blocks that only
7725 * referenced by the tree.
7726 *
7727 * when a shared tree block is found. this function decreases its
7728 * reference count by one. if update_ref is true, this function
7729 * also make sure backrefs for the shared block and all lower level
7730 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007731 *
7732 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007733 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007734int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007735 struct btrfs_block_rsv *block_rsv, int update_ref,
7736 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007737{
Chris Mason5caf2a02007-04-02 11:20:42 -04007738 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007739 struct btrfs_trans_handle *trans;
7740 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007741 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007742 struct walk_control *wc;
7743 struct btrfs_key key;
7744 int err = 0;
7745 int ret;
7746 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04007747 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05007748
Chris Mason5caf2a02007-04-02 11:20:42 -04007749 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007750 if (!path) {
7751 err = -ENOMEM;
7752 goto out;
7753 }
Chris Mason20524f02007-03-10 06:35:47 -05007754
Yan Zheng2c47e6052009-06-27 21:07:35 -04007755 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007756 if (!wc) {
7757 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007758 err = -ENOMEM;
7759 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007760 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007761
Yan, Zhenga22285a2010-05-16 10:48:46 -04007762 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007763 if (IS_ERR(trans)) {
7764 err = PTR_ERR(trans);
7765 goto out_free;
7766 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007767
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007768 if (block_rsv)
7769 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007770
Chris Mason9f3a7422007-08-07 15:52:19 -04007771 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007772 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007773 path->nodes[level] = btrfs_lock_root_node(root);
7774 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007775 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007776 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007777 memset(&wc->update_progress, 0,
7778 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007779 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007780 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007781 memcpy(&wc->update_progress, &key,
7782 sizeof(wc->update_progress));
7783
Chris Mason6702ed42007-08-07 16:15:09 -04007784 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007785 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007786 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007787 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7788 path->lowest_level = 0;
7789 if (ret < 0) {
7790 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007791 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007792 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007793 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007794
Chris Mason7d9eb122008-07-08 14:19:17 -04007795 /*
7796 * unlock our path, this is safe because only this
7797 * function is allowed to delete this snapshot
7798 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007799 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007800
Yan Zheng2c47e6052009-06-27 21:07:35 -04007801 level = btrfs_header_level(root->node);
7802 while (1) {
7803 btrfs_tree_lock(path->nodes[level]);
7804 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007805 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007806
7807 ret = btrfs_lookup_extent_info(trans, root,
7808 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007809 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007810 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007811 if (ret < 0) {
7812 err = ret;
7813 goto out_end_trans;
7814 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007815 BUG_ON(wc->refs[level] == 0);
7816
7817 if (level == root_item->drop_level)
7818 break;
7819
7820 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007821 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007822 WARN_ON(wc->refs[level] != 1);
7823 level--;
7824 }
7825 }
7826
7827 wc->level = level;
7828 wc->shared_level = -1;
7829 wc->stage = DROP_REFERENCE;
7830 wc->update_ref = update_ref;
7831 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007832 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007833 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007834
7835 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007836
Yan Zheng2c47e6052009-06-27 21:07:35 -04007837 ret = walk_down_tree(trans, root, path, wc);
7838 if (ret < 0) {
7839 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007840 break;
7841 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007842
7843 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7844 if (ret < 0) {
7845 err = ret;
7846 break;
7847 }
7848
7849 if (ret > 0) {
7850 BUG_ON(wc->stage != DROP_REFERENCE);
7851 break;
7852 }
7853
7854 if (wc->stage == DROP_REFERENCE) {
7855 level = wc->level;
7856 btrfs_node_key(path->nodes[level],
7857 &root_item->drop_progress,
7858 path->slots[level]);
7859 root_item->drop_level = level;
7860 }
7861
7862 BUG_ON(wc->level == 0);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007863 if (btrfs_should_end_transaction(trans, tree_root) ||
7864 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007865 ret = btrfs_update_root(trans, tree_root,
7866 &root->root_key,
7867 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007868 if (ret) {
7869 btrfs_abort_transaction(trans, tree_root, ret);
7870 err = ret;
7871 goto out_end_trans;
7872 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007873
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007874 btrfs_end_transaction_throttle(trans, tree_root);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007875 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007876 pr_debug("BTRFS: drop snapshot early exit\n");
Josef Bacik3c8f2422013-07-15 11:57:06 -04007877 err = -EAGAIN;
7878 goto out_free;
7879 }
7880
Yan, Zhenga22285a2010-05-16 10:48:46 -04007881 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007882 if (IS_ERR(trans)) {
7883 err = PTR_ERR(trans);
7884 goto out_free;
7885 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007886 if (block_rsv)
7887 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007888 }
Chris Mason20524f02007-03-10 06:35:47 -05007889 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007890 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007891 if (err)
7892 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007893
7894 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007895 if (ret) {
7896 btrfs_abort_transaction(trans, tree_root, ret);
7897 goto out_end_trans;
7898 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007899
Yan, Zheng76dda932009-09-21 16:00:26 -04007900 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007901 ret = btrfs_find_root(tree_root, &root->root_key, path,
7902 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007903 if (ret < 0) {
7904 btrfs_abort_transaction(trans, tree_root, ret);
7905 err = ret;
7906 goto out_end_trans;
7907 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007908 /* if we fail to delete the orphan item this time
7909 * around, it'll get picked up the next time.
7910 *
7911 * The most common failure here is just -ENOENT.
7912 */
7913 btrfs_del_orphan_item(trans, tree_root,
7914 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007915 }
7916 }
7917
Miao Xie27cdeb72014-04-02 19:51:05 +08007918 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007919 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007920 } else {
7921 free_extent_buffer(root->node);
7922 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00007923 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007924 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04007925 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007926out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007927 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007928out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007929 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007930 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007931out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04007932 /*
7933 * So if we need to stop dropping the snapshot for whatever reason we
7934 * need to make sure to add it back to the dead root list so that we
7935 * keep trying to do the work later. This also cleans up roots if we
7936 * don't have it in the radix (like when we recover after a power fail
7937 * or unmount) so we don't leak memory.
7938 */
Josef Bacikb37b39c2013-07-23 16:57:15 -04007939 if (!for_reloc && root_dropped == false)
Josef Bacikd29a9f62013-07-17 19:30:20 -04007940 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08007941 if (err && err != -EAGAIN)
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007942 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007943 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007944}
Chris Mason9078a3e2007-04-26 16:46:15 -04007945
Yan Zheng2c47e6052009-06-27 21:07:35 -04007946/*
7947 * drop subtree rooted at tree block 'node'.
7948 *
7949 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007950 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007951 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007952int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7953 struct btrfs_root *root,
7954 struct extent_buffer *node,
7955 struct extent_buffer *parent)
7956{
7957 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007958 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007959 int level;
7960 int parent_level;
7961 int ret = 0;
7962 int wret;
7963
Yan Zheng2c47e6052009-06-27 21:07:35 -04007964 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7965
Yan Zhengf82d02d2008-10-29 14:49:05 -04007966 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007967 if (!path)
7968 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007969
Yan Zheng2c47e6052009-06-27 21:07:35 -04007970 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007971 if (!wc) {
7972 btrfs_free_path(path);
7973 return -ENOMEM;
7974 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007975
Chris Masonb9447ef2009-03-09 11:45:38 -04007976 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007977 parent_level = btrfs_header_level(parent);
7978 extent_buffer_get(parent);
7979 path->nodes[parent_level] = parent;
7980 path->slots[parent_level] = btrfs_header_nritems(parent);
7981
Chris Masonb9447ef2009-03-09 11:45:38 -04007982 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007983 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007984 path->nodes[level] = node;
7985 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007986 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007987
7988 wc->refs[parent_level] = 1;
7989 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7990 wc->level = level;
7991 wc->shared_level = -1;
7992 wc->stage = DROP_REFERENCE;
7993 wc->update_ref = 0;
7994 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007995 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007996 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007997
7998 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007999 wret = walk_down_tree(trans, root, path, wc);
8000 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04008001 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008002 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008003 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04008004
Yan Zheng2c47e6052009-06-27 21:07:35 -04008005 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008006 if (wret < 0)
8007 ret = wret;
8008 if (wret != 0)
8009 break;
8010 }
8011
Yan Zheng2c47e6052009-06-27 21:07:35 -04008012 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008013 btrfs_free_path(path);
8014 return ret;
8015}
8016
Chris Masonec44a352008-04-28 15:29:52 -04008017static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8018{
8019 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008020 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04008021
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008022 /*
8023 * if restripe for this chunk_type is on pick target profile and
8024 * return, otherwise do the usual balance
8025 */
8026 stripped = get_restripe_target(root->fs_info, flags);
8027 if (stripped)
8028 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02008029
Chris Masoncd02dca2010-12-13 14:56:23 -05008030 /*
8031 * we add in the count of missing devices because we want
8032 * to make sure that any RAID levels on a degraded FS
8033 * continue to be honored.
8034 */
8035 num_devices = root->fs_info->fs_devices->rw_devices +
8036 root->fs_info->fs_devices->missing_devices;
8037
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008038 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008039 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008040 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8041
Chris Masonec44a352008-04-28 15:29:52 -04008042 if (num_devices == 1) {
8043 stripped |= BTRFS_BLOCK_GROUP_DUP;
8044 stripped = flags & ~stripped;
8045
8046 /* turn raid0 into single device chunks */
8047 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8048 return stripped;
8049
8050 /* turn mirroring into duplication */
8051 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8052 BTRFS_BLOCK_GROUP_RAID10))
8053 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04008054 } else {
8055 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008056 if (flags & stripped)
8057 return flags;
8058
8059 stripped |= BTRFS_BLOCK_GROUP_DUP;
8060 stripped = flags & ~stripped;
8061
8062 /* switch duplicated blocks with raid1 */
8063 if (flags & BTRFS_BLOCK_GROUP_DUP)
8064 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8065
Ilya Dryomove3176ca2012-03-27 17:09:16 +03008066 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04008067 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03008068
Chris Masonec44a352008-04-28 15:29:52 -04008069 return flags;
8070}
8071
Miao Xie199c36e2011-07-15 10:34:36 +00008072static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04008073{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008074 struct btrfs_space_info *sinfo = cache->space_info;
8075 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00008076 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008077 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008078
Chris Masonc286ac42008-07-22 23:06:41 -04008079
Miao Xie199c36e2011-07-15 10:34:36 +00008080 /*
8081 * We need some metadata space and system metadata space for
8082 * allocating chunks in some corner cases until we force to set
8083 * it to be readonly.
8084 */
8085 if ((sinfo->flags &
8086 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
8087 !force)
8088 min_allocable_bytes = 1 * 1024 * 1024;
8089 else
8090 min_allocable_bytes = 0;
8091
Yan, Zhengf0486c62010-05-16 10:46:25 -04008092 spin_lock(&sinfo->lock);
8093 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00008094
8095 if (cache->ro) {
8096 ret = 0;
8097 goto out;
8098 }
8099
Yan, Zhengf0486c62010-05-16 10:46:25 -04008100 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8101 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008102
Yan, Zhengf0486c62010-05-16 10:46:25 -04008103 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04008104 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
8105 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008106 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008107 cache->ro = 1;
8108 ret = 0;
8109 }
WuBo61cfea92011-07-26 03:30:11 +00008110out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008111 spin_unlock(&cache->lock);
8112 spin_unlock(&sinfo->lock);
8113 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008114}
8115
Yan, Zhengf0486c62010-05-16 10:46:25 -04008116int btrfs_set_block_group_ro(struct btrfs_root *root,
8117 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008118
8119{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008120 struct btrfs_trans_handle *trans;
8121 u64 alloc_flags;
8122 int ret;
8123
8124 BUG_ON(cache->ro);
8125
Josef Bacik7a7eaa42011-04-13 12:54:33 -04008126 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008127 if (IS_ERR(trans))
8128 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008129
8130 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008131 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04008132 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008133 CHUNK_ALLOC_FORCE);
8134 if (ret < 0)
8135 goto out;
8136 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008137
Miao Xie199c36e2011-07-15 10:34:36 +00008138 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008139 if (!ret)
8140 goto out;
8141 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04008142 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04008143 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008144 if (ret < 0)
8145 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00008146 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008147out:
8148 btrfs_end_transaction(trans, root);
8149 return ret;
8150}
8151
Chris Masonc87f08c2011-02-16 13:57:04 -05008152int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8153 struct btrfs_root *root, u64 type)
8154{
8155 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04008156 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04008157 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05008158}
8159
Miao Xie6d07bce2011-01-05 10:07:31 +00008160/*
8161 * helper to account the unused space of all the readonly block group in the
8162 * list. takes mirrors into account.
8163 */
8164static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8165{
8166 struct btrfs_block_group_cache *block_group;
8167 u64 free_bytes = 0;
8168 int factor;
8169
8170 list_for_each_entry(block_group, groups_list, list) {
8171 spin_lock(&block_group->lock);
8172
8173 if (!block_group->ro) {
8174 spin_unlock(&block_group->lock);
8175 continue;
8176 }
8177
8178 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8179 BTRFS_BLOCK_GROUP_RAID10 |
8180 BTRFS_BLOCK_GROUP_DUP))
8181 factor = 2;
8182 else
8183 factor = 1;
8184
8185 free_bytes += (block_group->key.offset -
8186 btrfs_block_group_used(&block_group->item)) *
8187 factor;
8188
8189 spin_unlock(&block_group->lock);
8190 }
8191
8192 return free_bytes;
8193}
8194
8195/*
8196 * helper to account the unused space of all the readonly block group in the
8197 * space_info. takes mirrors into account.
8198 */
8199u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8200{
8201 int i;
8202 u64 free_bytes = 0;
8203
8204 spin_lock(&sinfo->lock);
8205
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308206 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Miao Xie6d07bce2011-01-05 10:07:31 +00008207 if (!list_empty(&sinfo->block_groups[i]))
8208 free_bytes += __btrfs_get_ro_block_group_free_space(
8209 &sinfo->block_groups[i]);
8210
8211 spin_unlock(&sinfo->lock);
8212
8213 return free_bytes;
8214}
8215
Jeff Mahoney143bede2012-03-01 14:56:26 +01008216void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04008217 struct btrfs_block_group_cache *cache)
8218{
8219 struct btrfs_space_info *sinfo = cache->space_info;
8220 u64 num_bytes;
8221
8222 BUG_ON(!cache->ro);
8223
8224 spin_lock(&sinfo->lock);
8225 spin_lock(&cache->lock);
8226 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8227 cache->bytes_super - btrfs_block_group_used(&cache->item);
8228 sinfo->bytes_readonly -= num_bytes;
8229 cache->ro = 0;
8230 spin_unlock(&cache->lock);
8231 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008232}
8233
Josef Bacikba1bf482009-09-11 16:11:19 -04008234/*
8235 * checks to see if its even possible to relocate this block group.
8236 *
8237 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8238 * ok to go ahead and try.
8239 */
8240int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008241{
Zheng Yan1a40e232008-09-26 10:09:34 -04008242 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008243 struct btrfs_space_info *space_info;
8244 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8245 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04008246 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00008247 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04008248 u64 dev_min = 1;
8249 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008250 u64 target;
liubocdcb7252011-08-03 10:15:25 +00008251 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04008252 int full = 0;
8253 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008254
Josef Bacikba1bf482009-09-11 16:11:19 -04008255 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008256
Josef Bacikba1bf482009-09-11 16:11:19 -04008257 /* odd, couldn't find the block group, leave it alone */
8258 if (!block_group)
8259 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008260
liubocdcb7252011-08-03 10:15:25 +00008261 min_free = btrfs_block_group_used(&block_group->item);
8262
Josef Bacikba1bf482009-09-11 16:11:19 -04008263 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00008264 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04008265 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008266
Josef Bacikba1bf482009-09-11 16:11:19 -04008267 space_info = block_group->space_info;
8268 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008269
Josef Bacikba1bf482009-09-11 16:11:19 -04008270 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008271
Josef Bacikba1bf482009-09-11 16:11:19 -04008272 /*
8273 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008274 * relocate it unless we're able to allocate a new chunk below.
8275 *
8276 * Otherwise, we need to make sure we have room in the space to handle
8277 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008278 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008279 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00008280 (space_info->bytes_used + space_info->bytes_reserved +
8281 space_info->bytes_pinned + space_info->bytes_readonly +
8282 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008283 spin_unlock(&space_info->lock);
8284 goto out;
8285 }
8286 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008287
Josef Bacikba1bf482009-09-11 16:11:19 -04008288 /*
8289 * ok we don't have enough space, but maybe we have free space on our
8290 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008291 * alloc devices and guess if we have enough space. if this block
8292 * group is going to be restriped, run checks against the target
8293 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04008294 */
8295 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05008296
liubocdcb7252011-08-03 10:15:25 +00008297 /*
8298 * index:
8299 * 0: raid10
8300 * 1: raid1
8301 * 2: dup
8302 * 3: raid0
8303 * 4: single
8304 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008305 target = get_restripe_target(root->fs_info, block_group->flags);
8306 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00008307 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008308 } else {
8309 /*
8310 * this is just a balance, so if we were marked as full
8311 * we know there is no space for a new chunk
8312 */
8313 if (full)
8314 goto out;
8315
8316 index = get_block_group_index(block_group);
8317 }
8318
Miao Xiee6ec7162013-01-17 05:38:51 +00008319 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00008320 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04008321 /* Divide by 2 */
8322 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008323 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00008324 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00008325 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04008326 /* Multiply by 2 */
8327 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008328 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00008329 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04008330 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00008331 }
8332
Josef Bacik6df9a952013-06-27 13:22:46 -04008333 /* We need to do this so that we can look at pending chunks */
8334 trans = btrfs_join_transaction(root);
8335 if (IS_ERR(trans)) {
8336 ret = PTR_ERR(trans);
8337 goto out;
8338 }
8339
Josef Bacikba1bf482009-09-11 16:11:19 -04008340 mutex_lock(&root->fs_info->chunk_mutex);
8341 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00008342 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008343
Josef Bacikba1bf482009-09-11 16:11:19 -04008344 /*
8345 * check to make sure we can actually find a chunk with enough
8346 * space to fit our block group in.
8347 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01008348 if (device->total_bytes > device->bytes_used + min_free &&
8349 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04008350 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008351 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008352 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00008353 dev_nr++;
8354
8355 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05008356 break;
liubocdcb7252011-08-03 10:15:25 +00008357
Josef Bacikba1bf482009-09-11 16:11:19 -04008358 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008359 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008360 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008361 mutex_unlock(&root->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04008362 btrfs_end_transaction(trans, root);
Chris Masonedbd8d42007-12-21 16:27:24 -05008363out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008364 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008365 return ret;
8366}
8367
Christoph Hellwigb2950862008-12-02 09:54:17 -05008368static int find_first_block_group(struct btrfs_root *root,
8369 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008370{
Chris Mason925baed2008-06-25 16:01:30 -04008371 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008372 struct btrfs_key found_key;
8373 struct extent_buffer *leaf;
8374 int slot;
8375
8376 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8377 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008378 goto out;
8379
Chris Masond3977122009-01-05 21:25:51 -05008380 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008381 slot = path->slots[0];
8382 leaf = path->nodes[0];
8383 if (slot >= btrfs_header_nritems(leaf)) {
8384 ret = btrfs_next_leaf(root, path);
8385 if (ret == 0)
8386 continue;
8387 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008388 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008389 break;
8390 }
8391 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8392
8393 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008394 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8395 ret = 0;
8396 goto out;
8397 }
Chris Mason0b86a832008-03-24 15:01:56 -04008398 path->slots[0]++;
8399 }
Chris Mason925baed2008-06-25 16:01:30 -04008400out:
Chris Mason0b86a832008-03-24 15:01:56 -04008401 return ret;
8402}
8403
Josef Bacik0af3d002010-06-21 14:48:16 -04008404void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8405{
8406 struct btrfs_block_group_cache *block_group;
8407 u64 last = 0;
8408
8409 while (1) {
8410 struct inode *inode;
8411
8412 block_group = btrfs_lookup_first_block_group(info, last);
8413 while (block_group) {
8414 spin_lock(&block_group->lock);
8415 if (block_group->iref)
8416 break;
8417 spin_unlock(&block_group->lock);
8418 block_group = next_block_group(info->tree_root,
8419 block_group);
8420 }
8421 if (!block_group) {
8422 if (last == 0)
8423 break;
8424 last = 0;
8425 continue;
8426 }
8427
8428 inode = block_group->inode;
8429 block_group->iref = 0;
8430 block_group->inode = NULL;
8431 spin_unlock(&block_group->lock);
8432 iput(inode);
8433 last = block_group->key.objectid + block_group->key.offset;
8434 btrfs_put_block_group(block_group);
8435 }
8436}
8437
Zheng Yan1a40e232008-09-26 10:09:34 -04008438int btrfs_free_block_groups(struct btrfs_fs_info *info)
8439{
8440 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008441 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008442 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008443 struct rb_node *n;
8444
Josef Bacik9e351cc2014-03-13 15:42:13 -04008445 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04008446 while (!list_empty(&info->caching_block_groups)) {
8447 caching_ctl = list_entry(info->caching_block_groups.next,
8448 struct btrfs_caching_control, list);
8449 list_del(&caching_ctl->list);
8450 put_caching_control(caching_ctl);
8451 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04008452 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04008453
Zheng Yan1a40e232008-09-26 10:09:34 -04008454 spin_lock(&info->block_group_cache_lock);
8455 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8456 block_group = rb_entry(n, struct btrfs_block_group_cache,
8457 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008458 rb_erase(&block_group->cache_node,
8459 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008460 spin_unlock(&info->block_group_cache_lock);
8461
Josef Bacik80eb2342008-10-29 14:49:05 -04008462 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008463 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008464 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008465
Josef Bacik817d52f2009-07-13 21:29:25 -04008466 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008467 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008468
Josef Bacik3c148742011-02-02 15:53:47 +00008469 /*
8470 * We haven't cached this block group, which means we could
8471 * possibly have excluded extents on this block group.
8472 */
Josef Bacik36cce922013-08-05 11:15:21 -04008473 if (block_group->cached == BTRFS_CACHE_NO ||
8474 block_group->cached == BTRFS_CACHE_ERROR)
Josef Bacik3c148742011-02-02 15:53:47 +00008475 free_excluded_extents(info->extent_root, block_group);
8476
Josef Bacik817d52f2009-07-13 21:29:25 -04008477 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008478 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008479
8480 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008481 }
8482 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008483
8484 /* now that all the block groups are freed, go through and
8485 * free all the space_info structs. This is only called during
8486 * the final stages of unmount, and so we know nobody is
8487 * using them. We call synchronize_rcu() once before we start,
8488 * just to be on the safe side.
8489 */
8490 synchronize_rcu();
8491
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008492 release_global_block_rsv(info);
8493
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308494 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008495 int i;
8496
Chris Mason4184ea72009-03-10 12:39:20 -04008497 space_info = list_entry(info->space_info.next,
8498 struct btrfs_space_info,
8499 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008500 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308501 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00008502 space_info->bytes_reserved > 0 ||
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308503 space_info->bytes_may_use > 0)) {
David Sterbab069e0c2013-02-08 21:28:17 +00008504 dump_space_info(space_info, 0, 0);
8505 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008506 }
Chris Mason4184ea72009-03-10 12:39:20 -04008507 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008508 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
8509 struct kobject *kobj;
8510 kobj = &space_info->block_group_kobjs[i];
8511 if (kobj->parent) {
8512 kobject_del(kobj);
8513 kobject_put(kobj);
8514 }
8515 }
8516 kobject_del(&space_info->kobj);
8517 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04008518 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008519 return 0;
8520}
8521
Yan, Zhengb742bb82010-05-16 10:46:24 -04008522static void __link_block_group(struct btrfs_space_info *space_info,
8523 struct btrfs_block_group_cache *cache)
8524{
8525 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04008526 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04008527
8528 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04008529 if (list_empty(&space_info->block_groups[index]))
8530 first = true;
8531 list_add_tail(&cache->list, &space_info->block_groups[index]);
8532 up_write(&space_info->groups_sem);
8533
8534 if (first) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008535 struct kobject *kobj = &space_info->block_group_kobjs[index];
8536 int ret;
8537
8538 kobject_get(&space_info->kobj); /* put in release */
Miao Xie536cd962013-12-17 12:01:12 +08008539 ret = kobject_add(kobj, &space_info->kobj, "%s",
8540 get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008541 if (ret) {
Frank Holtonefe120a2013-12-20 11:37:06 -05008542 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008543 kobject_put(&space_info->kobj);
8544 }
8545 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008546}
8547
Miao Xie920e4a52014-01-15 20:00:55 +08008548static struct btrfs_block_group_cache *
8549btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
8550{
8551 struct btrfs_block_group_cache *cache;
8552
8553 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8554 if (!cache)
8555 return NULL;
8556
8557 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8558 GFP_NOFS);
8559 if (!cache->free_space_ctl) {
8560 kfree(cache);
8561 return NULL;
8562 }
8563
8564 cache->key.objectid = start;
8565 cache->key.offset = size;
8566 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8567
8568 cache->sectorsize = root->sectorsize;
8569 cache->fs_info = root->fs_info;
8570 cache->full_stripe_len = btrfs_full_stripe_len(root,
8571 &root->fs_info->mapping_tree,
8572 start);
8573 atomic_set(&cache->count, 1);
8574 spin_lock_init(&cache->lock);
8575 INIT_LIST_HEAD(&cache->list);
8576 INIT_LIST_HEAD(&cache->cluster_list);
8577 INIT_LIST_HEAD(&cache->new_bg_list);
8578 btrfs_init_free_space_ctl(cache);
8579
8580 return cache;
8581}
8582
Chris Mason9078a3e2007-04-26 16:46:15 -04008583int btrfs_read_block_groups(struct btrfs_root *root)
8584{
8585 struct btrfs_path *path;
8586 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008587 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008588 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008589 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008590 struct btrfs_key key;
8591 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008592 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008593 int need_clear = 0;
8594 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008595
Chris Masonbe744172007-05-06 10:15:01 -04008596 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008597 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008598 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008599 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008600 path = btrfs_alloc_path();
8601 if (!path)
8602 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008603 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008604
David Sterba6c417612011-04-13 15:41:04 +02008605 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008606 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008607 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008608 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008609 if (btrfs_test_opt(root, CLEAR_CACHE))
8610 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008611
Chris Masond3977122009-01-05 21:25:51 -05008612 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008613 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008614 if (ret > 0)
8615 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008616 if (ret != 0)
8617 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +08008618
Chris Mason5f39d392007-10-15 16:14:19 -04008619 leaf = path->nodes[0];
8620 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +08008621
8622 cache = btrfs_create_block_group_cache(root, found_key.objectid,
8623 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04008624 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008625 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008626 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008627 }
Josef Bacik96303082009-07-13 21:29:25 -04008628
Liu Bocf7c1ef2012-07-06 03:31:34 -06008629 if (need_clear) {
8630 /*
8631 * When we mount with old space cache, we need to
8632 * set BTRFS_DC_CLEAR and set dirty flag.
8633 *
8634 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8635 * truncate the old free space cache inode and
8636 * setup a new one.
8637 * b) Setting 'dirty flag' makes sure that we flush
8638 * the new space cache info onto disk.
8639 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008640 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008641 if (btrfs_test_opt(root, SPACE_CACHE))
8642 cache->dirty = 1;
8643 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008644
Chris Mason5f39d392007-10-15 16:14:19 -04008645 read_extent_buffer(leaf, &cache->item,
8646 btrfs_item_ptr_offset(leaf, path->slots[0]),
8647 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +08008648 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason0b86a832008-03-24 15:01:56 -04008649
Chris Mason9078a3e2007-04-26 16:46:15 -04008650 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008651 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +08008652
Josef Bacik817d52f2009-07-13 21:29:25 -04008653 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008654 * We need to exclude the super stripes now so that the space
8655 * info has super bytes accounted for, otherwise we'll think
8656 * we have more space than we actually do.
8657 */
Josef Bacik835d9742013-03-19 12:13:25 -04008658 ret = exclude_super_stripes(root, cache);
8659 if (ret) {
8660 /*
8661 * We may have excluded something, so call this just in
8662 * case.
8663 */
8664 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08008665 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04008666 goto error;
8667 }
Josef Bacik3c148742011-02-02 15:53:47 +00008668
8669 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008670 * check for two cases, either we are full, and therefore
8671 * don't need to bother with the caching work since we won't
8672 * find any space, or we are empty, and we can just add all
8673 * the space in and be done with it. This saves us _alot_ of
8674 * time, particularly in the full case.
8675 */
8676 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008677 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008678 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008679 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008680 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008681 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008682 cache->cached = BTRFS_CACHE_FINISHED;
8683 add_new_free_space(cache, root->fs_info,
8684 found_key.objectid,
8685 found_key.objectid +
8686 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008687 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008688 }
Chris Mason96b51792007-10-15 16:15:19 -04008689
Josef Bacik8c579fe2013-04-02 12:40:42 -04008690 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8691 if (ret) {
8692 btrfs_remove_free_space_cache(cache);
8693 btrfs_put_block_group(cache);
8694 goto error;
8695 }
8696
Chris Mason6324fbf2008-03-24 15:01:59 -04008697 ret = update_space_info(info, cache->flags, found_key.offset,
8698 btrfs_block_group_used(&cache->item),
8699 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008700 if (ret) {
8701 btrfs_remove_free_space_cache(cache);
8702 spin_lock(&info->block_group_cache_lock);
8703 rb_erase(&cache->cache_node,
8704 &info->block_group_cache_tree);
8705 spin_unlock(&info->block_group_cache_lock);
8706 btrfs_put_block_group(cache);
8707 goto error;
8708 }
8709
Chris Mason6324fbf2008-03-24 15:01:59 -04008710 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008711 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008712 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008713 spin_unlock(&cache->space_info->lock);
8714
Yan, Zhengb742bb82010-05-16 10:46:24 -04008715 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008716
Chris Mason75ccf472008-09-30 19:24:06 -04008717 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008718 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008719 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008720 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008721
8722 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8723 if (!(get_alloc_profile(root, space_info->flags) &
8724 (BTRFS_BLOCK_GROUP_RAID10 |
8725 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008726 BTRFS_BLOCK_GROUP_RAID5 |
8727 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008728 BTRFS_BLOCK_GROUP_DUP)))
8729 continue;
8730 /*
8731 * avoid allocating from un-mirrored block group if there are
8732 * mirrored block groups.
8733 */
chandan1095cc02013-07-16 12:28:56 +05308734 list_for_each_entry(cache,
8735 &space_info->block_groups[BTRFS_RAID_RAID0],
8736 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008737 set_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05308738 list_for_each_entry(cache,
8739 &space_info->block_groups[BTRFS_RAID_SINGLE],
8740 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008741 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008742 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008743
8744 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008745 ret = 0;
8746error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008747 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008748 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008749}
Chris Mason6324fbf2008-03-24 15:01:59 -04008750
Josef Bacikea658ba2012-09-11 16:57:25 -04008751void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8752 struct btrfs_root *root)
8753{
8754 struct btrfs_block_group_cache *block_group, *tmp;
8755 struct btrfs_root *extent_root = root->fs_info->extent_root;
8756 struct btrfs_block_group_item item;
8757 struct btrfs_key key;
8758 int ret = 0;
8759
8760 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8761 new_bg_list) {
8762 list_del_init(&block_group->new_bg_list);
8763
8764 if (ret)
8765 continue;
8766
8767 spin_lock(&block_group->lock);
8768 memcpy(&item, &block_group->item, sizeof(item));
8769 memcpy(&key, &block_group->key, sizeof(key));
8770 spin_unlock(&block_group->lock);
8771
8772 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8773 sizeof(item));
8774 if (ret)
8775 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacik6df9a952013-06-27 13:22:46 -04008776 ret = btrfs_finish_chunk_alloc(trans, extent_root,
8777 key.objectid, key.offset);
8778 if (ret)
8779 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacikea658ba2012-09-11 16:57:25 -04008780 }
8781}
8782
Chris Mason6324fbf2008-03-24 15:01:59 -04008783int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8784 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008785 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008786 u64 size)
8787{
8788 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008789 struct btrfs_root *extent_root;
8790 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008791
8792 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008793
Miao Xie995946d2014-04-02 19:51:06 +08008794 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -04008795
Miao Xie920e4a52014-01-15 20:00:55 +08008796 cache = btrfs_create_block_group_cache(root, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008797 if (!cache)
8798 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008799
Chris Mason6324fbf2008-03-24 15:01:59 -04008800 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008801 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
Chris Mason6324fbf2008-03-24 15:01:59 -04008802 btrfs_set_block_group_flags(&cache->item, type);
8803
Miao Xie920e4a52014-01-15 20:00:55 +08008804 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -04008805 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008806 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008807 ret = exclude_super_stripes(root, cache);
8808 if (ret) {
8809 /*
8810 * We may have excluded something, so call this just in
8811 * case.
8812 */
8813 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08008814 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04008815 return ret;
8816 }
Josef Bacik96303082009-07-13 21:29:25 -04008817
Josef Bacik817d52f2009-07-13 21:29:25 -04008818 add_new_free_space(cache, root->fs_info, chunk_offset,
8819 chunk_offset + size);
8820
Yan Zheng11833d62009-09-11 16:11:19 -04008821 free_excluded_extents(root, cache);
8822
Josef Bacik8c579fe2013-04-02 12:40:42 -04008823 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8824 if (ret) {
8825 btrfs_remove_free_space_cache(cache);
8826 btrfs_put_block_group(cache);
8827 return ret;
8828 }
8829
Chris Mason6324fbf2008-03-24 15:01:59 -04008830 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8831 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008832 if (ret) {
8833 btrfs_remove_free_space_cache(cache);
8834 spin_lock(&root->fs_info->block_group_cache_lock);
8835 rb_erase(&cache->cache_node,
8836 &root->fs_info->block_group_cache_tree);
8837 spin_unlock(&root->fs_info->block_group_cache_lock);
8838 btrfs_put_block_group(cache);
8839 return ret;
8840 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008841 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008842
8843 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008844 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008845 spin_unlock(&cache->space_info->lock);
8846
Yan, Zhengb742bb82010-05-16 10:46:24 -04008847 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008848
Josef Bacikea658ba2012-09-11 16:57:25 -04008849 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008850
Chris Masond18a2c42008-04-04 15:40:00 -04008851 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008852
Chris Mason6324fbf2008-03-24 15:01:59 -04008853 return 0;
8854}
Zheng Yan1a40e232008-09-26 10:09:34 -04008855
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008856static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8857{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008858 u64 extra_flags = chunk_to_extended(flags) &
8859 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008860
Miao Xiede98ced2013-01-29 10:13:12 +00008861 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008862 if (flags & BTRFS_BLOCK_GROUP_DATA)
8863 fs_info->avail_data_alloc_bits &= ~extra_flags;
8864 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8865 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8866 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8867 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008868 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008869}
8870
Zheng Yan1a40e232008-09-26 10:09:34 -04008871int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8872 struct btrfs_root *root, u64 group_start)
8873{
8874 struct btrfs_path *path;
8875 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008876 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008877 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008878 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008879 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008880 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008881 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008882 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008883
Zheng Yan1a40e232008-09-26 10:09:34 -04008884 root = root->fs_info->extent_root;
8885
8886 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8887 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008888 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008889
liubo9f7c43c2011-03-07 02:13:33 +00008890 /*
8891 * Free the reserved super bytes from this block group before
8892 * remove it.
8893 */
8894 free_excluded_extents(root, block_group);
8895
Zheng Yan1a40e232008-09-26 10:09:34 -04008896 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008897 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008898 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8899 BTRFS_BLOCK_GROUP_RAID1 |
8900 BTRFS_BLOCK_GROUP_RAID10))
8901 factor = 2;
8902 else
8903 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008904
Chris Mason44fb5512009-06-04 15:34:51 -04008905 /* make sure this block group isn't part of an allocation cluster */
8906 cluster = &root->fs_info->data_alloc_cluster;
8907 spin_lock(&cluster->refill_lock);
8908 btrfs_return_cluster_to_free_space(block_group, cluster);
8909 spin_unlock(&cluster->refill_lock);
8910
8911 /*
8912 * make sure this block group isn't part of a metadata
8913 * allocation cluster
8914 */
8915 cluster = &root->fs_info->meta_alloc_cluster;
8916 spin_lock(&cluster->refill_lock);
8917 btrfs_return_cluster_to_free_space(block_group, cluster);
8918 spin_unlock(&cluster->refill_lock);
8919
Zheng Yan1a40e232008-09-26 10:09:34 -04008920 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008921 if (!path) {
8922 ret = -ENOMEM;
8923 goto out;
8924 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008925
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008926 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008927 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008928 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008929 if (ret) {
8930 btrfs_add_delayed_iput(inode);
8931 goto out;
8932 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008933 clear_nlink(inode);
8934 /* One for the block groups ref */
8935 spin_lock(&block_group->lock);
8936 if (block_group->iref) {
8937 block_group->iref = 0;
8938 block_group->inode = NULL;
8939 spin_unlock(&block_group->lock);
8940 iput(inode);
8941 } else {
8942 spin_unlock(&block_group->lock);
8943 }
8944 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008945 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008946 }
8947
8948 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8949 key.offset = block_group->key.objectid;
8950 key.type = 0;
8951
8952 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8953 if (ret < 0)
8954 goto out;
8955 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008956 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008957 if (ret == 0) {
8958 ret = btrfs_del_item(trans, tree_root, path);
8959 if (ret)
8960 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008961 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008962 }
8963
Yan Zheng3dfdb932009-01-21 10:49:16 -05008964 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008965 rb_erase(&block_group->cache_node,
8966 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008967
8968 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8969 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008970 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008971
Josef Bacik80eb2342008-10-29 14:49:05 -04008972 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008973 /*
8974 * we must use list_del_init so people can check to see if they
8975 * are still on the list after taking the semaphore
8976 */
8977 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008978 if (list_empty(&block_group->space_info->block_groups[index])) {
8979 kobject_del(&block_group->space_info->block_group_kobjs[index]);
8980 kobject_put(&block_group->space_info->block_group_kobjs[index]);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008981 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008982 }
Josef Bacik80eb2342008-10-29 14:49:05 -04008983 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008984
Josef Bacik817d52f2009-07-13 21:29:25 -04008985 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008986 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008987
8988 btrfs_remove_free_space_cache(block_group);
8989
Yan Zhengc146afa2008-11-12 14:34:12 -05008990 spin_lock(&block_group->space_info->lock);
8991 block_group->space_info->total_bytes -= block_group->key.offset;
8992 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008993 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008994 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008995
Josef Bacik0af3d002010-06-21 14:48:16 -04008996 memcpy(&key, &block_group->key, sizeof(key));
8997
Chris Mason283bb192009-07-24 16:30:55 -04008998 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008999
Chris Masonfa9c0d792009-04-03 09:47:43 -04009000 btrfs_put_block_group(block_group);
9001 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04009002
9003 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
9004 if (ret > 0)
9005 ret = -EIO;
9006 if (ret < 0)
9007 goto out;
9008
9009 ret = btrfs_del_item(trans, root, path);
9010out:
9011 btrfs_free_path(path);
9012 return ret;
9013}
liuboacce9522011-01-06 19:30:25 +08009014
liuboc59021f2011-03-07 02:13:14 +00009015int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
9016{
9017 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00009018 struct btrfs_super_block *disk_super;
9019 u64 features;
9020 u64 flags;
9021 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00009022 int ret;
9023
David Sterba6c417612011-04-13 15:41:04 +02009024 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00009025 if (!btrfs_super_root(disk_super))
9026 return 1;
liuboc59021f2011-03-07 02:13:14 +00009027
liubo1aba86d2011-04-08 08:44:37 +00009028 features = btrfs_super_incompat_flags(disk_super);
9029 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
9030 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00009031
liubo1aba86d2011-04-08 08:44:37 +00009032 flags = BTRFS_BLOCK_GROUP_SYSTEM;
9033 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00009034 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00009035 goto out;
liuboc59021f2011-03-07 02:13:14 +00009036
liubo1aba86d2011-04-08 08:44:37 +00009037 if (mixed) {
9038 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
9039 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9040 } else {
9041 flags = BTRFS_BLOCK_GROUP_METADATA;
9042 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9043 if (ret)
9044 goto out;
9045
9046 flags = BTRFS_BLOCK_GROUP_DATA;
9047 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9048 }
9049out:
liuboc59021f2011-03-07 02:13:14 +00009050 return ret;
9051}
9052
liuboacce9522011-01-06 19:30:25 +08009053int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
9054{
9055 return unpin_extent_range(root, start, end);
9056}
9057
9058int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00009059 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08009060{
Li Dongyang5378e602011-03-24 10:24:27 +00009061 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08009062}
Li Dongyangf7039b12011-03-24 10:24:28 +00009063
9064int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
9065{
9066 struct btrfs_fs_info *fs_info = root->fs_info;
9067 struct btrfs_block_group_cache *cache = NULL;
9068 u64 group_trimmed;
9069 u64 start;
9070 u64 end;
9071 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08009072 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00009073 int ret = 0;
9074
Liu Bo2cac13e2012-02-09 18:17:41 +08009075 /*
9076 * try to trim all FS space, our block group may start from non-zero.
9077 */
9078 if (range->len == total_bytes)
9079 cache = btrfs_lookup_first_block_group(fs_info, range->start);
9080 else
9081 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00009082
9083 while (cache) {
9084 if (cache->key.objectid >= (range->start + range->len)) {
9085 btrfs_put_block_group(cache);
9086 break;
9087 }
9088
9089 start = max(range->start, cache->key.objectid);
9090 end = min(range->start + range->len,
9091 cache->key.objectid + cache->key.offset);
9092
9093 if (end - start >= range->minlen) {
9094 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00009095 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04009096 if (ret) {
9097 btrfs_put_block_group(cache);
9098 break;
9099 }
9100 ret = wait_block_group_cache_done(cache);
9101 if (ret) {
9102 btrfs_put_block_group(cache);
9103 break;
9104 }
Li Dongyangf7039b12011-03-24 10:24:28 +00009105 }
9106 ret = btrfs_trim_block_group(cache,
9107 &group_trimmed,
9108 start,
9109 end,
9110 range->minlen);
9111
9112 trimmed += group_trimmed;
9113 if (ret) {
9114 btrfs_put_block_group(cache);
9115 break;
9116 }
9117 }
9118
9119 cache = next_block_group(fs_info->tree_root, cache);
9120 }
9121
9122 range->len = trimmed;
9123 return ret;
9124}
Miao Xie8257b2d2014-03-06 13:38:19 +08009125
9126/*
9127 * btrfs_{start,end}_write() is similar to mnt_{want, drop}_write(),
9128 * they are used to prevent the some tasks writing data into the page cache
9129 * by nocow before the subvolume is snapshoted, but flush the data into
9130 * the disk after the snapshot creation.
9131 */
9132void btrfs_end_nocow_write(struct btrfs_root *root)
9133{
9134 percpu_counter_dec(&root->subv_writers->counter);
9135 /*
9136 * Make sure counter is updated before we wake up
9137 * waiters.
9138 */
9139 smp_mb();
9140 if (waitqueue_active(&root->subv_writers->wait))
9141 wake_up(&root->subv_writers->wait);
9142}
9143
9144int btrfs_start_nocow_write(struct btrfs_root *root)
9145{
9146 if (unlikely(atomic_read(&root->will_be_snapshoted)))
9147 return 0;
9148
9149 percpu_counter_inc(&root->subv_writers->counter);
9150 /*
9151 * Make sure counter is updated before we check for snapshot creation.
9152 */
9153 smp_mb();
9154 if (unlikely(atomic_read(&root->will_be_snapshoted))) {
9155 btrfs_end_nocow_write(root);
9156 return 0;
9157 }
9158 return 1;
9159}